linux邮件发送命令 linux 发送邮件到指定邮箱( 四 )


set from = "@gmail.com"set realname = "Dan Nanni"set smtp_url = "smtp://@smtp.gmail.com:587/"set smtp_pass = ""
一切就绪 , 使用 mutt 发送一封邮件:
$ echo "This is an email body." | mutt -s "This is an email subject" alice@yahoo.com
想在一封邮件中添加附件 , 使用 "-a" 选项
$ echo "This is an email body." | mutt -s "This is an email subject" alice@yahoo.com -a ~/test_attachment.jpg
使用 Gmail SMTP 服务器意味着邮件将显示是从你 Gmail 账户发出的 。换句话说,收件人将视你的 Gmail 地址为发件人地址 。如果你想要使用自己的域名作为邮件发送方,你需要使用 Gmail SMTP 转发服务 。
当服务器重启时发送邮件通知
如果你在 虚拟专用服务器(VPS)
上跑了些重要的网站,建议监控 VPS 的重启行为 。作为一个更为实用的例子 , 让我们研究如何在你的 VPS
上为每一次重启事件建立邮件通知 。这里假设你的 VPS 上使用的是 systemd,并向你展示如何为自动邮件通知创建一个自定义的 systemd
启动服务 。
首先创建下面的脚本 reboot_notify.sh,用于负责邮件通知 。
$ sudo vi /usr/local/bin/reboot_notify.sh
#!/bin/sh
echo "`hostname` was rebooted on `date`" | mutt -F /etc/muttrc -s "Notification on `hostname`" alice@yahoo.com
$ sudo chmod +x /usr/local/bin/reboot_notify.sh
在这个脚本中,我使用 "-F" 选项,用于指定系统级的 mutt 配置文件位置 。因此不要忘了创建 /etc/muttrc 文件,并如前面描述的那样填入 Gmail SMTP 信息 。
现在让我们创建如下一个自定义的 systemd 服务 。
$ sudo mkdir -p /usr/local/lib/systemd/system$ sudo vi /usr/local/lib/systemd/system/reboot-task.service
[Unit]
Description=Send a notification email when the server gets rebooted
DefaultDependencies=no
Before=reboot.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/reboot_notify.sh
[Install]
WantedBy=reboot.target
在创建服务后,添加并启动该服务 。
$ sudo systemctl enable reboot-task$ sudo systemctl start reboot-task
从现在起,在每次 VPS 重启时,你将会收到一封通知邮件 。
通过服务器使用监控发送邮件通知
作为最后一个例子,让我展示一个现实生活中的应用程序,Monit,这是一款极其有用的服务器监控应用程序 。它带有全面的 VPS 监控能力(比如 CPU、内存、进程、文件系统)和邮件通知功能 。
如果你想要接收 VPS 上由 Monit 产生的任何事件的邮件通知 , 你可以在 Monit 配置文件中添加以下 SMTP 信息 。
set mailserver smtp.gmail.com port 587
username "" password ""
using tlsv12
set mail-format {
from: @gmail.com
subject: $SERVICE $EVENT at $DATE on $HOST
message: Monit $ACTION $SERVICE $EVENT at $DATE on $HOST : $DESCRIPTION.
Yours sincerely,
Monit
}
# the person who will receive notification emails
set alert alice@yahoo.com
这是一个因为 CPU 负载超载而由 Monit 发送的邮件通知的例子 。
如何在Linux下使用mail命令发送邮件到外部1、首先你得开启25端口,邮件服务器可以用sendmail或postfix,因为发件人是Received: from localhost.localdomain有些邮箱可能会拒绝或当作垃圾邮件
2、发送内容
cat a.txt|mail youremail
3、发送附件
yum -y install sharutils
uuencode a.txt a.txt |mail -s ‘subject’ youremail
linux 如何在命令行下面发送邮件操作系统(无论linux还是windows)有一种“管道”的概念,可以把一个程序的标准输入/输出改到其他的地方 。
你说的那些gui外壳,就是为命令行程序创建了一个管道,标准输入和标准输出被改到了gui外壳程序的内部 。所以就不会再弹出命令行窗口了 。

推荐阅读