Guide to send an email from CLI using SMTP in Linux

Electronic mail is a method of exchanging messages between people using electronic devices. There are several methods to send an email, in this guide we are going to focus on sending an email from Linux CLI using SMTP.

Requirements send an email from CLI using SMTP :

  • Linux with sudo permissions
  • SMTP details for your email

Procedure :

  1. Installing epel-release on your system by using the following command.
    yum install epel-release
  2. Installing SMTP on your system by using the following command.
    sudo yum install ssmtp
  3. Open the following file in an editor.
    sudo vi /etc/ssmtp/ssmtp.conf
  4. Add the following lines to the file:
    TLS_CA_FILE=/etc/pki/tls/certs/ca-bundle.crt
    mailhub=smtp.gmail.com:587
    useSTARTTLS=YES
    AuthUser=youremail@mail.com
    AuthPass=your_password

smtp 01

  1. Save the file by pressing the Esc key, type `:wq` to write the file and exit the editor.
    Note : mailhub could be already defined in the ssmtp.conf file and make sure you edit the definition instead of adding a new mailhub definition.
  2. Send the email using the following command.
    echo -e "To: receiver@mail.com\nFrom: Sender Name <sender@mail.com>\nSubject: Email from CLI\n\nTest Email sent using SMTP from CLI" | ssmtp -v receiver@mail.com

Congratulations now you know how to send email from Linux using SMTP.

Leave a Reply