Skip to main content

mail

Installation

常用的寄送 mail 套件:

    mailutils: 支援 HTML 格式的郵件,不支援外部 SMTP 連線 Heirloom-mailx: RHEL 7/8 內建指令,不支援 HTML 格式的郵件,有支援連線外部 SMTP server。 BSD-mailx: 支援 HTML 格式的郵件,不支援外部 SMTP 連線 Mutt: 支援 HTML 格式的郵件 Sendmail: 支援 HTML 格式的郵件,不支援外部 SMTP 連線
    sudo apt install mailutils        # [On Debian, Ubuntu and Mint]
    sudo yum install mailx            # [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
    sudo emerge -a mail-client/mailx  # [On Gentoo Linux]
    sudo pacman -S mailutils          # [On Arch Linux]
    sudo zypper install mailutils     # [On OpenSUSE]  

    Alternative mailx

    Debian 系統預設 mailx 為 heirloom-mailx,因為無法寄送 HTML Mail 格式,想改成 mailutils。

    root@A2B:~/bin# dpkg --get-selections | grep mail
    bsd-mailx					deinstall
    heirloom-mailx					install
    libmail-sendmail-perl				install
    libmailtools-perl				install
    libmailutils4					install
    mailutils					install
    mailutils-common				install
    
    root@A2B:~/bin# update-alternatives --display mailx
    mailx - auto mode
      link currently points to /usr/bin/heirloom-mailx
    /usr/bin/heirloom-mailx - priority 60
      slave Mail: /usr/bin/heirloom-mailx
      slave Mail.1.gz: /usr/share/man/man1/heirloom-mailx.1.gz
      slave mail: /usr/bin/heirloom-mailx
      slave mail.1.gz: /usr/share/man/man1/heirloom-mailx.1.gz
      slave mailx.1.gz: /usr/share/man/man1/heirloom-mailx.1.gz
    /usr/bin/mail.mailutils - priority 30
      slave mail: /usr/bin/mail.mailutils
      slave mail.1.gz: /usr/share/man/man1/mail.mailutils.1.gz
      slave mailx.1.gz: /usr/share/man/man1/mail.mailutils.1.gz
    Current 'best' version is '/usr/bin/heirloom-mailx'.
    
    root@A2B:~/bin# update-alternatives --config mailx
    There are 2 choices for the alternative mailx (providing /usr/bin/mailx).
    
      Selection    Path                     Priority   Status
    ------------------------------------------------------------
    * 0            /usr/bin/heirloom-mailx   60        auto mode
      1            /usr/bin/heirloom-mailx   60        manual mode
      2            /usr/bin/mail.mailutils   30        manual mode
    
    Press enter to keep the current choice[*], or type selection number: 2
    update-alternatives: using /usr/bin/mail.mailutils to provide /usr/bin/mailx (mailx) in manual mode

    驗證套件版本

    root@A2B:~/bin# update-alternatives --display mailx
    mailx - manual mode
      link currently points to /usr/bin/mail.mailutils
    /usr/bin/heirloom-mailx - priority 60
      slave Mail: /usr/bin/heirloom-mailx
      slave Mail.1.gz: /usr/share/man/man1/heirloom-mailx.1.gz
      slave mail: /usr/bin/heirloom-mailx
      slave mail.1.gz: /usr/share/man/man1/heirloom-mailx.1.gz
      slave mailx.1.gz: /usr/share/man/man1/heirloom-mailx.1.gz
    /usr/bin/mail.mailutils - priority 30
      slave mail: /usr/bin/mail.mailutils
      slave mail.1.gz: /usr/share/man/man1/mail.mailutils.1.gz
      slave mailx.1.gz: /usr/share/man/man1/mail.mailutils.1.gz
    Current 'best' version is '/usr/bin/heirloom-mailx'.
    
    root@A2B:~/bin# mail --version
    mail (GNU Mailutils) 2.99.97
    Copyright (C) 2010 Free Software Foundation, inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    Text Mail

    echo "MAIL BODY" | mail -s 'MAIL SUBJECT' receiver@domain_name
    
    # Enter DOT or Ctrl-d to exit the CLI
    mail -s "Test mail" receiver@domain_name

    HTML Mail

    NOTE: RHEL 7+ 的 mailx 指令無法寄送 HTML 格式的郵件。

    mailx (舊版)
    echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" receiver@domain_name
    sendmail
    echo "FROM: info@mediasystemsfl.com
    To: $email
    Subject: $subject
    Content-Type: text/html; charset=utf-8
    
    <!DOCTYPE html>
    <html><head>
    <title>$subject</title>
    <body>
    <pre>
    ### Attention: Do not reply to this message. ###
    
    $(Show_head)
    
    $(Gen_asr_report)
    
    NOTE: The symbol '<<<' indicates an ASR(Answer-Seizure Ratio) of less than $thre%. 
    
    </pre>
    </body>
    </html>
    " | /usr/sbin/sendmail -t
    mutt on RHEL 7
    echo "<b>HTML Message goes here</b>" | mutt -s "Test HTML Mail" -e "my_hdr Content-Type: text/html" user@my.com

    Functions

    
    # Show_head & Gen_asr_report are custom functions.
    Send_mail() {
        subject="This is Mail Subject"
        email="user@email.com"
        echo -e "
    ### Attention: Do not reply to this message. ###
    
    $(Show_head)
    
    $(Gen_asr_report)
    
    NOTE: The symbol '<<<' indicates an ASR(answer-seizure ratio) of less than $thre%.
        " | /usr/bin/mail -s "$subject" "$email"
    }

    Via SMTP Server

    RHEL 7/8 內建的 Heirloom-mailx 支援不需要使用本地的 MTA 服務就可以外寄郵件。

    一般沒有認證與加密的 SMTP

    mail -v -S smtp=smtp://192.168.21.7:25 -s "Test SMTP" user@domain.tld

    認證且有加密的 SMTP

    新增 ~/.mailrc 或全域環境 /etc/mail.rc (以 Gmail 為例)

    set smtp-use-starttls
    set ssl-verify=ignore
    set smtp=smtp://smtp.gmail.com:587
    set smtp-auth=login
    set smtp-auth-user=$FROM_EMAIL_ADDRESS
    set smtp-auth-password=$EMAIL_ACCOUNT_PASSWORD
    set from="$FROM_EMAIL_ADDRESS($FRIENDLY_NAME)"