linux配置外部邮箱发送邮件

前言


前几天配置了一下centos系统的邮件发送。下面总结的是linux配置外部邮箱发送邮件的操作。
(以centos系统为例,主要是注意配置文件内容即可)



centos邮件发送配置


由于配置的外部邮箱发送邮件,依赖第三方的邮件服务器发送邮件,需要第三方的邮箱的账号,密码,客户端授权等。
配置简易,通常用于监控或其他通知邮件,不受云服务器对某些邮件端口的限制(例如阿里云的25),需注意于第三方邮件服务器的网络连通。



【1】安装mailx,sendmail


yum -y install mailx sendmail


【2】第三方邮箱配置


以163邮箱为例,其他邮箱配置smtp可从网上查询



j进入邮箱,开启smtp配置
linux配置外部邮箱发送邮件_第1张图片


客户端开启授权,并设置密码
linux配置外部邮箱发送邮件_第2张图片



【3】mail.rc配置


注意开通第三方邮件的smtp及客户端授权码


cat /etc/mail.rc 

# This is the configuration file for Heirloom mailx (formerly
# known under the name "nail".
# See mailx(1) for further options.
# This file is not overwritten when 'make install' is run in
# the mailx build process again.

# Sccsid @(#)nail.rc	2.11 (gritter) 8/2/08

# Do not forward to mbox by default since this is likely to be
# irritating for most users today.
set hold

# Append rather than prepend when writing to mbox automatically.
# This has no effect unless 'hold' is unset again.
set append

# Ask for a message subject.
set ask

# Assume a CRT-like terminal and invoke a pager.
set crt

# Messages may be terminated by a dot.
set dot

# Do not remove empty mail folders in the spool directory.
# This may be relevant for privacy since other users could
# otherwise create them with different permissions.
set keep

# Do not remove empty private mail folders.
set emptybox

# Quote the original message in replies by "> " as usual on the Internet.
set indentprefix="> "

# Automatically quote the text of the message that is responded to.
set quote

# Outgoing messages are sent in ISO-8859-1 if all their characters are
# representable in it, otherwise in UTF-8.
set sendcharsets=iso-8859-1,utf-8

# Display sender's real names in header summaries.
set showname

# Display the recipients of messages sent by the user himself in
# header summaries.
set showto

# Automatically check for new messages at each prompt, but avoid polling
# of IMAP servers or maildir folders.
set newmail=nopoll

# If threaded mode is activated, automatically collapse thread.
set autocollapse

# Mark messages that have been answered.
set markanswered

# Hide some header fields which are uninteresting for most human readers.
ignore received in-reply-to message-id references
ignore mime-version content-transfer-encoding

# Only include selected header fields when forwarding messages.
fwdretain subject date from to

# For Linux and BSD, this should be set.
set bsdcompat

#开启ssl
set ssl-verify=ignore
#下方输入证书目录,下方为centos系统证书默认位置,也自行生成证书并指定
set nss-config-dir=/etc/pki/nssdb

# 下方填入你配置的第三方smtp服务器的地址及端口,如果使用的是云服务器,安全组需要开放465端口(入口和出口)
set smtp=smtps://smtp.163.com:465                          
# 认证方式
set smtp-auth=login                            
# 下方输入用于发送邮件的邮箱账号
set [email protected]     
# 下方输入上方邮箱的客户端授权码
set smtp-auth-password=woeSEvrub213               
# 设置发信人邮箱和昵称
set [email protected]                       


【4】邮件发送


基本发送格式:

echo “邮件正文” | mail -s “邮件标题” 邮件接收人邮箱
or
mail -s “邮件标题” 邮件接收人邮箱 < mail.txt

(mail.txt文件中为邮件内容)

你可能感兴趣的:(Linux中的各类服务,运维日常的FAQ,linux配置外部邮箱发送邮件)