1、说明:
SendEmail是一个轻量级,命令行的SMTP邮件客户端。如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大.这个被设计用在php、bash
perl和web站点使用。请注意,不是sendmail。我第一次看到这个名字,直接yum install -y sendmail了,不是通过yum安装的命令
首先注册一个公司的邮箱地址,一般都是163企业邮箱或者腾讯企业邮箱,域名是自己公司的,在公司邮箱上增加一个用户,邮件地址:a@b.com
下载软件:已将其下载:sendEmail-v1.56.tar.gz
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
解压软件
tar zxf sendEmail-v1.56.tar.gz
这里有整个邮件发送参数配置,包括了各种用到的情况
--------------- Usage Overview --------------- sendEmail-1.56 by Brandon Zehm <caspian@dotconf.net> Synopsis: sendEmail -f ADDRESS [options] Required: -f ADDRESS from (sender) email address * At least one recipient required via -t, -cc, or -bcc * Message body required via -m, STDIN, or -o message-file=FILE Common: -t ADDRESS [ADDR ...] to email address(es) -u SUBJECT message subject -m MESSAGE message body -s SERVER[:PORT] smtp mail relay, default is localhost:25 Optional: -a FILE [FILE ...] file attachment(s) -cc ADDRESS [ADDR ...] cc email address(es) -bcc ADDRESS [ADDR ...] bcc email address(es) -xu USERNAME username for SMTP authentication -xp PASSWORD password for SMTP authentication Paranormal: -b BINDADDR[:PORT] local host bind address -l LOGFILE log to the specified file -v verbosity, use multiple times for greater effect -q be quiet (i.e. no STDOUT output) -o NAME=VALUE advanced options, for details try: --help misc -o message-content-type=<auto|text|html|other> -o message-file=FILE -o message-format=raw -o message-header=HEADER -o message-charset=CHARSET -o reply-to=ADDRESS -o timeout=SECONDS -o username=USERNAME -o password=PASSWORD -o tls=<auto|yes|no> -o fqdn=FQDN Help: --help the helpful overview you're reading now --help addressing explain addressing and related options --help message explain message body input and related options --help networking explain -s, -b, etc --help output explain logging and other output options --help misc explain -o options, TLS, SMTP auth, and more --------------- Examples --------------- Simple Email: sendEmail -f me@gmail.com \ -t friend@yahoo.com \ -s smtp.gmail.com:587 \ -xu me@gmail.com \ -xp MY-PASSWORD \ -u "Test email" \ -m "Hi buddy, this is a test email." Sending to mutiple people: sendEmail -f myaddress@isp.net \ -t "Scott Thomas <scott@isp.net>" jason@isp.net renee@isp.net \ -s relay.isp.net \ -u "Test email" \ -m "Hi guys, this is a test email." Sending to multiple people using cc and bcc recipients: (notice the different way I specified multiple To recipients, you can do this for cc and bcc as well) sendEmail -f myaddress@isp.net \ -t scott@isp.net;jason@isp.net;renee@isp.net \ -cc jennifer@isp.net paul@isp.net jeremiah@isp.net \ -bcc troy@isp.net miranda@isp.net jay@isp.net \ -s relay.isp.net \ -u "Test email with cc and bcc recipients" \ -m "Hi guys, this is a test email." Sending to multiple people with multiple attachments: sendEmail -f myaddress@isp.net \ -t jason@isp.net \ -cc jennifer@isp.net paul@isp.net jeremiah@isp.net \ -s relay.isp.net \ -u "Test email with cc and bcc recipients" \ -m "Hi guys, this is a test email." \ -a /mnt/storage/document.sxw "/root/My Documents/Work Schedule.kwd" Sending an email with the contents of a file as the message body: cat /tmp/file.txt | sendEmail -f myaddress@isp.net \ -t jason@isp.net \ -s relay.isp.net \ -u "Test email with contents of file" Sending an email with the contents of a file as the message body (method 2): sendEmail -f myaddress@isp.net \ -t jason@isp.net \ -s relay.isp.net \ -o message-file=/tmp/file.txt \ -u "Test email with contents of file" Sending an html email: (make sure your html file has <html> at the beginning) cat /tmp/file.html | sendEmail -f myaddress@isp.net \ -t jason@isp.net \ -s relay.isp.net \ -u "Test email with html content"
有时间测试上面的所有的情况
cc是抄送的意思,bcc是密送的意思
举例:
如果A 发送邮件给B、C,抄送给D、E,密送给F、G,那么:
B、C、D、E、F、G都能收到这封邮件。
B知道这是A发送给他和C的邮件,并且抄送给了D、E,但不知道密送给了F、G。
D知道这是A发送给B、C的邮件,并且抄送给了他和E,但也不知道密送给了F、G。
F知道这是A发送给B、C的邮件,并且抄送给了D、E,密送给了他,但不知道同时还密送给了G。
———
进入目录
cd /root/sendEmail-v1.56
复制文件,并设置权限
cp -a sendEmail /bin #因为已经有权限了,不再设置增加执行权限
安装组件
yum install perl-Net-SSLeay perl-IO-Socket-SSL -y
这里编写了一个脚本,可以使用这个脚本来进行发送邮件
如果是centos7,需要加上: -o tls=no 选项,不然报错
–
[root@master ~]#cat sendmail.sh #!/bin/bash fun_sendmail(){ #下面的变量中,如果没有就留空即可 to_contact="a@c.com" #这里是收件人地址 copy_contact="b@c.com" #这里是抄送人地址 from_address="c@c.com" #这里是发件人地址 from_passwd="aaa" #这里是发件人密码 smtp_srv="smtp.ym.163.com" subject="test" #标题 content="/tmp/abc.log" #如果是将文件内容当做邮件内容,这里是文件的路径,单独一句话使用"-m"选项 send_file="/tmp/abc.log" #如果有附件要发送,这里是附件位置,测试50MB以内可以成功 MaxCount=5 #最大发送尝试次数 echo echo "$(date +%F_%T) start sendEmail" while [[ $SendCount -le $MaxCount ]]; do /bin/sendEmail \ -f $from_address \ -t $to_contact \ -cc $copy_contact \ -s $smtp_srv \ -u $subject \ -a $send_file \ -o message-file=$content \ -o message-content-type=auto \ -o message-charset=utf8 \ -xu $from_address \ -xp $from_passwd SendStatus=$? let SendCount=SendCount+1 if [ $SendStatus -eq 0 ]; then break fi done echo "SendStatus: $SendStatus" echo "SendCount: $SendCount" echo "subject: $subject" echo "$(date +%F_%T) stop sendEmail" echo } fun_sendmail &>> /var/log/sendEmail.log
说明:
smtp.exmail.qq.com 表示邮箱的smtp服务器,因为我是用的腾讯企业邮箱。如果是其他邮箱,需要修改:发送邮件服务器:smtp.exmail.qq.com (端口 25),使用SSL,端口号465 编辑完成后,给脚本权限
chmod +x sendEmail.sh
如果脚本执行没有报错,收不到邮件的话,请检查linux网络问题,iptables、selinux是否关闭。
–
–
–
评论前必须登录!
注册