mail 发送邮件

(1)

直接使用shell当编辑器

# mail -s "Hello from linuxde.net by shell" admin@linuxde.net 
hello,this is the content of mail.
 welcome to www.linuxde.net

第一行是输入的命令,-s表示邮件的主题,后面的admin@linuxde.net则是邮件的接收人,输入完这行命令后回车,会进入邮件正文的编写,我们可以输入任何文字,比如上面的两行。当邮件正文输入完成后,需要按CTRL+D结束输入,此时会提示你输入Cc地址,即邮件抄送地址,没有直接回车就完成了邮件的发送。

使用管道进行邮件发送

echo "hello,this is the content of mail.welcome to www.linuxde.net" | mail -s "Hello from linuxde.net by pipe" admin@linuxde.net

使用管道直接敲入这行命令即可完成邮件的发送,其中echo后的是邮件正文。

 

使用文件进行邮件发送

mail -s "Hello from linuxde.net by file" admin@linuxde.net < mail.txt

使用上面的命令后,我们就可以把mail.txt文件的内容作为邮件的内容发送给admin@linuxde.net了。

使用上述三种方式都可以给外部邮箱进行邮件发送,但因为前面2中都是直接在shell中敲入邮件内容,因此无法输入中文,即使我们使用粘贴的方式输入了中文,那么收到的邮件也是乱码的。但第3种方式,我们可以在window下编辑好邮件内容后,放到linux下,再进行发送,这样就可以正常发送中文了。不过目前邮件的中文标题暂时没有找到解决办法。

 

因为mail程序本身就是调用sendmail来进行邮件发送的,因此我们可以在mail命令中使用sendmail的参数进行配置,比如我想使用特定的发件人发送邮件,可以使用如下命令:

mail -s "Hello from linuxde.net with sender" admin@linuxde.net -- -f user@linuxde.net < mail.txt
 
使用-- -f user@linuxde.net,能指定邮件的发件人是 “user@linuxde.net”。
上面的命令中,我们使用了–- -f user@linuxde.net这样的参数,这是sendmail的选项,其中-f表示邮件的发送人邮件地址。

来自: http://man.linuxde.net/mail

 

posted @ 2017-06-02 17:47  Big_Foot  阅读(322)  评论(0编辑  收藏  举报