Results 1 to 9 of 9

Thread: Sending Mail from bash script to application

  1. #1

    Default Sending Mail from bash script to application

    Dear all,
    I have been suggested to open a new thread on how to send mails to internet addresses.

    My script is using mail to report problems coming from a backup script. The idea is to get an email when a problem happens or when backup finishes succesfully.


    This is how my code looks like concerning the usage of mail


    Code:
    #!/bin/bash
    
    # The scripts should always start with the external devices unmounte.
    # 1. First checks that the device is not mounted (that should be always)
    # 2. Mounts the device
    # 3. Copy the files
    # 4. Unmounts the device
    # 5. Send emails at the moment a failure appears or at the end when everything was succesful
    
    
    SCRIPTNAME="BACKUPOFDATA.SH"
    MOUNTPOINT=/mnt/BckpMeasurements
    MAILADDRESS="hidden@hidden.com"
    
    # 1.
    MOUNT=`mount | grep $MOUNTPOINT`
    if ! test -n "$MOUNT" ; then
            #2.
            mount $MOUNTPOINT 
            # In case the above did not work. I need to send email with message and stop immediately. How I check that the mount was done?
    else
            echo "Device already mounted! Why? Check if something wrong happened with the backup scripts" | mail -r \
                                    $MAILADDRESS -s "Backup Scripts Failed" $MAILADDRESS
            exit
    
    fi
    
    # Do the Copy of The Files
    
    # 3.
    # rsync command goes here
    # if rsync failes send an email to me too
    
    # How to check that rsync returned succesfully
    # End of Copy of the files
    
    
    # 4.
    # Unmount the Device
    umount $MOUNTPOINT
    MOUNT=`mount | grep $MOUNTPOINT`
    if  test -n "$MOUNT" ; then
            echo "Device Failed to be unmounted! Why? Check if something wrong happened with the backup scripts" | mail -r \
                                    $MAILADDRESS -s "Backup Scripts Failed" $MAILADDRESS
            exit
    fi
    
    
    # I want to send me also an email with backup sucessful
    # That should be when rsync returned succesfully and the unmount was too.
    
    # 5.
    echo "Script Name: Data and Time. Rsync finished successfully. Device was also unmounted Successfully. Send a kiss to robin_lista" | mail -r \
                                    $MAILADDRESS -s "Backup Scripts Finished at" $MAILADDRESS

  2. #2
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: Sending Mail from bash script to application

    On 2013-03-11 08:26, alaios wrote:
    >
    > Dear all,
    > I have been 'suggested' (http://tinyurl.com/cs3lh2k) to open a new
    > thread on how to send mails to internet addresses.
    >
    > My script is using mail to report problems coming from a backup script.
    > The idea is to get an email when a problem happens or when backup
    > finishes succesfully.
    >
    >
    > This is how my code looks like concerning the usage of mail


    The only thing we need to know is is this:

    Code:
    
    >   #!/bin/bash
    >
    
    >   MAILADDRESS="hidden@hidden.com"
    
    >   mail -r $MAILADDRESS -s "Backup Scripts Failed" $MAILADDRESS
    Notice that the "From" address and the "To" address are exactly the
    same. So what we need to know now is what happens. Me, I would think it
    will fail because you have no mail body, so 'mail' would probably enter
    into interactive mode for you to type the body. The body should instead
    attached with a pipe from a text file " < body.txt".

    So what happens? If you do not know, look at the email log :-)

    --
    Cheers / Saludos,

    Carlos E. R.
    (from 11.4, with Evergreen, x86_64 "Celadon" (Minas Tirith))

  3. #3
    Join Date
    Aug 2010
    Location
    Chicago suburbs
    Posts
    12,624
    Blog Entries
    3

    Default Re: Sending Mail from bash script to application

    Quote Originally Posted by robin_listas View Post
    Me, I would think it
    will fail because you have no mail body, so 'mail' would probably enter
    into interactive mode for you to type the body.
    It is legal (i.e. allowed) for mail to have an empty body.
    openSUSE Leap 15.1; KDE Plasma 5;
    testing Leap 15.2Alpha

  4. #4

    Default Re: Sending Mail from bash script to application

    > It is legal (i.e. allowed) for mail to have an empty body.

    Yes, or at least empty to humans. Sending mail this way is pretty common:

    mail -s 'subject goes here' ab@no-mx.forums.novell.com < /dev/null

    Google found several hits and this one specifically shows some good examples:

    http://www.cyberciti.biz/faq/linux-u...l-attachments/

    Good luck.

  5. #5
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: Sending Mail from bash script to application

    On 2013-03-11 15:06, nrickert wrote:
    >
    > robin_listas;2533713 Wrote:
    >> Me, I would think it
    >> will fail because you have no mail body, so 'mail' would probably enter
    >> into interactive mode for you to type the body.

    > It is legal (i.e. allowed) for mail to have an empty body.


    Yes, of course, as long as the mail command allows for it. See 'ab'
    response, how he does a dev/null redirect.

    --
    Cheers / Saludos,

    Carlos E. R.
    (from 11.4, with Evergreen, x86_64 "Celadon" (Minas Tirith))

  6. #6

    Default Re: Sending Mail from bash script to application

    Hi,
    concerning security issues. I am using postfix, at least this is what I think mail command uses to send emails. My firewall is enabled and the only enables port is the ssh port.

    Is that enough for keeping outsiders?

    Regards
    Alex
    Quote Originally Posted by robin_listas View Post
    On 2013-03-11 15:06, nrickert wrote:
    >
    > robin_listas;2533713 Wrote:
    >> Me, I would think it
    >> will fail because you have no mail body, so 'mail' would probably enter
    >> into interactive mode for you to type the body.

    > It is legal (i.e. allowed) for mail to have an empty body.


    Yes, of course, as long as the mail command allows for it. See 'ab'
    response, how he does a dev/null redirect.

    --
    Cheers / Saludos,

    Carlos E. R.
    (from 11.4, with Evergreen, x86_64 "Celadon" (Minas Tirith))

  7. #7
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    11,282
    Blog Entries
    2

    Default Re: Sending Mail from bash script to application

    Looks to me should work.

    Besides your script,
    Make sure you have Postfix or Sendmail installed, default configuration should permit senind only from your local machine. No FW setting is required for protection.

    Secure your script so it cannot be invoked by anything but what you need configuring permissions.
    Secure your script so it's not publicly visible.

    Most public mailservers today are configured to block messages from any but those with DNS reverse lookup records. For that reason although you're running a local SMTP server, you will likely need to still route through an authorized Smart SMTP relay unless you're sending to a private mailserver you own that you can configure.

    HTH,
    TSU

  8. #8

    Default Re: Sending Mail from bash script to application

    With regard to mail unless you reconfigure Postfix to listen on all
    addresses and open TCP 25 (or another relevant port for Postfix) in the
    firewall it should be pretty tricky for somebody to do anything bad to
    this box. You're just sending, not receiving. Even legitimate folks
    trying to reply to these e-mails you send from the command line will
    probably fail unless your DNS mail server handles this sender's address
    for you.

    Good luck.

  9. #9
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: Sending Mail from bash script to application

    On 2013-03-12 19:36, tsu2 wrote:

    > Most public mailservers today are configured to block messages from any
    > but those with DNS reverse lookup records. For that reason although
    > you're running a local SMTP server, you will likely need to still route
    > through an authorized Smart SMTP relay unless you're sending to a
    > private mailserver you own that you can configure.


    Yes, that's the tricky part.

    For example, I teach my postfix to know that when I send an email with a
    gmail "from" address it has to contact gmail with the proper
    login/password to rely my email to its destination - the same as if I
    were using kmail or thunderbird to send email.

    I can explain that another day. I have to travel tomorrow and I also
    have a toothache, so I don't feel inclined to write a lot till the
    dentist does his job (argh!). In fact, I'm waiting for the analgesic to
    make effect and I go to sleep.


    Or you may have an ISP that accepts you send through them all your
    emails. This is the traditional rely setup. I have never seen such an
    ISP, though: I have to use a separate login/pass for each from address.
    Postfix supports both modes.


    Postfix is, of course, perfectly capable of sending any email directly
    to the destination without help from any other server, but... but no
    destination will accept those emails any longer. Not unless you have a
    properly registered and configured domain (with a fixed address). Your
    email will be rejected, you will not manage to connect, etc.


    Another method is to configure the program 'mail' (aka 'mailx') to use
    an external SMTP server and identify to it. I have never done this, but
    it is documented in its manual page. This is basically the same most
    people do with thunderbird, evolution, kmail...

    --
    Cheers / Saludos,

    Carlos E. R.
    (from 11.4, with Evergreen, x86_64 "Celadon" (Minas Tirith))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •