Linux如何向qq发送邮件并携带附件

1.开启qq邮箱的SMTP,并获取授权码,如下所示:

1.进入qq邮箱的设置
在这里插入图片描述
2.进入账户在这里插入图片描述
3.开启SMTP服务,并且获取到授权码
Linux如何向qq发送邮件并携带附件_第1张图片

2.关闭Linux机器的sendmail服务,开启postfix服务

	[root@hadoop001 ~]# service sendmail stop
	[root@hadoop001 ~]# service postfix start
		
	[root@hadoop001 ~]# chkconfig --list

显示如下:
在这里插入图片描述
在这里插入图片描述

3.创建证书文件

	[root@hadoop001 ~]# mkdir -p /root/.certs/
	[root@hadoop001 ~]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
	[root@hadoop001 ~]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
	[root@hadoop001 ~]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
	[root@hadoop001 ~]# certutil -L -d /root/.certs
	[root@hadoop001 ~]# cd /root/.certs
	[root@hadoop001 ~]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu"  -d ./ -i qq.crt

4.修改/etc/mail.rc文件

	[root@hadoop001 ~]# vi /etc/mail.rc
	添加如下内容
	set [email protected]						#你的qq邮箱
	set smtp=smtp.qq.com
	set smtp-auth-user=xxxxxxxx					#你的qq
	#授权码
	set smtp-auth-password=xxxxxxx				#授权码
	set smtp-auth=login
	set smtp-use-starttls
	set ssl-verify=ignore
	set nss-config-dir=/root/.certs

5.测试是否成功

	[root@hadoop001 ~]# echo  "hello word" | mail -s "title"  [email protected]

此时去自己的qq邮箱查看
在这里插入图片描述
成功。

6.发送邮件带附件

	[root@hadoop001 data]# vi test.log
	test1
	test2
	test3

	[root@hadoop001 data]# vi attachment.sh
	#!/bin/bash


	FROM_EMAIL="[email protected]"			#你的qq
	TO_EMAIL="[email protected]"			#你的qq
	
	LOG=/root/data/test.log
	
	echo -e "`date "+%Y-%m-%d %H:%M:%S"` : Please to check the test attachement." | mailx \
	-r "From: Admin <${FROM_EMAIL}>" \
	-a ${LOG} \
	-s "Critical:DSHS test log." ${TO_EMAIL}
	
	执行该脚本
	[root@hadoop001 data]# sh attachment.sh

用到的命令是 mailx -a (添加附件) mailx -r(设置发件人地址) mail -s(指定主题)
mailx更多参数:http://linux.51yip.com/search/mailx
执行完毕后去自己的qq邮箱:
在这里插入图片描述
Linux如何向qq发送邮件并携带附件_第2张图片
Linux如何向qq发送邮件并携带附件_第3张图片

你可能感兴趣的:(Linux)