Command sendmail command line linux

Bash script to send email

Email is a very common communication medium in today’s world now. Anyone can easily communicate or share any document with friends, family members, and colleagues by sending the email. Generally, people use any free email server like Gmail, Yahoo, Hotmail etc. for sending the email. But you can send email from command line easily in Linux operating system. There are many ways to send email in Linux. Some common and easy ways to send email in Ubuntu operating system from the terminal is shown in this tutorial.

Initialization:

Many Linux command is available to send email from the command line or by using a bash script. But any email sending command will not work if no SMTP server is set up properly in the system. You can setup your own SMTP server to send email from the command line or you can use any free SMTP server of any well-known email service provider like Gmail or Yahoo. SMTP server of a Gmail account is used in this tutorial. To do this task, you have to select a Gmail account and enable the option of ‘Allow less secure apps’ for that account to send the email. You can visit the following tutorial to enable this option.

After enabling this option, you have to open the file ‘ /etc/ssmtp/ssmtp.conf’ with ‘root’ privilege and add the following lines at the end of the file. You have to set your email address to ‘AuthUser’ and your email password to ‘AuthPass’ to complete the setup.

UseSTARTTLS =YES
FromLineOverride =YES
root =admin @ example.com
mailhub =smtp.gmail.com: 587
AuthUser =username @ gmail.com
AuthPass =password

Example-1: Using Sendmail Command

One of the popular email sending commands of Linux is `sendmail`. The user can easily send email from the command line by using this command. If Sendmail package is not installed in the system then run the following command to install the package.

Suppose, the email content is stored in a file named ‘email.txt’ with the following content. Here, the text after the ‘Subject:’ will be sent as email subject and the remaining part will be sent as email body.

Читайте также:  Linux fdisk отказано в доступе

Run `sendmail’ command with recipient email address like the following command.

Example-2: Using ‘mail’ Command

The most common command for sending email in Linux is `mail` command. This command is not installed on Ubuntu by default. Run the following command to install `mail` command.

The following command will show the version of this command if it is installed in the system.

‘-s’ option is used in the `mail` command to define the subject of the email. Run `mail’ command by ‘-s’ option with email subject and the recipient email address like the following command. It will ask for Cc: address. If you don’t want to use Cc: field then keep it blank and press enter. Type the message body and press Ctrl+D to send the email.

If you want to add email message body in the command then use ‘

You can also add the sender email address with `mail` command by using the ‘-a’ option. The following command will send the email with the subject, ‘message subject’, sender’s name and email address, ‘Admin’ and recipient email address, username@gmail.com and message body, ‘testing message’.

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

Email can be sent to multiple recipients by using `mail` command and adding the recipients’ addresses by separating comma. The following command will be sent an email to two recipients.

Example-3: Using `mutt` command

Like `mail` command, `mutt` command is not installed on Ubuntu by default. Run the following command to install `mutt` command.

`mutt` command works similar to `mail` command but `mutt` command has own editor to send 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 `mail` or `mutt` command. The following command will attach the file items.txt while sending the email.

Example-4: Using ‘SSMTP’ Command

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

Run the following command with the valid email address. Type the email subject and body. Press ctrl+D to exit and send the email.

Читайте также:  Install python in linux centos

Example-5: Using mailx Command

You can send HTML content as email body without just sending the text message. Create an HTML file named ‘test.html’ to use as a message body.

`mailx` command works like `mail` command. You have to mention Content-Type as text/html to send HTML content as the message body in `mailx` command. The following command will send the content of ‘test.html’ file as the email message body.

$ mailx -a ‘Content-Type: text/html’ -s «Sending email by using mailx »
< test.html "username@gmail.com"

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

Conclusion

After practicing the above commands properly, hope the reader will be able to send email from the command line easily in Linux.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

5 Ways to Send Email From Linux Command Line

We all know the importance of emails these days for information transfer. There are many free emails service providers which we used for use like Gmail, Yahoo, RediffMail etc, which provides a web interface for sending and receiving emails. But this is not enough, sometimes we also required to send emails from system command line. This tutorial will provide you multiple ways to send emails from the Linux command line. This is useful for sending email through our shell scripts, cronjobs etc.

email-banner

There are various ways to send emails from the command line but here I am sharing few options used by most users. You can use anyone option given below to send email from Linux command line.

1. Using ‘sendmail’ Command

Sendmail is a most popular SMTP server used in most of Linux/Unix distribution. Sendmail allows sending email from command line. Use below instructions to send email using ‘sendmail‘ command.

Created a file with email content:

cat /tmp/email.txt Subject: Terminal Email Send Email Content line 1 Email Content line 2

Subject: line will be used as subject for email.

Now send email using the following command.

2. Using ‘mail’ Command

mail command is most popular command to send emails from Linux terminal. Use few of below examples to send an email.

Also, you can send an attachment with this command. Use -a for mailx and -A for mailutils.

Читайте также:  All network ports in use linux

Also, we can add comma separated emails to send the email to multiple recipients together.

3. Using ‘mutt’ command

Mutt is basically used for reading emails from Linux terminal from local user mailboxes, also useful to read emails from POP/IMAP servers. Mutt command is little similar to mail command. Use few of below examples to send an email.

Send an email including an attachment

4. Using ‘SSMTP’ Command

sSMTP allows users to send emails from SMTP server from Linux command line. For example to send an email to user [email protected] use following command. Now type your subject of the email as below with keyword Subject. After that type your message to be sent to the user, After finishing your message press CTRL+d (^d) to send the email.

ssmtp [email protected]  Subject: Test SSMTP Email Email send test using SSMTP via SMTP server. ^d

5. Using ‘telnet’ Command

As per my experience, all system administrators use telnet command to test remote port connectivity test or login to the server remotely. Most of the newbie in Linux doesn’t know that we can send email using telnet also, which is the better way to troubleshoot email sending problems. Below is an example of email sending.

Red marked text is the user input and remaining is the responses of that commands.

telnet localhost smtp Trying 127.0.0.1. Connected to localhost.localdomain (127.0.0.1). Escape character is '^]'. 220 fbreveal.com ESMTP Sendmail 8.13.8/8.13.8; Tue, 22 Oct 2013 05:05:59 -0400 HELO yahoo.com 250 tecadmin.net Hello tecadmin.net [127.0.0.1], pleased to meet you mail from: [email protected] 250 2.1.0 [email protected]. Sender ok rcpt to: [email protected] 250 2.1.5 [email protected]. Recipient ok data 354 Enter mail, end with "." on a line by itself Hey This is test email only Thanks . 250 2.0.0 r9M95xgc014513 Message accepted for delivery quit 221 2.0.0 fbreveal.com closing connection Connection closed by foreign host.

Bonus Tips — Attach a file to Email from Shell

You can also send email with a attachment from Linux shell. Use -A option to provide a file path with mail command.

mail -s "Test Mail" -r [email protected] -A attach.zip [email protected]  

Thank you for using this article. We will add more ways soon with this list. We also request you to help me with more commands which you know and not listed above.

Источник

Оцените статью
Adblock
detector