Linux shell mail from

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

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

Читайте также:  Kali linux grub error

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

Источник

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.

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.

Читайте также:  How to check cpu temperature linux

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.

Источник

Sending a mail from a linux shell script

I want to send an email from a Linux Shell script. What is the standard command to do this and do I need to set up any special server names?

Try asking on Superuser [superuser.com ] or, better yet, Unix and Linux SE [unix.stackexchange.com ].

11 Answers 11

If the server is well configured, eg it has an up and running MTA, you can just use the mail command.

For instance, to send the content of a file, you can do this:

$ cat /path/to/file | mail -s "your subject" your@email.com 

man mail for more details.

What does 'mta' mean here, is there a full name or a link? As a beginner, that's what I want to know. As I lack of experience to do that.

If you're not sure how to install/configure and you're on Ubuntu: sudo apt-get install mailutils and select Internet site: Mail is sent and received directly using SMTP..

If you want a clean and simple approach in bash, and you don't want to use cat , echo , etc., the simplest way would be:

mail -s "subject here" email@address.com  

If both exim and ssmtp are running, you may enter into troubles. So if you just want to run a simple MTA, just to have a simple smtp client to send email notifications for insistance, you shall purge the eventually preinstalled MTA like exim or postfix first and reinstall ssmtp.

Then it's quite straight forward, configuring only 2 files (revaliases and ssmtp.conf) - See ssmtp doc - , and usage in your bash or bourne script is like :

#!/bin/sh SUBJECT=$1 RECEIVER=$2 TEXT=$3 SERVER_NAME=$HOSTNAME SENDER=$(whoami) USER="noreply" [[ -z $1 ]] && SUBJECT="Notification from $SENDER on server $SERVER_NAME" [[ -z $2 ]] && RECEIVER="another_configured_email_address" [[ -z $3 ]] && TEXT="no text content" MAIL_TXT="Subject: $SUBJECT\nFrom: $SENDER\nTo: $RECEIVER\n\n$TEXT" echo -e $MAIL_TXT | sendmail -t exit $? 

Obviously do not forget to open your firewall output to the smtp port (25).

I would not do this in this shell otherwise you will get stucked sooner or later. You might do it in the config file : See unix.stackexchange.com/a/132731

Another option for in a bash script:

mailbody="Testmail via bash script" echo "From: info@myserver.test" > /tmp/mailtest echo "To: john@mywebsite.test" >> /tmp/mailtest echo "Subject: Mailtest subject" >> /tmp/mailtest echo "" >> /tmp/mailtest echo $mailbody >> /tmp/mailtest cat /tmp/mailtest | /usr/sbin/sendmail -t 
  • The file /tmp/mailtest is overwritten everytime this script is used.
  • The location of sendmail may differ per system.
  • When using this in a cron script, you have to use the absolute path for the sendmail command.

downvoted because more than one process could call the script at the same time. This could cause corrupt/incorrect tmp file. Also the /tmp file is not overwritten each time this is used - it actually grows with each email containing all previous emails inside. Not good.

The first write to /tmp/mailtest is an overwrite so it will remove whatever was in there previously. Still, this method is not ideal for the other reasons you have described.

Generally, you'd want to use mail command to send your message using local MTA (that will either deliver it using SMTP to the destination or just forward it into some more powerful SMTP server, for example, at your ISP). If you don't have a local MTA (although it's a bit unusual for a UNIX-like system to omit one), you can either use some minimalistic MTA like ssmtp.

ssmtp is quite easy to configure. Basically, you'll just need to specify where your provider's SMTP server is:

# The place where the mail goes. The actual machine name is required # no MX records are consulted. Commonly mailhosts are named mail.domain.com # The example will fit if you are in domain.com and you mailhub is so named. mailhub=mail 

Another option is to use one of myriads scripts that just connect to SMTP server directly and try to post a message there, such as Smtp-Auth-Email-Script, smtp-cli, SendEmail, etc.

Источник

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