Sending mail from Linux shell script

I want to send an email from a linux shell script. What is the standard command for this, and do I need to configure any special server names?

+94
linux shell email sendmail
Mar 01 2018-11-11T00:
source share
9 answers

If the server is configured correctly, for example, it has an MTA, it can use the mail command.

For example, to send the contents of a file, you can do this:

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

man mail for more details.

+103
Mar 01 2018-11-11T00:
source share

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

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

<<< used to redirect standard input. It has been part of bash for a long time.

+77
Nov 19
source share

If both Exim and ssmtp are running, you may run into problems. Therefore, if you just want to start a simple MTA, just for the simple smtp client to send email notifications for confirmation, first clear the pre-installed MTA, such as Exim or postfix, and reinstall ssmtp.

Then it is pretty simple, setting up only 2 files (revaliases and ssmtp.conf) - see ssmtp doc - and using in your bash or bourne script looks like this:

 #!/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 the firewall output to smtp (25).

+20
Mar 02 '11 at 17:53
source share

Another option for 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 /tmp/mailtest overwritten every time this script is used.
  • The sendmail location may vary for each system.
  • When using this parameter in a cron script, you need to use the absolute path for the sendmail command.
+7
Nov 10 '15 at 13:12
source share

Typically, you want to use the mail command to send your message using the local MTA (which will either deliver it using SMTP to the destination, or simply forward it to a more powerful SMTP server, for example, from your Internet service provider). If you don't have a local MTA (although it's a little unusual for a UNIX-like system to omit it), you can either use a minimalistic MTA, such as ssmtp .

ssmtp pretty easy to set up. Basically, you just need to indicate where your provider’s SMTP server is located:

 # 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 the myriads scripts that connect directly to the SMTP server and try to send a message there, for example, Smtp-Auth-Email-Script , smtp-cli , SendEmail , etc.

+6
Mar 01 2018-11-11T00:
source share

Recognizing that you want to use some smtp server, you can do:

 export SUBJECT=some_subject export smtp=somehost:someport export EMAIL=someaccount@somedomain echo "some message" | mailx -s "$SUBJECT" "$EMAIL" 

Change somehost , someport and someaccount@somedomain to the actual values ​​that you will use. In this example, encryption and authentication are not performed.

+3
Oct 11 '12 at 16:22
source share

You don't even need an MTA. SMTP is simple enough to write directly to your SMTP server. You can even communicate via SSL / TLS if you have the OpenSSL package installed. Check out this post: https://33hops.com/send-email-from-bash-shell.html

Above is an example of how to send text / html email messages that will work out of the box. If you want to add attachments, the thing may get a little complicated, you will need to base64 encode the binaries and insert them between the borders. This is a good place to start an investigation: http://forums.codeguru.com/showthread.php?418377-Send-Email-w-attachments-using-SMTP

+1
Jul 12 '16 at 19:03
source share

On linux , you can use the mail utility to send attachments with the -a option. Go through the man pages to read about the option. For example, the following code will send an attachment:

mail -s "CONFIRM THIS" -a attachment.txt name@domain.com <<< "Hello, buddy, see bug reports."

+1
Feb 21 '18 at 11:22
source share

The mail command does this (who would have guessed ;-). Open a shell and enter man mail to get the manual page for the mail command for all available options.

0
Mar 01 '11 at 14:40
source share



All Articles