Linux shell send mail

Shell script to send email [duplicate]

I am on linux machine and I monitor a process usage. Most of the time I will be away from my system and I have access to internet on my device. So I planned to write a shell-script that can mail me the output of the process. Is it possible? If so how to make a shell-script send me mail? Please provide a snippet to get started.

7 Answers 7

Yes it works fine and is commonly used:

$ echo "hello world" | mail -s "a subject" someone@somewhere.com 

$ echo «hello world» | mail -s «a subject» -a «attachment file» someone@somewhere.com for sending files

Where can I find the preset tutorial for this command? As to execute this command, I need to configure something.

Basically there’s a program to accomplish that, called «mail». The subject of the email can be specified with a -s and a list of address with -t. You can write the text on your own with the echo command:

echo "This will go into the body of the mail." | mail -s "Hello world" you@youremail.com 

or get it from other files too:

mail -s "Hello world" you@youremailid.com < /home/calvin/application.log 

mail doesn't support the sending of attachments, but Mutt does:

echo "Sending an attachment." | mutt -a file.zip -s "attachment" target@email.com 

Note that Mutt's much more complete than mail. You can find better explanation here

PS: thanks to @slhck who pointed out that my previous answer was awful. 😉

Stackoverflow should be self contained - it would have taken you the same amount of time to copy/paste the URL of that site and to copy/paste the one-liner @Ashwin asked for.

@slhck: Probably the full page is better than one or two lines and there's explained better than I would do. Mine isn't surely the best answer but downvote it is quite unfair since it provides the same information of any other answer.

This has been a long discussion: meta.stackexchange.com/questions/8231/… - Even if it provides the same information, you could have digested it and cited your source.

@slhck: I agree with that, rarely I give only a link. But I thought, "a link is better than nothing", in fact I've never sent email in this way, just wanted to be helpful. 😉

Читайте также:  Warcraft 3 linux mint

sendmail works for me on the mac (10.6.8)

echo "Hello" | sendmail -f my@email.com my@email.com 
mail -s "Your Subject" your@email.com < /file/with/mail/content 

( /file/with/mail/content should be a plaintext file, not a file attachment or an image, etc)

Does this work with binary files? I think I've had to use mutt before to do all the binary file encoding in the past.

You mean when trying to send a file as a mail attachment? You're right about this, of course. I meant text files only.

#!/bin/sh #set -x LANG=fr_FR # ARG FROM="foo@bar.com" TO="foo@bar.com" SUBJECT="test é" MSG="BODY éé" FILES="fic1.pdf fic2.pdf" # http://fr.wikipedia.org/wiki/Multipurpose_Internet_Mail_Extensions SUB_CHARSET=$(echo $ | file -bi - | cut -d"=" -f2) SUB_B64=$(echo $ | uuencode --base64 - | tail -n+2 | head -n+1) NB_FILES=$(echo $ | wc -w) NB=0 cat To: $ MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=frontier Subject: =?$?B?$?= --frontier Content-Type: $(echo $ | file -bi -) Content-Transfer-Encoding: 7bit $ $(test $NB_FILES -eq 0 && echo "--frontier--" || echo "--frontier") $(for file in $ ; do let NB=$+1 FILE_NAME="$(basename $file)" echo "Content-Type: $(file -bi $file); name=\"$\"" echo "Content-Transfer-Encoding: base64" echo "Content-Disposition: attachment; filename=\"$\"" #echo "" uuencode --base64 $ $ test $ -eq $ && echo "--frontier--" || echo "--frontier" done) EOF 

Well, the easiest solution would of course be to pipe the output into mail:

vs@lambda:~$ cat test.sh sleep 3 && echo test | mail -s test your@address vs@lambda:~$ nohup sh test.sh nohup: ignoring input and appending output to `nohup.out' 

I guess sh test.sh & will do just as fine normally.

top -b -n 1 | mail -s "any subject" your_email@domain.com 

Linked

Hot Network Questions

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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.

Читайте также:  Управление ядрами процессора linux

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.

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’.

Читайте также:  Баррет linux основные команды карманный справочник

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.

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.

Источник

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