脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - shell - Shell脚本实现自动发送邮件的例子

Shell脚本实现自动发送邮件的例子

2023-02-17 11:16shell教程网 shell

这篇文章主要介绍了Shell脚本实现自动发送邮件的例子,使用.muttrc文件配合shell脚本实现,需要的朋友可以参考下

1、编辑用户Home目录下的.muttrc文件,设置发信环境。

复制代码 代码如下:

# cat /root/.muttrc
set envelope_from=yes
set from=owinux@sina.cn
set realname="Owinux"
set use_from=yes
set rfc2047_parameters=yes
set charset="utf-8"

 

2、发信脚本

复制代码 代码如下:

# cat automail.sh
#!/bin/sh
# automail.sh
DIR=/root/owinux
mailcontent=$DIR/mailcontent
> "$mailcontent"
echo -e "owinux,您好!\n" >> $mailcontent
echo -e "\t附件为 `date +%Y-%m-%d` 设备日检报告,敬请查收。" >> $mailcontent
cat mailcontent | /usr/bin/mutt -s "设备日检报告" -a /root/owinux/report.xls owinux@126.com -c owinux@yeah.net -c owinux@sina.com

 

3、简要说明:
 
-s:指定主题
-a:附件
-c:抄送,需要抄送多人的话,须使用多个 -c 选项
 
使用上面的发信环境可以保证在页面查看邮件的时候,附件名不出现乱码。
其中 .muttrc 的 "set from"可以随便设置,可以是不存在的邮箱地址,当然正确的最好。
比如:

复制代码 代码如下:

set from=abc@owinux.com

 

不过也并不是你设置的正确的邮箱地址就一定能发送成功。

复制代码 代码如下:

set from=owinux@126.com

 

我新注册了一个 owinux@126.com,然后将 set from 设置为owinux@126.com,就没有成功。
原因在于126邮箱报了 550 MI:SPF 错误,连接到126 企业退信的常见问题? 发现

复制代码 代码如下:

550 MI:SPF 发信IP未被发送域的SPF许可。 

 

Shell脚本实现自动发送邮件的例子

延伸 · 阅读

精彩推荐