ubuntu配置mail邮件服务器
背景:最近在看项目中前辈写过的shell,当成功或失败时有邮件发出,并且发送邮件的命令很简单,只是一个mail命令,由于之前不是很了解,所以简单看了一下,并在自己的虚拟机上测试了一下。所以此文的目的主要是给像我一样的新手入门参考的。环境:Ubuntu14.04, 没有安装mail命令摘要:ubuntu中sendmail函数可以很方便的发送邮件,ubuntu sendmail先要...
背景:
最近在看项目中前辈写过的shell,当成功或失败时有邮件发出,并且发送邮件的命令很简单,只是一个mail命令,由于之前不是很了解,所以简单看了一下,并在自己的虚拟机上测试了一下。所以此文的目的主要是给像我一样的新手入门参考的。
环境:
Ubuntu14.04, 没有安装mail命令
摘要:
ubuntu中sendmail函数可以很方便的发送邮件,ubuntu sendmail先要安装两个包,再安装一个工具包
第一步:安装sendmail
sudo apt-get install sendmail
sudo apt-get install sendmail-cf
sudo apt-get install mailutils
安装过程中会出现如下界面:
原文说这个System mail name会被用到,我自己测试发现并没有使用,默认发出邮件的名字是当前登录用户的名字,并不是这里配置的System mail name,给本机发邮件可以使用也可以只使用username,下文会提到。
这里一路选择默认就可以了。
安装完以后检查一下:
ps aux |grep sendmail
darren@ubuntu:~/darren$ ps aux |grep sendmail
root 1276 0.0 0.4 100728 4920 ? Ss Apr12 0:06 sendmail: MTA: accepting connections
darren 11770 0.0 0.2 11764 2252 pts/0 S+ 13:42 0:00 grep --color=auto sendmail
说明sendmail 已经安装成功并启动了
第二步:配置sendmail
sendmail 默认只会为本机用户发送邮件,只有把它扩展到整个Internet,才会成为真正的邮件服务器。
打开sendmail的配置宏文件:/etc/mail/sendmail.mc,修改如下行
vim /etc/mail/sendmail.mc
DAEMON_OPTIONS(`Family=inet, Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl
//修改为
DAEMON_OPTIONS(`Family=inet, Name=MTA-v4, Port=smtp, Addr=0.0.0.0')dnl
0.0.0.0表名可以连接任何服务器
重新生成配置文件:
cd /etc/mail
mv sendmail.cf sendmail.cf_bk //做一个备份
m4 sendmail.mc > sendmail.cf //>的左右有空格,提示错误没有安装sendmail-cf
//注意,如过最后一个命令报没有权限,需要切换到root上执行
第三步:测试发送邮件
echo “邮件正文” | mail -s 邮件主题 test@126.com //简单方式
mail -s 邮件主题 test@126.com < test.txt //文件内容发送方式
其它可选想如下:
-a, --append=HEADER: VALUE append given header to the message being sent
//追加,例如-aFrom: xxx@yyy.com, 指定邮件是谁发出的
-A, --attach=FILE
attach FILE
//发送附件,-A 文件路径, 注意如果发送多个附件,需要使用多次-A, 例如:-A file1 -A file2
-s, --subject=SUBJ
send a message with the given SUBJECT
//指定邮件标题
这里只列出了部分选项,帮助文档请使用man mail查看
常用例子如下:
- 给多个人发邮件:邮件列表使用“,”分割
echo “邮件正文” | mail -s 邮件主题 test@126.com,test2@126.com
- 指定发件人:加-aFrom:xxx@yyy.com
echo “邮件正文” | mail -s 邮件主题 test@126.com -aFrom: test2@126.com
- 给本机其它用户发送邮件:使用username或username@System mail name
echo "This is the message body" | mail -s "hello, this is a test!" username
echo "This is the message body" | mail -s "hello, this is a test!" username@ubuntu
//这两种方法等价,第二种中的ubuntu是当前系统的主机名(hostname),见安装部分的最后一张图,系统邮件名默认的是主机名,也是ubuntu
- 发送附件:使用-A file
echo "This is the message body" | mail -s "subject" user@qq.com -A file
//发送两个附件
echo "This is the message body" | mail -s "subject" user@qq.com -A file -A file2
- 查看邮件:直接mail命令
在第三行显示有7封邮件,其中3封未读。
第二列是邮件编号,最后一列是邮件主题,中间是收件日期。
最后一行?
提示符表示等待输入命令。
输入邮件编号,回车后就可以打开该邮件进行阅读。
输入z
回车后退后邮件列表。
输入q
回车后退出
参考:
更多推荐
所有评论(0)