Postfix 是邮件传输代理软件。Postfix试图更快、更容易管理、更安全,同时还与sendmail保持足够的兼容性。 mail是postfix一个常用的命令,我们在很多地方都要用到它,比如定时把系统状况或日志发送到我们的邮箱里等等。
1. 发送邮箱相关服务安装
## centos/rethat
apt-get update
#apt-get install bsd-mailx
apt-get install postfix
# apt-get install -y mailutils sendmail
## centos/rethat
# yum install mailx
# 配置postfix基础配置
dpkg-reconfigure postfix
#apt-get install -y heirloom-mailx
vim /etc/mail.rc
set from=xxxxx@qq.com #设置发送邮箱
set smtp="smtp.qq.com" #设置smtp服务器和端口
set smtp-auth=login #认证方式
set smtp-auth-user="xxxxx@qq.com" #设置用户名,记得加域名啊
set smtp-auth-password="<授权码>" #邮箱密码,不是什么授权码
#echo -e "set from=xxxx@163.com\nset smtp=smtp.163.com\nset smtp-auth-user=xxxx@163.com\nset smtp-auth-password=xxxx\nset smtp-auth=login" > /etc/mail.rc
说明:
- from: 对方收到邮件时显示的发件人
- smtp: 指定第三方发送邮件的smtp服务器地址
- smtp-auth: SMTP的认证方式。默认是LOGIN,也可改为CRAM-MD5或PLAIN方式
- smtp-auth-user: 第三方发邮件的用户名
- smtp-auth-password: 用户名对应密码
这里使用了QQ邮箱的SMTP,需要做以下配置:登录第三方客户端时,密码框中输入授权码。
1.1. 报错 postdrop: warning: unable to look up public/pickup: No such file or directory
/etc/postfix/main.cf:这个文件保存全局配置信息,所有进程都会用到,除非这些配置在 master.cf 文件中被重新设置了。
/etc/postfix/master.cf:这个文件保存了额外的进程运行时环境参数,在 main.cf 文件中定义的配置可能会被本文件的配置覆盖掉
#rm -f /var/spool/mail
echo "content" | mail -s "topic" xxx@126.com
echo "content" | mail -s "topic" turbock@126.com
postdrop: warning: unable to look up public/pickup: No such file or directory
报了一下错
send-mail: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol
send-mail: warning: inet_protocols: configuring for IPv4 support only
postdrop: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol
postdrop: warning: inet_protocols: configuring for IPv4 support only
postdrop: warning: unable to look up public/pickup: No such file or directory
修改方法:
#查看当前的inet_protocols
/usr/sbin/postconf | grep inet_protocols
inet_protocols = all
vi /etc/postfix/main.cf
#inet_protocols = all 改为
inet_protocols = ipv4
#重启
#/etc/init.d/postfix restart
service postfix start
service postfix reload
1.2. postfix服务未启动
启动命令
sudo /usr/sbin/postfix start
报错:/etc/init.d/postfix: line 73: [: =: unary operator expected
原因:[ "$NETWORKING" = "no" ] && exit 0
解决:1)vi /etc/sysconfig/network 并添加一行
NETWORKING=yes
2)重启网络服务:/etc/init.d/network restart
3) 启动postfix : sudo /usr/sbin/postfix start
1.3. 2.新发送邮件未送达
新发送邮件未送达,历史邮件零星送达。推测postfix邮件发送阻塞堆积
- 查询邮件任务队列:发现已被灌水
postqueue -p - 尝试清除队列中邮件
sudo postsuper -d ALL
PS:因队列堆积太多,清理失败 - 移动邮件任务队列文件夹,并重新创建
mv /var/spool/postfix/maildrop /var/spool/postfix/maildrop.bck
mkdir /var/spool/postfix/maildrop - 执行发送脚本,发现没有执行权限
解决:chmod -R 755 /var/spool/postfix/*
1.4. 相关命令
- 查看队列
postqueue -p - 根据ID删除队列邮件
postsuper -d MessageID - 删除所有邮件
postsuper -d ALL
运行相关
- 启动postfix
postfix start - 重启postfix
postfix reload - 关闭postfix
postfix stop - 检测postfix服务是否正常
postfix check
2. 命令详解
首先通过help参数来查看mail的所有参数如下:
mail -eiIUdEFntBDNHRVv~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users
-a #带附件
-A #配置好的发送邮件的账号
-s #邮件标题
-b #指定密件副本的收信人地址
-c #指定副本的收信人地址
-u #读取指定用户的邮件
-f #读取指定邮件文件中的邮件
3. 最简单的三种发邮件方式
3.1. 发送给某用户并编辑内容
mail -s "this is a title" bbmonkey62@163.com
回车,继续输入文本,也就是邮件内容
this is content
然后按Ctrl+D退出。
EOT
3.2. 不用回车输入邮件内容发送邮件
在上面的例子中,你如果照做的话,你会发现很不方便,那就是每次输入邮件内容都需要在命令运行后回车输入内容,最后还要再按Ctrl+D完成。实际上,可以不用这么费劲的,如下:
echo "this is content" | mail -s "this is a title" bbmonkey62@163.com
通过echo命令来输入邮件内容"this is content",这样就简单多了!
3.3. 发送给多个邮箱
上面是最简单的例子,假如我们想要发送给多个邮箱呢?在上面的邮箱后面加另外的邮箱地址,邮箱间以空格隔开即可,如下:
mail -s "this is a title" bbmonkey62@163.com admin@163.com
3.4. 指定发送者发送
在上面的例子中,发件人是Linux当前的登录用户名,比如我当前登录的用户名为admin,那么发送的邮箱地址是admin@163.com,如果邮箱后缀域名是163.com的话。但有时我们想写上发件人的名称,还有不想采用默认的邮箱地址,这时我们可以这么做:
通过SMTP发送时,mail手册页建议以这种方式设置from变量;
echo "this is content test" | mail -s "this is a title" -r 'sender@163.com' reciever@163.com
# echo "this is content test" | mail -s "test_title" -S from=aaaaa@126.com bbbbbbb@163.com
echo "content 1" | mail -s "test_title" turbock@163.com
这时发件人邮箱就变为了 myname@zihou.me,一些垃圾邮件就是采用的这种方法,隐藏自己真实的邮件地址。
3.5. 指定邮件回复地址
如果我们想要收件人回复邮件时,回复的邮箱地址与我们的发信邮箱不同,那么我们可以自定义回复邮箱地址,如下:
mail -s "this is a title" -r "myname@zihou.me" -R "myreply@zihou.me" bbmonkey62@163.com
那么收信人回复时,会默认发送到myreply@zihou.me,而不是默认的myname@zihou.me。
3.6. 发送附件
有时我们需要发送附件,那么也是非常容易的,如下:
mail -s "this is a title" -r "myname@zihou.me" -a "/home/hadoop/report/3.txt" bbmonkey62@163.com
在这里要说明一下的时,如果我们的附件是不大的文本文件,我们有时也用不着发送附件,可以直接将附件的文本当做邮件内容发送,这样通过邮件查看时也更方便些,如下:
mail -s "this is a test" bbmonkey62@163.com < /home/hadoop/project/3.txt
4. 查看邮件
通过mail命令也可以查看收到的邮件,这个我就简单说一下吧,毕竟这个相对不常用,命令很简单,直接输入mail,会出现收到的邮件,每个邮件都对应一个序列号,如果要查看某封邮件,直接在?后输入数字序列号即可。
在?后输入help可以查看所有的参数,如下:
? help
mail commands
type <message list> type messages
next goto and type next message
from <message list> give head lines of messages
headers print out active message headers
delete <message list> delete messages
undelete <message list> undelete messages
save <message list> folder append messages to folder and mark as saved
copy <message list> folder append messages to folder without marking them
write <message list> file append message texts to file, save attachments
preserve <message list> keep incoming messages in mailbox even if saved
Reply <message list> reply to message senders
reply <message list> reply to message senders and all recipients
mail addresses mail to specific recipients
file folder change to another folder
quit quit and apply changes to folder
xit quit and discard changes made to folder
! shell escape
cd <directory> chdir to directory or home if none given
list list names of all available commands</p> <p>A <message list> consists of integers, ranges of same, or other criteria
separated by spaces. If omitted, mail uses the last message typed.
在?后也可以直接输入mail写信哦,就跟上面的例子一样,所不同的是,每步都会有提示,如下:
? mail
To: bbmonkey62@163.com
Subject: this is a title
content
EOT
sendmial
service sendmail stop
chkconfig sendmail off
postfix
service postfix stop
chkconfig postfix off
SMTP错误码/建议解决方法
https://blog.csdn.net/chenfei_5201213/article/details/10138969
参考文档
https://zhuanlan.zhihu.com/p/109084397
https://www.bbsmax.com/A/MAzAnp1oJ9/
https://blog.csdn.net/wych1981/article/details/7578322
exim4 https://manpages.debian.org/jessie/exim4-config/exim4_passwd_client.5.en.html
Exim4 配置外部 SMTP 服务器发邮件 https://www.hostarr.com/exim4-external-smtp/
linux(debian)下邮件发送 https://www.shuzhiduo.com/A/QW5YYOO95m/