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
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.
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.
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]>
.
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
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.
"Long Name" <[email protected]>
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 . . .
you can use mailx and always put your quotes around variables
mailx -s "$subj" [email protected] < myfile.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With