# mail

#### Installation

常用的寄送 mail 指令套件：

1. mailutils: 支援 HTML 格式的郵件，不支援外部 SMTP 連線
2. Heirloom-mailx: RHEL 7/8 內建指令，不支援 HTML 格式的郵件，有支援連線外部 SMTP server。
3. BSD-mailx: 支援 HTML 格式的郵件，不支援外部 SMTP 連線
4. Mutt: 支援 HTML 格式的郵件
5. Sendmail: 支援 HTML 格式的郵件，不支援外部 SMTP 連線

```shell
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

```shell
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  


<p class="callout warning">NOTE: RHEL 7+ 的 mailx 指令無法寄送 HTML 格式的郵件；改用 mutt 或 sendmail.postfix。</p>

##### mailx (舊版)

```shell
echo "<b>HTML Message goes here</b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" receiver@domain_name
```

##### sendmail for Postfix

> NOTE: 如果系統使用 postfix 作為 MTA 時，指令改成 `sendmail.postfix`。

檢查 sendmail 的版本

```
┌──[root@tpedevdb02:/worktmp/my_shell]
└─# which sendmail
/usr/sbin/sendmail

┌──[root@tpedevdb02:/worktmp/my_shell]
└─# readlink /usr/sbin/sendmail
/etc/alternatives/mta

┌──[root@tpedevdb02:/worktmp/my_shell]
└─# update-alternatives --display mta
mta - status is auto.
 link currently points to /usr/sbin/sendmail.postfix
/usr/sbin/sendmail.postfix - priority 30
 slave mta-mailq: /usr/bin/mailq.postfix
 slave mta-newaliases: /usr/bin/newaliases.postfix
 slave mta-pam: /etc/pam.d/smtp.postfix
 slave mta-rmail: /usr/bin/rmail.postfix
 slave mta-sendmail: /usr/lib/sendmail.postfix
 slave mta-mailqman: /usr/share/man/man1/mailq.postfix.1.gz
 slave mta-newaliasesman: /usr/share/man/man1/newaliases.postfix.1.gz
 slave mta-sendmailman: /usr/share/man/man1/sendmail.postfix.1.gz
 slave mta-aliasesman: /usr/share/man/man5/aliases.postfix.5.gz
Current `best' version is /usr/sbin/sendmail.postfix.
```

Sample #1

```bash
(
    echo "To: user@example.com"
    echo "Subject: HTML test"
    echo "Content-Type: text/html"
    echo
    cat file.html
) | sendmail.postfix -t

```

Sample #2

```bash
echo "FROM: info@mediasystemsfl.com
To: $email
Subject: $subject
Content-Type: text/html; charset=utf-8


<html><head>
<title>$subject</title>
<body>

### 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%. 


</body>
</html>
" | /usr/sbin/sendmail -t
```

##### mutt on RHEL 7

```bash
echo "<b>HTML Message goes here</b>" | mutt -s "Test HTML Mail" -e "my_hdr Content-Type: text/html" user@my.com
```

#### Functions

```bash

# 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

<p class="callout info">RHEL 7/8 內建的 Heirloom-mailx 支援不需要使用本地的 MTA 服務就可以外寄郵件。</p>

一般沒有認證與加密的 SMTP

```bash
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)"
```

</body></html>