SMTP server for Gmail : How to send email by Linux command

SMTP server for Gmail Introduction

Email has always been very important since it was invented. For sending mail from your Linux computer you have to configure SMTP server in you Linux mail server. This article is about configuring your smtp server for gmail to send mail via Gmail. SSMTP program is not used to receive mails, it is used to send mails from your local computer to any mail host.

What is an smtp server and where it can be used?

SMTP (Simple Mail Transfer Protocol) is an Internet standard for electronic mail transmission. It is used for sending mail. SMTP server for gmail can be used to automate mail in case of any system alert.

What is ssmtp?

SSMTP is a very simple MTA (Mail Transfer Agent). It is used to deliver mails from a computer to a SMTP server. SSMTP cannot be used to receive mails.

ssmtp installation

Install ssmtp package in your Linux computer by the following command.

# yum install ssmtp
ssmtp configuration

When ssmtp is installed, ssmtp.conf file will be created under 'etc' directory. Edit the file as given below. Find the following line and change. Please note the following configuration is only for gmail. Here we will be configuring smtp server for gmail.

mailhub = smtp server host with its port number
UseSTARTTLS = will be yes if your smtp server is using TLS(Transport Layer Security)
AuthUser = You gmail id will b here
AuthPass = your gmail id password
TLS_CA_File = This is a default set of the file path, it is required if any error occurrs when sending the mail.

# vi /etc/ssmtp/ssmtp.conf
AuthPass=your password
Test mail

When the smtp server for gmail is configured, you can actually test by sending the mail to your own gmail id. Use the below command to test if the smtp server is working.

# ssmtp
Subject: TEST
This is a test mail
Check you mail box

Check your mail box, you should have received one mail.

SMTP server for Gmail
SMTP server for Gmail

Also see tutorial how to use sed command in Linux


Echo command to send mail

In place of SSMTP you can use 'echo' command to send mail. Lets see the example.

# echo "This is a test mail" | mail -s "This is my subject"
Mail command to send mail

Similarly, if you don't want to use ssmtp or echo command to send mail you can use 'mail' command alternatively. mail command is an interactive command. After giving the subject and writing the body content use ctrl+d to send the mail.

# mail
Set SSMTP as default

The crucial thing to note here that command 'echo' and 'mail' will uses default mail server setting to send mail. If ssmtp is not allocated as default, your mail won't go with 'echo' and 'mail' command. So, you need to change mta and setup ssmtp as default smtp mail server.

# alternatives --config mta
There are 2 programs which provide 'mta'.
Selection Command

*+ 1 /usr/sbin/sendmail.postfix
   2 /usr/sbin/sendmail.ssmtp
Enter to keep the current selection[+], or type selection number:

For the above command select 2 and enter to choose ssmtp as default mta.

Confirm if default mta changed

Use the below command to check if the default mta has changed to ssmtp or not.

# sendmail -V
sSMTP 2.64 (Not sendmail at all)

Test before sending mail

It is better to test before actually sending the mail. The below command will test if your smtp server for gmail is properly configured to forward mail.

# echo This is smtp server test | mail -v -s "smtp test"

Alternative method of sending mail

An alternative method of sending mail is by creating a text file and then sending it with SSMTP. Create a test file as given below. In this case in 'To' field put mail id of the recipient, in from field keep your own mail id.

# vi test-mail.txt 
Subject:This is a test mail.
# ssmtp test-mail.txt

This is it with the article SMTP server for Gmail : How to send email by Linux command

1 Comment on SMTP server for Gmail : How to send email by Linux command

Leave a Reply

Your email address will not be published.