linux的mail,Linux 使用 mail/mailx 发送邮件

在Linux服务器维护的时候,发送邮件有时很有必要,当检测到服务、程序有问题时,可以及时发送邮件通知您处理,当然可以自己写个程序来发送,mailx已经很强大。

安装 mailx

# ubuntu/debian

$ sudo apt-get install heirloom-mailx

# fedora/centos

$ sudo yum install mailx

1. 简单的邮件

$ mail -s "This is the subject" someone@example.com

Hi someone

How are you

I am fine

Bye

EOT

输入第一行命令,回车之后就可以输入邮件的内容了,完成输入内容后按 CTRL+D ,mailx 就会显示出 EOT。

2. 使用文件中的取邮件内容

$ mail -s "This is Subject" someone@example.com < /path/to/file

或者可以使用 echo

$ echo "This is message body" | mail -s "This is Subject" someone@example.com

3. 多个收件人

多个收件人,收件地址只需要用逗号“,”隔开即可。

$ echo "This is message body" | mail -s "This is Subject" someone@example.com,someone2@example.com

4. 抄送和密送CC,BCC

用 -c 表示抄送,-b 表示密送

$ echo "This is message body" | mail -s "This is Subject" -c ccuser@example.com someone@example.com

5. 定义发件人信息

使用 -r 定义发件人

$ echo "This is message body" | mail -s "This is Subject" -r "Harry" someone@example.com

6. 使用特殊的回复地址

# 回复地址

$ echo "This is message" | mail -s "Testing replyto" -S replyto="mark@gmail.com" someone@example.com

# 回复地址和名称

$ echo "This is message" | mail -s "Testing replyto" -S replyto="Mark" someone@example.com

7. 发送附件

使用 "-a" 加上文件路径即可

$ echo "This is message body" | mail -s "This is Subject" -r "Harry" -a /path/to/file someone@example.com

8. 使用外部的SMTP服务器

使用外部的SMTP服务器,这样就可以不用自己架设邮件服务器了。

$ echo "This is the message body and contains the message" | mailx -v -r "someone@example.com" -s "This is the subject" -S smtp="mail.example.com:587" -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="someone@example.com" -S smtp-auth-password="abc123" -S ssl-verify=ignore yourfriend@gmail.com

或者使用换行的形式,可读性更强

$ echo "This is the message body and contains the message" | mailx -v \

> -r "someone@example.com" \

> -s "This is the subject" \

> -S smtp="mail.example.com:587" \

> -S smtp-use-starttls \

> -S smtp-auth=login \

> -S smtp-auth-user="someone@example.com" \

> -S smtp-auth-password="abc123" \

> -S ssl-verify=ignore \

> yourfriend@gmail.com

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值