Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Mail Services»How to Send Email from Gmail SMTP with Linux Command Line

    How to Send Email from Gmail SMTP with Linux Command Line

    By RahulJune 24, 20212 Mins Read

    In our earlier tutorial we had discussed about SSMTP for sending email through SMTP server via command line. This tutorial makes this process much easier using the sendemail command-line utility. SendEmail is a Lightweight command line SMTP email client for sending emails through the SMTP server.

    Advertisement

    In this article, we have described how to install sendemail package and a few examples for sending email through remote SMTP servers like Gmail.

    Install SendEmail Package

    SendEmail SMTP client is available under default Ubuntu repositories, Use the following commands to install it.

    sudo apt-get update
    sudo apt-get install sendemail
    

    Send Email through SMTP

    Example 1:

    In this example, the email body is passed as an argument (-o message-file=/tmp/mailbody.txt) to the command. Commands read the file content and use at email body content.

    sendemail -l email.log     \
        -f "[email protected]"   \
        -u "Email Subject 1"     \
        -t "[email protected]" \
        -s "smtp.gmail.com:587"  \
        -o tls=yes \
        -xu "[email protected]" \
        -xp "Email Password" \
        -o message-file="/tmp/mailbody.txt"
    

    Example 2:

    In this example, email body text is passed as piped input from other command output. You can also include cc and bcc email addresses as well.

    cat mailbody.txt | sendemail -l email.log \
        -f "[email protected]"  \
        -u "Email Subject 2"  \
        -t "[email protected]"  \
        -cc "[email protected]"  \
        -bcc "[email protected]"  \
        -s "smtp.gmail.com:587"  \
        -o tls=yes  \
        -xu "[email protected]"  \
        -xp "Email Password" 
    

    mail command line sendemail sendmail smtp
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Configuring Sendmail through the External SMTP relay

    Postfix: Sending Emails From External SMTP Servers

    Postfix: Sending Emails From External SMTP Servers

    Postfix: Configure SASL Authentication for Remote SMTP

    Postfix: Configure SASL Authentication for Remote SMTP

    View 7 Comments

    7 Comments

    1. stephan on October 13, 2019 11:02 am

      I dont have 2 factor auth enabled and I am still getting
      ERROR => ERROR => SMTP-AUTH: Authentication to smtp.gmail.com:587 failed.

      Any ideas ?

      Reply
      • Thepoor on December 26, 2019 2:11 am

        Hellow brothers you can teach me how?

        Reply
      • domenico on May 13, 2021 4:18 pm

        sudo apt-get install libio-socket-ssl-perl
        sudo apt-get install libnet-ssleay-perl

        Reply
    2. A.DC on August 2, 2019 12:32 pm

      Works like a charm. Yet I have one question: is there away to pass the body as html? I tried with the -o option referencing to a .html file but it just sends it in clear.

      Reply
    3. Jim Mooney on July 24, 2018 1:32 am

      Doesn’t work. Gmail changed the certification routine.
      Authentication to smtp.gmail.com:587 failed.

      Reply
      • MC on December 31, 2018 6:10 pm

        If you have 2 factor auth enabled, you need to create an “application password” in google security, then use that instead of your normal password. it works fine then, as posted here.

        Reply
    4. Mangesh Sathe on December 13, 2015 7:14 am

      Thanks. It worked. You saved my sunday!
      Can you tell me where can I configure gmail SMTP? so that i dont have to type every time gmail id and password!

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.