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="root@sped56.lss.emc.com"
receiver="root@sped56.lss.emc.com"
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
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 <doe@none.com>
.
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-addr@example.com>
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 . . .
你可以使用mailx并始终在变量周围加上引号
mailx -s "$subj" my.email@my.domain.com < myfile.txt
mailx will not present in many of the environments. but you can have the same functionality using mail/sendmail
cat myfile.txt | mail -s " Hi My mail goes here " mailid@domain.com
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.