(原)郵件告警發送服務基礎環境搭建筆記


 

環境: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 打印消息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM