Trending News
By using our website, you agree to the use of our cookies.

Blog

How to Send Email from the Linux Command Line Using an SMTP Server (with SSMTP) [ 4 Easy Steps ]
How To

How to Send Email from the Linux Command Line Using an SMTP Server (with SSMTP) [ 4 Easy Steps ] 

We want to stay connected to everyone in the global world. In order to accomplish this, email is a widely used way of sending and receiving information. Linux systems also provide command-line tools for sending emails, which can be used to obtain system information, send emails from shell scripts, and so on.

Table of Contents

Linux Command Line

Email is sent as system username@hostname when sent from a Linux terminal. These emails can be blocked or marked as spam by certain SMTP servers. As a result, we need a method that maximizes email delivery to the inbox. We would use this article to set up our server to transfer email from SMTP servers like Gmail, Amazon SES, and others. This article will show you How to Send Email from the Linux Command Line Using an SMTP Server (with SSMTP).

Step 1: Install SSMTP Server

SSMTP service packages can be found in the EPEL repository, so make sure you have the EPEL yum repository configured.

				
					# yum install ssmtp
				
			

Step 2: Set up SSMTP

Add the following values to the SSMTP configuration file. I am going to use the Gmail SMTP server for this tutorial. If you want to set up with Gmail as well, make sure you already have a Gmail account.

				
					# vim /etc/ssmtp/ssmtp.conf
				
			

In the configuration file, change the following values.

				
					mailhub=smtp.gmail.com:587
 UseSTARTTLS=YES
 AuthUser=your-email@gmail.com
 AuthPass=XXXXXXXXXXXXXXX
 TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt
				
			
The following are the details listed above:
  • mailhub: your SMTP server’s host/ip address and port
  • UseSTARTTLS: If the SMTP server uses TLS, set it to Yes; otherwise, set it to No.
  • AuthUser: Use your Gmail ID here.
  • AuthPass: Use your Gmail ID’s password.
  • TLS_CA_File: If you encounter an error such as “send-mail: Cannot open smtp.gmail.com:587,” this will take some time.

Step 3: Send a Test Email

Send a test email to an email address to test the setup. First, write the email in a text file, as follows:

				
					# vim msg.txt
				
			
Add email content, such as:
				
					msg.txt
				
			
				
					Subject: This is Subject Line

Email content line 1
Email content line 2
				
			

Then, using the ssmtp command, send an email to “user@domain.com”:

				
					# ssmtp user@domain.com < msg.txt
				
			

Let us see if there is something new in your inbox.

Step 4: Make SSMTP the default client.

Set SSMTP as your default mail server so that you can send emails via SSMTP with the mail command.

				
					alternatives --config mta

There are 2 programs which provide 'mta'.

  Selection    Command
-----------------------------------------------
   1           /usr/sbin/sendmail.ssmtp
*+ 2           /usr/sbin/sendmail.sendmail

Enter to keep the current selection[+], or type selection number: 1

				
			

Verify the changes by following command:

				
					sendmail -V
sSMTP 2.61 (Not sendmail at all)
				
			
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on vk
Share on tumblr
Share on skype
Share on stumbleupon
Share on whatsapp

Related posts

Leave a Reply