Send email from Linux terminal

/
0 Comments
Yeah you can send mails right from your terminal. No need to every time log into Gmail accounts. In this post I'm going to show you that how you can send mail from your Linux terminal using Gmail account.

Follow these steps:

1) Install required tools: 

Fire following commands from your terminal

sudo apt-get install msmtp  
sudo apt-get install mailutils

msmtp is an SMTP client and mailutils provides set of utilities to send and receive emails.

2) Configure msmtp:

We need to configure msmtp client before we can start sending emails.
For that fire this command:

gedit ~/.msmtprc

This will open new file named .msmtprc in gedit.

Paste following lines in this file:



# gmail account
account gmail
auth on
host smtp.gmail.com
port 587
tls              on                                                                        #using TLS encryption
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt  #TLS trust file for 
                                                                                                                #gmail
user YOURUSERNAME@gmail.com
password YOURPASSWORD
from YOURUSERNAME@gmail.com                                      #recipient will receive mail
                                                                                                   # from this email 


# set default account to use (not necessary with single account)
account default : gmail

Replace capitalized words with appropriate value specific to your account .

Save the file and close gedit. 

Now fire following command to change permission of above file:

chmod 600 ~/.msmtprc


3) Configure mailx:

Fire following command in terminal:

gedit ~/.mailrc

This will again open a new file in gedit.
Paste following lines to this file.

from="YOURUSERNAME@gmail.com (YOURNAME)"
sendmail="/usr/bin/msmtp"
message-sendmail-extra-arguments="-a gmail"

Again replace capitalized words with appropriate values.

Save the file and close gedit.

Now you are all set to send mails right from your terminal.

Here is the syntax of the command:
mailx -s "subject line" -a /path/file recipient@email.com < /path/body.txt

Here -a option specifies file to be attached you can remove this option if you don't want to send attachments. "/path/body.txt" specifies the path of the file where body text is written.
-s option specifies subject of the mail.
recipient@email.com is the email id of the recipient. 

Alternatively you can also use below syntax:
mailx -s "subject line" recipient@email.com 

After firing above command you can write body text of the mail in terminal itself and after you have written body text press "Enter" and "ctrl+D" to send the mail.

Write comments if any trouble. 


You may also like

Popular Posts