Sending mail from bash [Script]

Sending mail from bash [Script]



The mail command can be used under Linux or UNIX bash, ksh, csh shell to send an email. To send a message to one or more people, mail can be invoked with arguments which are the names of people to whom the mail will be sent. You are then expected to type in your message, followed by an <control-D> at the beginning of a line. However, using the following syntax one can send email easily:

Apply here payday loans

mail -s ‘Subject’ yourmail@test.com

Script

#!/bin/bash
# Subject
SUBJECT="EMAIL-SUBJECT"
# To
TOEMAIL="tomail@test.com"
# Message
EMAILMESSAGE="/home/user/message.txt"
echo "This is email text" >>$EMAILMESSAGE
# Sending email using /bin/mail
/bin/mail -s "$SUBJECT" "$TOEMAIL" < $EMAILMESSAGE

4 Responsesto “Sending mail from bash [Script]”

  1. juriad says:

    better is:

    /bin/mail -s "$SUBJECT" "$TOEMAIL" <<END
    This is email text
    END

    and you don’t need any extra file

  2. stderr says:

    For a quick test of mailserver, I usually use pipe.

    echo "This is a message" | mail -s "$SUBJECT" "$TOEMAIL"

  3. Murdej Ukrutny says:

    or this if you need send program output:


    ( echo "foooo" ; ls ; ps ) | /bin/mail -s "$SUBJECT" "$TOEMAIL"

  4. Ash says:

    sendmail “$TOEMAIL” <<<"Message"

Trackbacks/Pingbacks

  1. Links 21/5/2012: Linux 3.4 Released, Dream Studio 12.04 | Techrights - [...] Sending mail from bash [Script] [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>