If you have to setup a new Linux server where you need to do a certain local running daemons monitoring with a custom scripts on the local machine Nagios / Zabbix / Graphana etc. that should notify about local running custom programs or services in case of a certain criteria is matched or you simply want your local existing UNIX accounts to be able to send outbound Emails to the Internet.
Then usually you need to install a fully functional SMTP Email server that was Sendmail or QMAIL in old times in early 21st century andusually postfix or Exim in recent days and configure it to use as as a Relay mail server some Kind of SMTP.
The common Relay smtp setting would be such as Google's smtp.gmail.com, Yahoo!'s smtp.mail.yahoo.com relay host, mail.com or External configured MTA Physical server with proper PTR / MX records or a SMTP hosted on a virtual machine living in Amazon's AWS or m$ Azure that is capable to delivere EMails to the Internet.
Configuring the local installed Mail Transport Agent (MTA) as a relay server is a relatively easy task to do but of course why should you have a fully stacked MTA service with a number of unnecessery services such as Email Queue, Local created mailboxes, Firewall rules, DNS records, SMTP Auth, DKIM keys etc. and even the ability to acccept any emails back in case if you just want to simply careless send and forget with a confirmation that remote email was send successfully?
This is often the case for some machines and especially with the inclusion of technologies such as Kubernettes / Clustered environments / VirtualMachines small proggies such as ssmtp / msmtp that could send mail without a Fully functional mail server installed on localhost ( 127.0.0.1 ) is true jams.
ssmtp program is Simple Send-only sendMail emulator has been around in Debian GNU / Linux, Ubuntu, CentOS and mostly all Linuxes for quite some a time but recently the Debian package has been orphaned so to install it on a deb based server host you need to use instead msmtp.
1. Install ssmtp on CentOS / Fedora / RHEL Linux
In RPM distributions you can't install until epel-release repository is enabled.
[root@centos:~]# yum –enablerepo=extras install epel-release
[root@centos:~]# yum install ssmtp
2. Install ssmp / msmtp Debian / Ubuntu Linux
If you run older version of Debian based distribution the package to install is ssmtp, e.g.:
root@debian:~# apt-get install –yes ssmtp
On Newer Debians as of Debian 10.0 Buster onwards install instead
root@debian:~# apt install –yes msmtp-mta
can save you a lot of effort to keep an eye on a separately MTA hanging around and running as a local service eating up resources that could be spared.
3. Configure Relay host for ssmtp
A simple configuration to make ssmtp use gmail.com SMTP servers as a relay host below:
linux:~# cat << EOF > /etc/ssmtp/ssmtp.conf
# The user that gets all the mails (UID < 1000, usually the admin)
# The full hostname. Must be correctly formed, fully qualified domain name or GMail will reject connection.
# The mail server (where the mail is sent to), both port 465 or 587 should be acceptable
# See also https://support.google.com/mail/answer/78799
# The address where the mail appears to come from for user authentication.
# Email 'From header's can override the default domain?
# Use SSL/TLS before starting negotiation
This configuration is very basic and it is useful only if you don't want to get delivered mails back as this functionality is also supported even though rarely used by most.
One downside of ssmtp is mail password will be plain text, so make sure you set proper permissions to /etc/ssmtp/ssmtp.conf
– If your Gmail account is secured with two-factor authentication, you need to generate a unique App Password to use in ssmtp.conf. You can do so on your App Passwords page. Use Gmail username (not the App Name) in the AuthUser line and use the generated 16-character password in the AuthPass line, spaces in the password can be omitted.
– If you do not use two-factor authentication, you need to allow access to unsecure apps.
4. Configuring different msmtp for separate user profiles
SSMTP is capable of respecting multiple relays for different local UNIX users assuming each of whom has a separate home under /home/your-username
To set a certain user lets say georgi to relay smtp sent emails with mail or mailx command create ~/.msmtprc
linux:~# vim ~/.msmtprc
Append configuration like:
# Set default values for all following accounts.
passwordeval gpg –no-tty -q -d ~/.msmtp-gmail.gpg
# Set a default account
account default : gmail
To add it for any different user modify the respective fields and set the different Mail hostname etc.
5. Using mail address aliases
msmtp also supports mail aliases, to make them work you will need to have file /etc/msmptrc with
Standard aliasses them should work
linux:~# cat /etc/aliases
# Example aliases file
# Send root to Joe and Jane
root: firstname.lastname@example.org, email@example.com
# Send everything else to admin
6. Get updated when your Debian servers have new packages to update
msmpt can be used for multiple stuff one example use would be to use it together with cron to get daily updates if there are new debian issued security or errata update pending packages, to do so you can use the apticron shell script.
To use it on debian install the apticron pack:
root@debian:~# apt-get install –yes apticron
apticron has the capability to:
* send daily emails about pending upgrades in your system;
* give you the choice of receiving only those upgrades not previously notified;
* automatically integrate to apt-listchanges in order to give you by email the
new changes of the pending upgrade packages;
* handle and warn you about packages put on hold via aptitude/dselect,
avoiding unexpected package upgrades (see #137771);
* give you all these stuff in a simple default installation;
To configure it you have to place a config copy the one from /usr/lib/apticron/apticron.conf to /etc/apticron/apticron.conf
The only important value to modify in the config is the email address to which an apt-listchanges info for new installable debs from the apt-get dist-upgrade command. Output from them will be be send to the configured EMAIL field in apticron.conf.
The timing at which the offered new pending package update reminder will be sent is controlled by /etc/cron.d/apticron
debian:~# cat /etc/cron.d/apticron
# cron entry for apticron
48 * * * * root if test -x /usr/sbin/apticron; then /usr/sbin/apticron –cron; else true; fi
apticron will use the local previous ssmtp / msmpt program to deliver to configured mailbox.
To manually trigger apticron run:
root@debian:~# if test -x /usr/sbin/apticron; then /usr/sbin/apticron –cron; else true; fi
7. Test whether local mail send works to the Internet
To test mail sent we can use either mail / mailx or sendmail command or some more advanced mailer as alpine or mutt.
Below is few examples.
linux:~$ echo -e "Subject: this is the subjectnnthis is the body" | mail firstname.lastname@example.org
To test attachments to mail also works run:
linux:~$ mail -s "Subject" email@example.com < mail-content-to-attach.txt
Prepare the mail you want to send and send it with sendmail
linux:~$ vim test-mail.txt
Subject: Test Email
This is a test mail.
linux:~$ sendmail -t < test-mail.txt
Sending encoded atacchments with uuencode is also possible but you will need sharutils Deb / RPM package installed.
To attach lets say 2 simple text files uuencoded:
linux:~$ uuencode file.txt myfile.txt | sendmail firstname.lastname@example.org
To: email@example.com From: firstname.lastname@example.org Subject: A test Hello there." > test.mail
linux:~$ cat test.mail | msmtp -a default <username>@domain.com
That's all folks, hope you learned something, if you know of some better stuff like ssmtp please shar e it.