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. Gmail offers very portable smtp server to send mail from different network. You can also send mail from your Linux computer by configuring the Gmail SMTP server in you Linux system. This tutorial will guide you through how to configure smtp server for gmail to send email by utilizing this Gmail smtp resource. But before we discuss more about the topic, I want you to know the term 'SSMTP'.
SSMTP program is not used to receive mails, it is used to send mails from your local computer to any mail host.
Also see how to create ssl certificate in Linux for postfix mail server.

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 email. You can configure and setup smtp for various purposes, but one of the most important reason to setup smtp server for gmail in your server is to get a prior notification via an email before any disaster occur in the server. In other words receiving am email during an error, warning message or alert if there is any intruder attack on the server.

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

You can install ssmtp package in your Linux computer by the following command.

# yum install ssmtp

ssmtp configuration

Aftre the ssmtp is installed, ssmtp.conf file will be created under 'etc' directory. You need to edit the file as given below. Find the following lines inside the file and change. Please note the following configuration is only for gmail. Here we will be configuring smtp server for gmail only.

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
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
AuthUser=your_id@gmail.com
AuthPass=your password
TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt

 

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 your_id@gmail.com
Subject: TEST
This is a test mail
^d

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" your_id@gmail.com

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 your_id@gmail.com

Set SSMTP as default

The crucial thing to note here that command 'echo' and 'mail' 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" your_id@gmail.com


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
To:Recipient_id@anyMail.com 
From:your_id@gmail.com 
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

Download Our Free eBook now

Linux and UNIX Shell scripting ebook

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

Leave a Reply

Your email address will not be published.


*


shares