Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email using unix shell scripting

I have to write a script to send mails using unix shell scripts.

The following script allows me to have variable message body.

Is it possible to have a variable subject part in the code below?

#!/bin/bash
# Sending mail to remote user

sender="[email protected]"
receiver="[email protected]"
body="THIS IS THE BODY"
subj="THIS IS THE SUBJECT."


echo $body | mail $receiver -s "THIS IS THE SUBJECT" // this works fine
echo $body | mail $receiver -s $subj // ERROR - sends one mail with only
//"THIS" as subject and generates another error mail for the other three words 
like image 892
kk. Avatar asked Nov 23 '09 09:11

kk.


People also ask

Can I send email from Linux command line?

Use the sendmail command to send emails to one or more people at once. Sendmail is one of the most popular SMTP servers in Linux. You can easily send emails directly from the command line using the sendmail command. To route the information, the sendmail command makes use of the network configured on your system.

What is mail command in UNIX?

The Mail command in unix or linux system is used to send emails to the users, to read the received emails, to delete the emails etc. Mail command will come in handy especially when writing automated scripts. For example, you have written an automated script for taking weekly backup of oracle database.


3 Answers

You forgot the quotes:

echo $body | mail $receiver -s "$subj"

Note that you must use double quotes (otherwise, the variable won't be expanded).

Now the question is: Why double quotes around $subj and not $body or $receiver. The answer is that echo doesn't care about the number of arguments. So if $body expands to several words, echo will just print all of them with a single space in between. Here, the quotes would only matter if you wanted to preserve double spaces.

As for $receiver, this works because it expands only to a single word (no spaces). It would break for mail addresses like John Doe <[email protected]>.

like image 167
Aaron Digulla Avatar answered Oct 01 '22 22:10

Aaron Digulla


Old question, I know, but likely ever popular. My favorite way provides more flexibility and has worked on any UNIX/POSIX environment I have used since the dawn of my UNIX use. Only the sendmail path may change to meet the local implementation.

sed <<"ENDMAIL" -e '/^From [^ ]/s/^From /From  /' -e 's/^\.$/. /' | /usr/sbin/sendmail -t -f "$sender"
To: $receiver, $receiver2, $receiver3
From: $sender
Subject: $subj
Any-Other-Standard-Optional-Headers: place headers in any order, including,
MIME-Version: 1.0
Content-Type: text/plain;
X-Mailer: any identity you want to give your E-mailing application
X-Your-Custom-Headers: X- headers can be your own private headers
x-Last-Header: a blank line MUST follow the last header

Your body text here

ENDMAIL
  • In many environments lines starting with "From " then a non-space, are reserved as an internal "start new message" marker in bundles of messages. Stick an extra space in to avoid truncating your messages. Though for maximum portability on any system with perl replace sed with: perl -p -e 's/^From ([^ ])/From $1/' as sed in some UNIX systems is not really up to what I like and especially if echo "From me" gets a third space when piped into your local sed.
  • A line consisting of just a period is the classic end-of-message marker to the sendmail family of agents. Change such lines to end in a space... looks the same but no longer triggers end-of-message truncations.
  • You can use full E-mail addresses, like:
    "Long Name" <[email protected]>
  • Except for sendmail's -f option all the text needing quotes, escapes, and the like, are buried in the "here document", which suddenly are much less bothersome.
  • Read the man page on the sendmail "-" options used here.

Get more elaborate and you can send attachments, different message encoding, different E-mail formats, multipart messagesm and more. Do some research on this and beware the number of hyphens needed in different parts of the message.

Content-Type: multipart/mixed;
  boundary="----Some-unique-char-string-not-in-any-message"

------Some-unique-char-string-not-in-any-message
. . .
like image 29
Gilbert Avatar answered Oct 01 '22 21:10

Gilbert


you can use mailx and always put your quotes around variables

mailx -s "$subj" [email protected] < myfile.txt
like image 26
ghostdog74 Avatar answered Oct 01 '22 22:10

ghostdog74