Oct 13, 2016

Linux: Sending Mail From Command Line

E-mail clients are usually a graphical and heavy application, sometimes all that you wish is a quick and lightweight way to send an email, look here an example using GMail with the sSMTP and MSMTP tool.

MSMTP

Install from apt-get:

apt-get install msmtp ca-certificates

Edit its configuration file:

nano /etc/msmtprc

Add this in the end:

defaults
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default
host smtp.gmail.com
port 587
auth on
user [email protected]
password passwordhere
from [email protected]
logfile /var/log/msmtp.log

Now test with this command:

echo -e "Subject: Test subject" | msmtp --debug --from=default -t [email protected]

That's it! check your inbox. Now let's see another way, with attachments.

sSMTP and MPACK

Install them:

apt-get install ssmtp
apt-get install mpack

Edit the configuration file:

nano /etc/ssmtp/ssmtp.conf

Add this content with your data:

[email protected]
mailhub=smtp.gmail.com:465
rewriteDomain=gmail.com
AuthUser=user
AuthPass=password
FromLineOverride=YES
UseTLS=YES

Now test with this command:

ssmtp [email protected]mail.com

Write 'subject: something' and hit CTRL + D when done, then you should see an email in your inbox. To attach a file we will use the mpack tool, which gets the proper MIME file type and send:

mpack -s subject file.txt [email protected]

Check your inbox and you should see the file attached. That's all! more details about each tool here:
sSMTP
MSMTP
MPACK

Used versions:
  • sSMTP 2.64
  • msmtp version 1.6.3
  • mpack version 1.6

0 comentários :

Post a Comment