LINUX 发送邮件(例子qq邮件)

本次测试 使用云主机 ,配置 liunx发送邮件至qq邮箱中

  • 前往QQ邮箱,在设置-账户-下开启SMTP和获取授权码
    在这里插入图片描述
  • 安装mailx
[root@JD .certs]# yum -y install mailx
[root@JD learn_shell]# which mail
/usr/bin/mail
  • 停⽌服务:
    service sendmail stop
    chkconfig sendmail off
[root@JD ~]# service sendmail stop
Redirecting to /bin/systemctl stop  sendmail.service
Failed to stop sendmail.service: Unit sendmail.service not loaded.
[root@JD ~]# chkconfig sendmail off
error reading information on service sendmail: No such file or directory

  • 启动postfix服务:
    service postfix start
    chkconfig postfix on
    postfix check
    systemctl status postfix
[root@JD ~]# service postfix start
Redirecting to /bin/systemctl start  postfix.service
[root@JD ~]# chkconfig postfix on
Note: Forwarding request to 'systemctl enable postfix.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/postfix.service to /usr/lib/systemd/system/postfix.service.
[root@JD ~]# postfix check
[root@JD ~]# systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-08-23 11:07:54 CST; 31s ago
 Main PID: 2145 (master)
   CGroup: /system.slice/postfix.service
           ├─2145 /usr/libexec/postfix/master -w
           ├─2147 pickup -l -t unix -u
           └─2148 qmgr -l -t unix -u

Aug 23 11:07:52 JD systemd[1]: Starting Postfix Mail Transport Agent...
Aug 23 11:07:53 JD postfix/postfix-script[2143]: starting the Postfix mail system
Aug 23 11:07:54 JD postfix/master[2145]: daemon started -- version 2.10.1, configuration /etc/postfix
Aug 23 11:07:54 JD systemd[1]: Started Postfix Mail Transport Agent.
  • 调整参数:
[root@JD ~]# vi /etc/postfix/main.cf
inet_interfaces = all

在这里插入图片描述

  • 生成证书
# 创建证书目录
[root@JD ~]# mkdir -p /root/.certs/
[root@JD ~]# cd .certs/
[root@JD .certs]# ll
total 0
#获取证书内容
[root@JD .certs]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
depth=2 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA
verify return:1
depth=1 C = US, O = DigiCert Inc, OU = www.digicert.com, CN = GeoTrust RSA CA 2018
verify return:1
depth=0 C = CN, ST = Guangdong, L = Shenzhen, O = Tencent Technology (Shenzhen) Company Limited, OU = R&D, CN = pop.qq.com
verify return:1
DONE
[root@JD .certs]# ll
total 4
-rw-r--r-- 1 root root 2529 Aug 23 15:46 qq.crt
# 添加证书到数据库
[root@JD .certs]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@JD .certs]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@JD .certs]# ll
total 80
-rw------- 1 root root 65536 Aug 23 15:47 cert8.db
-rw------- 1 root root 16384 Aug 23 15:47 key3.db
-rw-r--r-- 1 root root  2529 Aug 23 15:46 qq.crt
-rw------- 1 root root 16384 Aug 23 15:47 secmod.db
# 列出指定目录下的证书
[root@JD .certs]# certutil -L -d /root/.certs

Certificate Nickname                                         Trust Attributes
                                                             SSL,S/MIME,JAR/XPI

GeoTrust SSL CA                                              C,,  
# 标记新人证书、防报错
[root@JD .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
Notice: Trust flag u is set automatically if the private key is present.

  • 编辑mail的配置文件
set [email protected]     	#发送邮件后显示的邮件发送方
set smtp=smtps://smtp.qq.com:465     #qq smtp邮件服务器
set [email protected]  # 你的qq邮箱
#授权码
set smtp-auth-password=lmpzzmmxxxxxx  # 你的qq邮箱密码(设置页面加密后的授权码)
set smtp-auth=login        # 动作、登录
set ssl-verify=ignore          # 忽略ssl验证
set nss-config-dir=/root/.certs      # 证书路径

  • 测试
#不带附件的
[root@JD .certs]# echo "来自liunx的第二封邮件" | mail -s " title" [email protected]

在这里插入图片描述

#带附件发送邮件到QQ邮箱
[root@JD learn_shell]#  echo "发送带一个附件过来" |mail -s "来自linux的邮件" -r "from:LIUNX1 <[email protected]>" -a /root/learn_shell/awk.log [email protected]

在这里插入图片描述

发布了33 篇原创文章 · 获赞 1 · 访问量 2589

猜你喜欢

转载自blog.csdn.net/weixin_44131414/article/details/100038845