Send email from Linux terminal

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
port 587
tls              on                                                                        #using TLS encryption
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt  #TLS trust file for 
from                                      #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=" (YOURNAME)"
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 < /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. is the email id of the recipient. 

Alternatively you can also use below syntax:
mailx -s "subject line" 

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