(原)邮件告警发送服务基础环境搭建笔记

 

环境:ubuntu20.04 容器

邮件发送使用s-nail

配置文件为/etc/s-nail.rc

环境搭建步骤:

  1. 安装s-nail邮件服务

    sudo apt install s-nail
  2. 打开163邮箱,进入设置,开启IMAP/stmp,POP3/stmp服务,并获取邮箱的授权码:xxxx

  3. 因为需要 163邮箱的 SSL 证书,所以我们还需要手动的获取163邮箱的证书保存到本地指定的目录里以备调用和验证,具体命令如下:


    apt install -y certutil
    apt-get install libnss3-tools

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

    为了防止出现前文所说的发送邮件警告提示,还需要进入邮箱 SSL 证书存放目录 /root/.certs 里执行如下命令:


    cd /root/.certs/
    certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i 163.crt
  4. 编写s-nail.rc配置文件:

    使用vi /etc/s-nail.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

    set nss-config-dir=/root/.certs/  
    set from=lihaiping1603@163.com
    set smtp=smtps://smtp.163.com
    set ssl-verify=ignore
    set smtp-auth=login
    set smtp-auth-user=lihaiping1603@163.com
    set smtp-auth-password=xxx

    配置文件的具体内容如上述。其中对几个主要参数做进一步的介绍:

    from:对方收到邮件时显示的发件人
    smtp:指定第三方发邮件的smtp服务器地址 
    set smtp-auth-user:第三方发邮件的用户名 
    set smtp-auth-password:用户名对应的密码,有些邮箱填的是授权码 
    smtp-auth:SMTP的认证方式,默认是login,也可以改成CRAM-MD5或PLAIN方式 
    nss-config-dir:对应的ssl证书生成目录,我们可以通过find / -name "cert*.db"方式查找到相应的目录。
  5. 测试


    root@e2579822f3cc:/home/lhp/xx# echo "body" | s-nail -v -s "test" xxxx@qq.com
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp
    s-nail: Obsoletion warning: variable superseded or obsoleted: ssl-verify
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-user
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-password
    s-nail: No such file to load: /root/.mailrc
    s-nail: Warning -- v15-compat=yes will be default in v14.10.0!
    s-nail: P(seudo)R(andom)N(umber)G(enerator): *TLS RAND_*
    s-nail: Obsoletion warning: Use of old-style credentials, which will vanish in v15!
    s-nail:   Please read the manual section "On URL syntax and credential lookup"
    s-nail: Obsoletion warning: please don't use *smtp*: assign a smtp:// URL to *mta*!
    s-nail: Resolving host smtp.163.com:smtps ... done
    s-nail: Connecting to 220.181.12.18:smtps ... connected.
    s-nail: TLS connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
    root@e2579822f3cc:/home/lhp/xx#
    root@e2579822f3cc:/home/lhp# cat /tmp/tmp_mail.txt | s-nail -v -Sttycharset=utf8 -s "pipe" lihaiping1@chinatelecom.cn
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp
    s-nail: Obsoletion warning: variable superseded or obsoleted: ssl-verify
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-user
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-password
    s-nail: Field already ignored: received
    s-nail: Field already ignored: in-reply-to
    s-nail: Field already ignored: message-id
    s-nail: Field already ignored: references
    s-nail: Field already ignored: mime-version
    s-nail: Field already ignored: content-transfer-encoding
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp
    s-nail: Obsoletion warning: variable superseded or obsoleted: ssl-verify
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-user
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-password
    s-nail: Warning -- v15-compat=yes will be default in v14.10.0!
    s-nail: P(seudo)R(andom)N(umber)G(enerator): *TLS RAND_*
    s-nail: Obsoletion warning: Use of old-style credentials, which will vanish in v15!
    s-nail:   Please read the manual section "On URL syntax and credential lookup"
    s-nail: Obsoletion warning: please don't use *smtp*: assign a smtp:// URL to *mta*!
    s-nail: Resolving host smtp.163.com:smtps ... done
    s-nail: Connecting to 220.181.12.14:smtps ... connected.
    s-nail: TLS connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
    root@e2579822f3cc:/home/lhp#
    root@e2579822f3cc:/home/lhp# s-nail -v -Sttycharset=utf8 -s "a" -a /tmp/tmp_mail.txt lihaiping1@chinatelecom.cn < /tmp/tmp_mail.txt
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp
    s-nail: Obsoletion warning: variable superseded or obsoleted: ssl-verify
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-user
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-password
    s-nail: Field already ignored: received
    s-nail: Field already ignored: in-reply-to
    s-nail: Field already ignored: message-id
    s-nail: Field already ignored: references
    s-nail: Field already ignored: mime-version
    s-nail: Field already ignored: content-transfer-encoding
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp
    s-nail: Obsoletion warning: variable superseded or obsoleted: ssl-verify
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-user
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-password
    s-nail: Warning -- v15-compat=yes will be default in v14.10.0!
    s-nail: P(seudo)R(andom)N(umber)G(enerator): *TLS RAND_*
    s-nail: Obsoletion warning: Use of old-style credentials, which will vanish in v15!
    s-nail:   Please read the manual section "On URL syntax and credential lookup"
    s-nail: Obsoletion warning: please don't use *smtp*: assign a smtp:// URL to *mta*!
    s-nail: Resolving host smtp.163.com:smtps ... done
    s-nail: Connecting to 220.181.12.18:smtps ... connected.
    s-nail: TLS connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
    root@e2579822f3cc:/home/lhp#
    root@e2579822f3cc:/home/lhp# s-nail -v -Sttycharset=utf8 -s "this is test" lihaiping1@chinatelecom.cn < /tmp/tmp_mail.txt
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp
    s-nail: Obsoletion warning: variable superseded or obsoleted: ssl-verify
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-user
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-password
    s-nail: Field already ignored: received
    s-nail: Field already ignored: in-reply-to
    s-nail: Field already ignored: message-id
    s-nail: Field already ignored: references
    s-nail: Field already ignored: mime-version
    s-nail: Field already ignored: content-transfer-encoding
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp
    s-nail: Obsoletion warning: variable superseded or obsoleted: ssl-verify
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-user
    s-nail: Obsoletion warning: variable superseded or obsoleted: smtp-auth-password
    s-nail: Warning -- v15-compat=yes will be default in v14.10.0!
    s-nail: P(seudo)R(andom)N(umber)G(enerator): *TLS RAND_*
    s-nail: Obsoletion warning: Use of old-style credentials, which will vanish in v15!
    s-nail:   Please read the manual section "On URL syntax and credential lookup"
    s-nail: Obsoletion warning: please don't use *smtp*: assign a smtp:// URL to *mta*!
    s-nail: Resolving host smtp.163.com:smtps ... done
    s-nail: Connecting to 220.181.12.16:smtps ... connected.
    s-nail: TLS connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
    root@e2579822f3cc:/home/lhp#
    root@e2579822f3cc:/home/lhp# s-nail -h
    s-nail (s-nail v14.9.15): send and receive Internet mail

    Send-only mode: send mail "to-addr"(ess) receiver(s):
    s-nail [-DdEFinv~#] [-: spec] [-A account] [:-C "field: body":]
            [:-a attachment:] [:-b bcc-addr:] [:-c cc-addr:]
            [-M type | -m file | -q file | -t] [-r from-addr] [:-S var[=value]:]
            [-s subject] [-T "arget: addr"] [:-X/Y cmd:] [-.] :to-addr:

    "Receive" mode, starting on [-u user], primary *inbox* or [$MAIL]:
    s-nail [-DdEeHiNnRv~#] [-: spec] [-A account] [:-C "field: body":]
            [-L spec] [-r from-addr] [:-S var[=value]:] [-u user] [:-X/Y cmd:]

    "Receive" mode, starting on -f (secondary $MBOX or [file]):
    s-nail [-DdEeHiNnRv~#] [-: spec] [-A account] [:-C "field: body":] -f
            [-L spec] [-r from-addr] [:-S var[=value]:] [:-X/Y cmd:] [file]

    -d sandbox, -:/ no .rc files, -. end options and force send-mode
    -a attachment[=input-charset[#output-charset]]
    -[bcrT], to-addr: ex@am.ple or '(Lovely) Ex <am@p.le>'
    -[Mmqt]: special input data (-t: template message on stdin)
    -e only mail check, -H header summary; both: message specification via -L
    -S (un)sets variable, -X/-Y execute commands pre/post startup, -#: batch mode
    Features via "$ s-nail -Xversion -Xx"; there is --long-help
    Bugs/Contact via "$ s-nail -Sexpandaddr=shquote '\$contact-mail'"

    这里对s-nail的常用命令参数进行简单的介绍,详细的可以参考:http://manpages.ubuntu.com/manpages/bionic/man1/s-nail.1.html

    -a file,可以为邮件添加附件文件file

    -M xxxx, 为mime指定type

    -m file, 这个没太搞懂和-q的区别,难道是这种会为文件的内容做拓展?然后文件的内容也会作为邮件的消息内容

    -q file,将文件的内容指定为邮件的消息体

    -S var=value,设置内部可变变量的值

    -s subject,设置邮件的标题

    -v 打印消息

posted @ 2022-01-18 14:36  lihaiping  阅读(966)  评论(0编辑  收藏  举报