Join us
Basic steps to successfully build your Linux emails
Linux operating system powers a huge portion of the worldâs servers and supercomputers!Â
In fact, ~96% of web servers (out of the top 1M) and the 500 fastest supercomputers worldwide use Linux. Fun trivia â SpaceX used Linux-based systems to successfully complete 65 space missions.Â
Does Linux send email?
Oh yes, system administrators can easily send emails with the Linux command line using various tools available in the ecosystem. In this article, weâll walk you through the steps to send emails with Linux using some popular command-line tools.
The following sections list the most popular commands, youâll learn how to send with Mailtrap Sending API, and we included a section to test emails in a sandbox with Mailtrap Testing.Â
Note that some of the typical command options you can use to send emails with a Linux command line interface include:
-a
â to denote an attachment-b
â (BCC) the blind copy-c
â (CC) the email address copy-s
â to denote email subjectWe included the installation steps on Debian and RedHat-based distributions for the commands that might not be on Linux by default. Note that some may require installing an additional package manager. But if you already have them installed, feel free to skip these steps.Â
curl
commandThe curl
command is a versatile tool for transferring data between servers and applications. It can also be used to send emails from the command line in Linux.Â
Hereâs how to use it:
First, create a plain text file containing the email message you want to send. You can use a text editor like nano or vim to create the file. For example, letâs create a file called message.txt
with the following content:
From: "User Name" <username@example.com>
To: "John Doe" <john@example.com>
Subject: You are awesome
Congrats for sending a test email with Mailtrap on Linux.
Replace the credentials, and modify the subject and body of the email as needed.
Next, use the curl
command to send the email. Hereâs the basic syntax:
curl \
--ssl-reqd \
--url 'https://send.api.mailtrap.io/api/send:587' \
--user 'YourMailtrapPassword or API token' \
--mail-from from@example.com \
--mail-rcpt to@example.com \
--upload-file - message.txt
From: âUser Nameâ <username@example.com>
To: âJohn Doeâ <john@example.com>
Subject: You are awesome!
Content-Type: multipart/alternative; boundary="boundary-string"
--boundary-string
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Congrats for sending test email with Mailtrap on Linux!
--boundary-string
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
--boundary-string--
mail
commandAptly named, the mail command does exactly that â sends emails from the command line. Itâs a good option to programmatically generate emails from web apps or Shell scripts, particularly in systems sans a GUI.Â
mail
If youâre running a Debian-based distribution like Ubuntu, use the following commands to update and install it:
$ sudo apt-get update
Then, run the following command to install mailutils
:
$ sudo apt install -y mailutils
If youâre running a Red Hat-based distribution like CentOS, you can use the following command:
sudo yum install mailx
Note: When installing mailutils
on Debian-based distributions, the action automatically installs Postfix. Follow the Postfix Configuration wizard to connect it to your website.Â
Continue configuring Postfix so it works with your email provider. To do that, first, navigate to the configuration file below and edit it.Â
$ sudo nano /etc/postfix/main.cf
At the end of the file, youâll find relayhost =
. Delete the line and add the following in its place:Â
relayhost = [live.smtp.mailtrap.io]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
Save the file, then exit.Â
To keep your credentials safe, create a file using the snippet below:Â
$ sudo nano /etc/postfix/sasl_passwd
Then, populate the file with your credentials.
[live.smtp.mailtrap.io]:587 username@example.com:password
If you arenât a Mailtrap user, list your credentials, then save and exit.Â
Next, use the postmap
command to make a hash database.Â
$ sudo postmap /etc/postfix/sasl_passwd
Now, navigate to the <strong>/etc/postfix/</strong>
directory, and you should be able to find the <strong>sasl_passwd.db</strong>
file.Â
Use the snippet below to change the certs
directory.Â
$ cd /etc/ssl/certs
Get the Thwate cert.Â
$ sudo wget https://www.thawte.com/roots/thawte_Primary_Root_CA.pem
Create certificate.
$ cat /etc/ssl/certs/thawte_Primary_Root_CA.pem | sudo tee -a /etc/postfix/cacert.pem
Inspect the configuration by locating the cacert.perm
under /etc/postfix
directory.
Now that mail
is installed and configured, you can send an email using the following command:
echo "This email body is sent with Mailtrap" | mail -s "Mailtrap Linux email" recipient@example.com
You can change the body and the subject in the echo
command, and the recipient, of course.
Click here to Maitrap blog post about how to launch mpack and mutt commands.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.