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="to.person@domain.com"
FROM_ADDRESS="from.me@domain.com"
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: <201408200908.s7K98rdu004168@e7021.com>
Date: Wed, 20 Aug 2014 04:08:53 -0500
To: to.person@domain.com, -r, --, from.me@domain.com
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.
JiniKJohny
  • 1,172
  • 13
  • 29

1 Answers1

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="to.person@domain.com"
FROM_ADDRESS="from.me@domain.com"
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).

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318