Jump to content

Sending e-mail from a script


Recommended Posts

Hello,

 

I am trying to get rsync to copy files to an external hard drive, log everything to a file, and then e-mail me that file after the operation has complete.  So far rsync copy and writing to log file work fine, but I can not get the script to e-mail me the log file.  I have set up the SMTP settings in unraid 6.1 notifications, and the test function does send an e-mail properly.  Below is the script I am running/ working on.

 

#!/bin/bash

#File and directory backup script
# Andrew Robinson 2015

#Each line is set up to run rsync on a specific directory and back up to external HDD.  
#after sync completed, log file with results is writen
#then e-mailed off to me

echo "Backing up data directories to removeable hard drive"
rsync -avh --delete /mnt/user/ /mnt/disks/ST4000DM000_1F2168_Z300HVNP/ --log-file=/var/log/backup_rsync.log
echo "Backup complete"
cat /var/log/backup_rsync.log | mail -s "backup_rsync.log" [email protected]

 

Thanks

Link to comment

If you want to mail with the mail command then mailx slackware package needs to be installed.

This isn't done by default because LT wants people to use the notification system.

 

If this script runs in cron, stdout and stderr normally get mailed to whatever you've configured in the mail settings page.

Within the script you can do a cat of the log file and will get mailed with the job's output.

I.E. you should not need the cat logfile | mail -s 'subject'

You can cat the logfile within the script and it will be included with job output via cron.

 

To send it as a separate log, you'll need to install mailx or call SSMTP directly.

 

http://linux.die.net/man/8/ssmtp

 

Here's a quick example of how to do it via ssmtp directly. Note I have the -v verbose switch on, normally that would not be included.

 

root@unRAIDb:/tmp# ls -l > logfile

 

root@unRAIDb:/tmp# cat example_ssmtp.sh

#!/bin/bash

 

cat - logfile <<-EOF | ssmtp -v [email protected]

Subject: logfile for `date`

EOF

 

root@unRAIDb:/tmp# ./example_ssmtp.sh

[<-] 220 hosting.empoweringmedia.net ESMTP Exim 4.84 Wed, 02 Sep 2015 13:38:52 -0400

[->] EHLO unRAIDb

[<-] 250 HELP

[->] STARTTLS

[<-] 220 TLS go ahead

[->] EHLO unRAIDb

[<-] 250 HELP

[->] AUTH LOGIN

[<-] 334 ************

[->] **********

[<-] 334 ******

[<-] 235 Authentication succeeded

[->] MAIL FROM:<[email protected]>

[<-] 250 OK

[->] RCPT TO:<[email protected]>

[<-] 250 Accepted

[->] DATA

[<-] 354 Enter message, ending with "." on a line by itself

[->] Received: by unRAIDb (sSMTP sendmail emulation); Wed, 02 Sep 2015 13:38:51 -0400

[->] From: "Console and webGui login account" <[email protected]>

[->] Date: Wed, 02 Sep 2015 13:38:51 -0400

[->] Subject: logfile for Wed Sep  2 13:38:51 EDT 2015

[->] total 4

[->] -rwxrw-rw- 1 root root  96 Sep  2 13:38 example_ssmtp.sh*

[->] -rw-rw-rw- 1 root root  0 Sep  2 13:38 logfile

[->] drwxr-xr-x 4 root root  80 Aug 26 21:03 notifications/

[->] drwxr-xr-x 2 root root 220 Aug 31 00:10 plugins/

[->]

[->] .

[<-] 250 OK id=************

[->] QUIT

[<-] 221 hosting.empoweringmedia.net closing connection

Link to comment

I've update my script and it now seems to be working, but I'm getting the error message

 

ssmtp: 550 5.7.60 SMTP; Client does not have permissions to send as this sender

 

I'm not sure how to fix this error.  The sending e-mail address in my smtp settings (notifications) is the same e-mail address I have set up in this script.

Link to comment

I've update my script and it now seems to be working, but I'm getting the error message

 

ssmtp: 550 5.7.60 SMTP; Client does not have permissions to send as this sender

 

I'm not sure how to fix this error.  The sending e-mail address in my smtp settings (notifications) is the same e-mail address I have set up in this script.

 

It is recommended to use the notify command to send notifications (including email), and not use another mail or smtp application.

 

Link to comment

I've update my script and it now seems to be working, but I'm getting the error message

 

ssmtp: 550 5.7.60 SMTP; Client does not have permissions to send as this sender

 

I'm not sure how to fix this error.  The sending e-mail address in my smtp settings (notifications) is the same e-mail address I have set up in this script.

 

It is recommended to use the notify command to send notifications (including email), and not use another mail or smtp application.

 

 

 

root@unRAIDb:/tmp# /usr/local/emhttp/webGui/scripts/notify   

notify [-e "event"] [-s "subject"] [-d "description"] [-i "normal|warning|alert"] [-m "message"] [-x] [-t] [add]

  create a notification

  use -e to specify the event

  use -s to specify a subject

  use -d to specify a short description

  use -i to specify the severity

  use -m to specify a message (long description)

  use -x to create a single notification ticket

  use -t to force send email only (for testing)

  all options are optional

 

notify init

  Initialize the notification subsystem.

 

notify smtp-init

  Initialize sendmail configuration (ssmtp in our case).

 

notify get

  Output a json-encoded list of all the unread notifications.

 

notify archive file

  Move file from 'unread' state to 'archive' state.

 

notify does not look like it's capable of accepting stdin and sending that data.

i.e. as in sending the log with a subject.

Link to comment

I've update my script and it now seems to be working, but I'm getting the error message

 

ssmtp: 550 5.7.60 SMTP; Client does not have permissions to send as this sender

 

I'm not sure how to fix this error.  The sending e-mail address in my smtp settings (notifications) is the same e-mail address I have set up in this script.

 

for the ssmtp error,

Looks like the smtp server requires more then mine did.

 

Try adding a From: as it normally defaults to the root user.

 

#!/bin/bash

 

ls -l > logfile

 

cat - logfile <<-EOF | ssmtp -v [email protected]

From: [email protected]

Subject: logfile for `date`

EOF

 

 

root@unRAIDb:/tmp# ./example_ssmtp.sh

[<-] 220 hostingservice.empoweringmedia.net ESMTP Exim 4.84 Sat, 05 Sep 2015 09:47:43 -0400

[->] EHLO unRAIDb

[<-] 250 HELP

[->] STARTTLS

[<-] 220 TLS go ahead

[->] EHLO unRAIDb

[<-] 250 HELP

[->] AUTH LOGIN

[<-] 334 *********

[->] ***************************

[<-] 334 **********

[<-] 235 Authentication succeeded

[->] MAIL FROM:<[email protected]>

[<-] 250 OK

[->] RCPT TO:<[email protected]>

[<-] 250 Accepted

[->] DATA

[<-] 354 Enter message, ending with "." on a line by itself

[->] Received: by unRAIDb (sSMTP sendmail emulation); Sat, 05 Sep 2015 09:47:42 -0400

[->] Date: Sat, 05 Sep 2015 09:47:42 -0400

[->] From: [email protected]

[->] Subject: logfile for Sat Sep  5 09:47:42 EDT 2015

[->] total 4

[->] -rwxrw-rw- 1 root root 139 Sep  5 09:47 example_ssmtp.sh

[->] -rw-rw-rw- 1 root root  0 Sep  5 09:47 logfile

[->] drwxr-xr-x 4 root root  80 Sep  4 14:16 notifications

[->] drwxr-xr-x 2 root root 280 Sep  4 23:56 plugins

[->] drwx------ 2 root root  60 Sep  4 23:47 ssh-3GhthFJM2r

[->] drwx------ 2 root root  60 Sep  4 23:30 ssh-mYwUqW0iyo

[->]

[->] .

[<-] 250 OK id=********************-Rx

[->] QUIT

[<-] 221 hostingservice.empoweringmedia.net closing connection

Link to comment

I've update my script and it now seems to be working, but I'm getting the error message

 

ssmtp: 550 5.7.60 SMTP; Client does not have permissions to send as this sender

 

I'm not sure how to fix this error.  The sending e-mail address in my smtp settings (notifications) is the same e-mail address I have set up in this script.

 

for the ssmtp error,

Looks like the smtp server requires more then mine did.

 

Try adding a From: as it normally defaults to the root user.

 

#!/bin/bash

 

ls -l > logfile

 

cat - logfile <<-EOF | ssmtp -v [email protected]

From: [email protected]

Subject: logfile for `date`

EOF

 

 

root@unRAIDb:/tmp# ./example_ssmtp.sh

[<-] 220 hostingservice.empoweringmedia.net ESMTP Exim 4.84 Sat, 05 Sep 2015 09:47:43 -0400

[->] EHLO unRAIDb

[<-] 250 HELP

[->] STARTTLS

[<-] 220 TLS go ahead

[->] EHLO unRAIDb

[<-] 250 HELP

[->] AUTH LOGIN

[<-] 334 *********

[->] ***************************

[<-] 334 **********

[<-] 235 Authentication succeeded

[->] MAIL FROM:<[email protected]>

[<-] 250 OK

[->] RCPT TO:<[email protected]>

[<-] 250 Accepted

[->] DATA

[<-] 354 Enter message, ending with "." on a line by itself

[->] Received: by unRAIDb (sSMTP sendmail emulation); Sat, 05 Sep 2015 09:47:42 -0400

[->] Date: Sat, 05 Sep 2015 09:47:42 -0400

[->] From: [email protected]

[->] Subject: logfile for Sat Sep  5 09:47:42 EDT 2015

[->] total 4

[->] -rwxrw-rw- 1 root root 139 Sep  5 09:47 example_ssmtp.sh

[->] -rw-rw-rw- 1 root root  0 Sep  5 09:47 logfile

[->] drwxr-xr-x 4 root root  80 Sep  4 14:16 notifications

[->] drwxr-xr-x 2 root root 280 Sep  4 23:56 plugins

[->] drwx------ 2 root root  60 Sep  4 23:47 ssh-3GhthFJM2r

[->] drwx------ 2 root root  60 Sep  4 23:30 ssh-mYwUqW0iyo

[->]

[->] .

[<-] 250 OK id=********************-Rx

[->] QUIT

[<-] 221 hostingservice.empoweringmedia.net closing connection

 

That seems to have resolved the issue.  Thank you very much!

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...