Bash script to send email

initialization:

Many linux commands are available to send email from the command line or via a bash script. but any send email command will not work if no smtp server is properly configured in the system. You can set up your own smtp server to send email from command line or you can use any free smtp server from any well known email service provider like gmail or yahoo. This tutorial uses the smtp server of a gmail account. To perform this task, you must select a Gmail account and enable the ‘Allow less secure apps’ option for that account to send the email. you can visit the following tutorial to enable this option.

after enabling this option, you need to open the ‘/etc/ssmtp/ssmtp.conf’ file with ‘root’ privilege and add the following lines to the end of the file. you must set your email address to “authuser” and your email password to “authpass” to complete the setup.

example-1: use the sendmail command

one of the most popular linux send email commands is `sendmail`. user can easily send email from command line using this command. If the sendmail package is not installed on the system, run the following command to install the package.

suppose the content of the email is stored in a file called “email.txt” with the following content. here, the text after the ‘subject:’ will be sent as the subject of the email and the remaining part will be sent as the body of the email.

See Also:  How To Get FREE Coupons From Meijer Why You&039re Not Getting Them

email.txt

run the ‘sendmail’ command with the recipient’s email address as the following command.

example-2: use the ‘mail’ command

The most common command to send email on linux is the `mail` command. this command is not installed in ubuntu by default. run the following command to install the `mail` command.

The following command will display the version of this command if it is installed on the system.

The ‘-s’ option is used in the ‘mail’ command to define the subject of the email. run the ‘mail’ command with the ‘-s’ option with the email subject and the recipient’s email address as the following command. it will ask for cc: address. if you don’t want to use the cc: field, leave it blank and press enter. type the body of the message and press ctrl+d to send the email.

if you want to add the body of the email message in the command, use the ‘<<<‘ operator like the following command.

You can also add the sender’s email address with the ‘mail’ command using the ‘-a’ option. the following command will send the email with the subject, ‘message subject’, the sender’s name and email address, ‘admin<[email protected]>’ and the recipient’s email address, [email protected] and the message body, ‘testing message’.

You can also send the body of an email message using the echo command and pipe( | ). The following command uses the pipe (|) to send the body of the message.

See Also:  How Does Schedule Send Work in Gmail?

email can be sent to multiple recipients using the `mail` command and adding the recipient addresses separating the comma. the following command will send an email to two recipients.

example-3: using the `mutt` command

just like the `mail` command, the `mutt` command is not installed on ubuntu by default. run the following command to install the `mutt` command.

The `mutt` command works similar to the `mail` command but the `mutt` command has its own editor for sending the email. run the following command with the valid email address to send the email.

You can attach any file using the ‘-a’ option with the ‘mail’ or ‘mutt’ command. the following command will attach the items.txt file when sending the email.

example-4: use the ‘ssmtp’ command

Another email command is the `ssmtp` command. if ssmtp is not installed before, install the package first. the installation command for ssmtp is shown in example-1. ssmtp and sendmail commands work similarly.

run the following command with the valid email address. Type the subject and body of the email. press ctrl+d to exit and send the email.

example-5: using the mailx command

you can send html content as email body without sending just the text message. create an html file called ‘test.html’ to use as the message body.

test.html

the `mailx` command works like the `mail` command. you must mention the content type as text/html to send html content as the message body in the `mailx` command. the following command will send the content of the ‘test.html’ file as the body of the email message.

See Also:  Treasure Hunt - Treasure Hunt

When you check the inbox of the recipient’s email account, the list of emails will be shown as the following image.

conclusion

After successfully practicing the above commands, I hope the reader will be able to send email from the command line easily on linux.

Leave a Reply

Your email address will not be published. Required fields are marked *