Effortless Email Sending on Linux: A Step-by-Step Project Guide

Effortless Email Sending on Linux: A Step-by-Step Project Guide

INTRODUCTION

Emails have been an important part of communication. Today in this blog we are going to know how to send emails without ever going to your browser or Gmail app by just using some cool command lines on Linux. you may ask But why to do all this for now I will say maybe just to look like a hacker

SETTING UP THE THINGS

Later in the process, we are going to require an SMTP server to send emails. In this project, we are going to use Gmail servers as it is widely used. For this, we are going to set up an application password for our Google account.

  • open setting go to Google account.

  • In the Security tab if 2-step verification is off turn it on.

  • At the bottom of 2-step, there will be an option for app passwords .set up a new application password.

  • A password will come on the screen note it up as it will be not available we are going to use that later.

  • To send emails we are going to use a Linux package S-nail to install run command sudo apt-get install s-nail in your terminal.

CODE

Now let's start the project go to your Linux terminal and run the command nano filename.sh

to open a text editor and write the following shell script code.

#!/bin/bash
read -p "enter mail id:" mail
read -p "enter subject:" subject
read -p "enter message:" message

echo "$message" | s-nail  -r "yourmail" -s "$subject" -S smtp="smtp.gmail.com:587" -S smtp-use-starttls  -S smtp-auth=login -S smtp-auth-user="your gmail id" -S smtp-auth-password="app password " -S ssl-verify=ignore $mail

Let's break down the code-

  • The initial three lines are used to take the input from the user.

  • echo is used to print anything.

  • we are using the s-nail package for sending emails.

  • the message is the body of our email,-r for sender email,-s is used for the subject,-S is used to specify other settings we are using to set up our SMTP server.

  • Press ctrl+o & ctrl+x to save and exit the editor.

CONCLUSION

Ok if you finished reading this and did all things mentioned you are all set to send emails. Just run the shell script we made using sudo chmod 777 file_name.sh & ./filename.sh enter the required details press enter and enjoy the joy and happiness of sending a mail just using command lines.

Happy Coding :)

Thank you for being here!! give it a thumbs up, drop a comment and share if you find it even a little helpful.

Did you find this article valuable?

Support Akshat's blogs by becoming a sponsor. Any amount is appreciated!