2

I need to send a mail regarding deployment of an application using shell script. For this I have just created a shell script and tested with

#!/bin/bash

TO_ADDRESS="[email protected]"
FROM_ADDRESS="[email protected]"
SUBJECT="Test mail"
BODY="hai friend, this mail is automated from shell script for Release automation."

echo ${BODY}| mail -s ${SUBJECT} ${TO_ADDRESS} -- -r ${FROM_ADDRESS}

But while running this script, it is printing like:

You have new mail in /var/spool/mail/jaykay

And a file named jaykay is created in /var/spool/mail/

  • Why this is happening?

  • How can I send a mail using shell script?

And the output file looks like

From jaykay Wed Aug 20 04:08:53 2014
Return-Path: <jaykay>
Received: (from jaykay@localhost)
    by e7021.com (8.14.4/8.14.4/Submit) id s7K98rdu004168;
    Wed, 20 Aug 2014 04:08:53 -0500
From: Jini K Johny <jaykay>
Message-Id: <[email protected]>
Date: Wed, 20 Aug 2014 04:08:53 -0500
To: [email protected], -r, --, [email protected]
Subject: Test mail
User-Agent: Heirloom mailx 12.4 7/29/08
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

hai friend, this mail is automated from shell script for Release automation.
3
  • you are missing a } in your last line on ${FROM_ADDRESS. Commented Aug 20, 2014 at 8:24
  • did you setup a mail server yet? For instance OpenSmtpd cise.ufl.edu/~bn0/mail_tutorial.html Commented Aug 20, 2014 at 8:27
  • @ÉdouardLopez it was a typo, updated now. thanks for the review. Commented Aug 20, 2014 at 8:53

1 Answer 1

2

Your fundamental problem is that you must use proper quoting. This is basically a duplicate of a question which gets asked here every day.

Without quotes, the second token in $SUBJECT is interpreted as the address to send to.

  • An email is submitted for delivery to mail (the second word in "Test mail").

  • The address is undeliverable, so you get a bounce message.

  • The bounce message is delivered to your inbox.

  • Your shell notifies you that your inbox has a new message.

Additionally, it seems that your version of mail does not understand the -- option, so it gets taken as just one more address to send to. (You would get a bounce message from that, I suppose.) Because the -r option is also interpreted as just another address to send to, you get one copy (like a Bcc:) of the outgoing message in the mailbox you tried to specify in the $FROM_ADDRESS.

The fix, of course, is easy:

#!/bin/bash

TO_ADDRESS="[email protected]"
FROM_ADDRESS="[email protected]"
SUBJECT="Test mail"
BODY="hai friend, this mail is automated from shell script for Release automation."

echo "${BODY}" | mail -s "${SUBJECT}" "${TO_ADDRESS}"  # -- -r "${FROM_ADDRESS}"

(The curlies aren't really necessary here, but I kept them since you had them in your code.)

E.g. this recent answer has guidance for when and how exactly you need to quote.

The mail program is really a rather thin wrapper; you could do something like this instead;

/usr/lib/sendmail -oi -t -f "$FROM_ADDRESS" <<____HERE
From: My Name <$FROM_ADDRESS>
To: Your Name <$TO_ADDRESS>
Subject: $SUBJECT

$BODY
____HERE

... where the path to /usr/lib/sendmail is likely to be something else on many systems.

(For bonus k00lness points, add X-Mailer: Look, I can put anything I like here!)

I'm guessing here that you meant sendmail -f "$FROM_ADDRESS" which sets the envelope sender address (not -r which I cannot find documented anywhere).

Sign up to request clarification or add additional context in comments.

6 Comments

No change in the output. :(
What's in /var/spool/mail/jaykay? I'm guessing you are getting a bounce message or automatic response for some other reason, and that the bounce message tells you why.
Guessing the -r option is erroneous, I can't find any documentation for it. Did you mean -f?
am in new with this shell script. :(
But from and to are different, and the mail is not delivering.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.