Sending Emails Made Easy with the Mail Command in Linux

Sending Emails Made Easy with the Mail Command in Linux

To set up mail service in Linux to send emails from your Gmail account, you can use the following steps:

  1. Install the "ssmtp" package on your Linux system, which allows you to send emails using an external SMTP server:

    sudo apt-get install ssmtp

  2. Open the ssmtp configuration file using your favorite text editor. For example, if you're using nano:

    sudo vim /etc/ssmtp/ssmtp.conf

  3. Edit the configuration file to include your Gmail account information:

    root=<your Gmail address>

    mailhub=smtp.gmail.com:587

    AuthUser=<your Gmail address>

    AuthPass=<your Gmail password>

    UseSTARTTLS=yes

    Make sure to replace <your Gmail address> and <your Gmail password> with your actual Gmail account details.

    Just have a look for file contents below--->

    To get your AuthPass:

    1. Go to your google account and search "App passwords"

    2. Select 'Mail' and 'Windows Computer'. Then Click "Generate password"

    3. Copy your 12 characters AuthPass and use this inside .conf file.

  4. Save and close the configuration file.

  5. Test the mail service by sending a test email to yourself:

    echo "Hello World" | mail -s "Test Email" <your email address>

    Make sure to replace <your email address> with your actual email address.

Let's try it!

echo "This is a test email." | mail -s "Test email" ramgaikwad##@gmail.com

Check your inbox to confirm that you received the test email.

That's it! You should now be able to send emails from your Linux system using your Gmail account.

Summary:

In this article, we learned how to set up mail service in Linux to send emails from your Gmail account. We used the "ssmtp" package to send emails using an external SMTP server and configured the ssmtp.conf file to include our Gmail account details. We then tested the mail service by sending a test email to ourselves and confirmed that we received it in our inbox. With these simple steps, you can easily set up and use the mail command in Linux to send emails from your Gmail account.

Stay tuned for my next blog on "Use Cases of Shell Scripting". I will keep sharing my learnings and knowledge here with you.

Let's learn together! I appreciate any comments or suggestions you may have to improve my Linux blog.

Thank you,

Chaitannyaa Gaikwad

Did you find this article valuable?

Support Chaitannyaa Gaikwad by becoming a sponsor. Any amount is appreciated!