For those managing servers or working on automation tasks, knowing how to send emails from the Linux terminal is essential. It offers complete control over email functions and eliminates the need for complex mail programs. This is useful in scenarios where speed and simplicity matter most.
Common tools such as sendmail
and mailx
are frequently used for sending messages, checking SMTP settings, automating alerts, and integrating with scripts. They are straightforward yet effective, making them perfect for tasks like informing teams about server updates, automating reports, or testing email setups.
This guide is designed for users looking to manage their email directly from the terminal. It covers the installation of essential tools and delves into more advanced tasks, such as sending attachments and configuring email tools.
Two commonly used tools, sendmail
and mailx
, are reliable options for mail transmission in Linux. They come with a certain set of benefits:
Before utilizing these Linux mail command line tools, ensure you have terminal access. Root privileges may be required in some cases, especially for configuring each mail command on Linux discussed in this guide.
SMTP servers are essential for sending emails. These servers fall into two categories: External and Local SMTP servers.
It refers to a mail server hosted by a third-party provider. These servers are utilized to deliver emails over the internet to recipients who are not part of your local network. They are built to manage global mail delivery while ensuring proper authentication, encryption, and spam prevention.
Gmail
Address: smtp.gmail.com
Port: 587 (with TLS) or 465 (with SSL)
Outlook
Address: smtp.office365.com
Port: 587
These servers need appropriate authentication methods (such as a username, password, or app-specific passwords) and encryption (like TLS or SSL) to ensure secure communication.
Note: We’ve already provided a guide for setting up external SMTP servers. The command to send emails through Postfix remains the same as mentioned in this article. Simply configure the SMTP settings using our guide, and replace the email address with Gmail or any other preferred provider for proper email delivery.
This server functions solely within a private network or system. It is perfect for:
Here are the procedures to set up a local SMTP server using Postfix:
Install Postfix via:
sudo apt install postfix
Modify the Postfix configuration file:
sudo nano /etc/postfix/main.cf
Update or confirm these key settings:
myhostname = mail.office.local
mydomain = office.local
myorigin = $mydomain
inet_interfaces = loopback-only
local_recipient_maps = proxy:unix:passwd.byname
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
Save and exit the file after doing changes, then restart Postfix:
sudo systemctl restart postfix
To create email addresses like linux@office.local and hostman@office.local, set up user accounts on the server:
sudo adduser linux
sudo adduser hostman
sendmail
is a prominent mail transfer agent (MTA) in Linux. It works flawlessly with SMTP servers for mail delivery and allows emails to be sent and routed from local systems or scripts.
Before sending emails, you must install the Linux sendmail
tool. Execute the commands below based on your distribution:
sudo apt install sendmail
sudo yum install sendmail
Once installed, make sure sendmail
is running and configured to start at boot:
sudo systemctl start sendmail
sudo systemctl enable sendmail
Check the sendmail
is set up correctly by executing:
echo "Testing sendmail setup" | sendmail -v your-email@example.com
Verify email by executing the mail
command:
mail
Note: Install mailutils
package in case the mail command is not working.
sudo apt install mailutils
Or utilize the cat
command:
cat /var/mail/user
To customize settings for sendmail
, modify the configuration file located at /etc/mail/sendmail.mc
:
sudo nano /etc/mail/sendmail.mc
Make the required changes to fit your server. For example, if you want to define the domain name for your server, you can add or modify the following line:
define(`confDOMAIN_NAME', `your_domain.com')dnl
Here, replace your_domain
with your actual domain name.
Then rebuild the configuration file:
sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
If a "permission denied" error occurs, use:
sudo sh -c "m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf"
Finally, restart the service:
sudo systemctl restart sendmail
With sendmail
, you can easily deliver emails, customize subjects, and even add attachments using external tools. Let’s go over the process to send emails:
To send an email with sendmail
, use the below-given instructions:
First, create a file to hold the message:
nano email.txt
Add any content to the file, for example:
Subject: Test Email from Hostman
This is a test email sent using sendmail on Linux.
Deliver the file's contents:
sendmail recipient@example.com < email.txt
The contents of email.txt
will be sent to the designated recipient.
For verification, apply:
mail
sendmail
by itself doesn’t support attachments. You’ll need to utilize uuencode
or similar tools to include files. First, install sharutils
for uuencode
:
sudo apt install sharutils
Here’s how to attach a file:
( echo "Subject: Email with attachment"; uuencode file.txt file.txt ) | sendmail recipient@example.com
In the above sendmail
example we send an email with file.txt
attached.
To verify, apply the Linux command mail
:
mail
The mailx
Linux command is a simple and effective terminal application for managing emails. It is included in the mailutils package found in most Linux distributions.
Install mailutils
package on your system to utilize the mailx
command on Linux:
sudo apt install mailutils
sudo yum install mailx
This is a simple example demonstrating the use of mailx
.
Include a subject line and message in your email:
echo "This is the body of the email" | mailx -s "Test Email from Mailx" recipient@example.com
Utilize the Linux mail
command for verification:
Use the -A
flag with the mailx
command to send emails from Linux with attachments:
echo "Please find the attached document" | mailx -s "Email with Attachment" -A email.txt recipient@example.com
This sends email.txt
as an attachment to the recipient.
Sending email from the Linux command line is an effective method for automating communication tasks, troubleshooting servers, or testing configurations. Using tools such as sendmail
and mailx
, you can manage everything from simple messages to more complex setups with attachments. This guide has provided detailed instructions to help you begin without difficulty. Utilize these Linux email commands to improve your workflow. If you face any issues, feel free to refer back to this tutorial.