Linux——mail

mailx是linux系统上常见的邮件发送工具,可以通过配置smtp服务器使用大部分的邮箱发送邮件。

一、插件安装

[root@master ~]#yum -y install mailx

二、配置文件

[root@master ~]#vim /etc/mail.rc    
set from=linuxbe@126.com   #设置发件人的信息
set smtp=smtp.126.com  #配置为smtp. 126 .com
set smtp-auth-user= linuxbe @126.com #配置发件人邮箱地址
set smtp-auth-password=FFSFD112sf    #客户端授权码,不是邮箱地址密码!!!!!!
set smtp-auth=login    #邮件认证方式

三、测试验证

1.管道方式
[root@master ~]#cat /etc/passwd | mail -s test linuxbe@126.com
[root@master ~]#echo "邮件内容" | mail -s "邮件主题"   linuxbe @foxmail.com
2.重定向方式
[root@master ~]#mail -s "邮件标题" -a data.txt linuxbe@126.com < data.txt        #文件内容读取输出     
[root@master ~]#echo "邮件内容" | mail -s "邮件标题" -a data.txt linuxbe@126.com     #直接填写正文输出     

四、发送文件编码

利用uuencode编码,利用uudecode解码。uuencode 是将二进制文件转换为文本文件的过程,转换后的文件可以通过纯文本 e-mail 进行传输,在接收方对该文件进行 uudecode,即将其转换为初始的二进制文件。

1.安装包

uuencode 包含在 sharutils 包里,需要安装 sharutils。

[root@localhost ~]# yum install -y sharutils
2.编码

uuencode 编码后的文件比原文件要大

[root@localhost ~]# uuencode file.txt file.txt
begin 644 file.txt
=:&5L;&\@=V]R;&0*"C$Q+C$Q(&ES(&-O;6EN9PH``
end

使用 -m 选项,则使用 base64 编码格式。
第一个 file.txt 是需要编码的文件名,第二个 file.txt 是编码后的文件名(解码时使用)。

[root@localhost ~]# uuencode file.txt file.txt > file.txt.uue
[root@localhost ~]# ls -l file.txt file.txt.uue 
-rw-r--r--. 1 root root 29 11月 10 15:26 file.txt
-rw-r--r--. 1 root root 67 11月 11 13:35 file.txt.uue
3.解码

新建 tmp 目录,把 file.txt.uue 拷贝进去,用 uudecode 对文件解码。

[root@localhost tmp]# uudecode file.txt.uue

解码后的文件名,编码时输入的第二个 file.txt 参数,文件的权限为 644,也就是编码时默认的 644。

4.mail 命令结合 uuencode 发送附件

用 mail 命令结合 uuencode 快速发送邮件附件

[root@localhost ~]# uuencode file.txt file.txt | mail -s "邮件主题" 123456@qq.com

包含两个及以上附件

[root@localhost ~]# (uuencode file.txt file.txt;uuencode file2.txt file2.txt) | mail -s "邮件主题" 123456@qq.com

在 Winows 中,用 Outlook、Foxmail 等客户端能自动识别经过 uuencode 编码的附件。如果附件被收取到 Linux/Unix 等系统下,如果 Linux 下的 MUA 或者客户端不识别,可以手工处理邮件提取相应的部分,用 uudecode 解码即可。

vim send_mail.sh
#!/bin/bash
help() {
   echo "eg: $0 [subject] [address] [content_file]"   # 脚本发送格式
   exit 1
}

if [ ! -n "$1" ] ; then
    help
fi

if [ ! -n "$2" ] ; then
    help
else
    mail_to=$2
    echo "Send mail to ${mail_to} ..."
fi

if [ ! -n "$3" ] ; then
    help
else
    mail -s $1 ${mail_to} < $3  # 发送邮件
fi   
[root@localhost ~]# ./send_mail.sh "邮件标题" 123456@qq.com,888888@qq.com file.txt
Send mail to 123456@qq.com,888888@qq.com ...

————Blueicex 2020/2/4 7:50 blueice1980@126.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值