Installation
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
mailx
NOTE: Doesn't work with Notes mail client
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"
}