Send email from shell

Status
Not open for further replies.

djoole

Contributor
Joined
Oct 3, 2011
Messages
158
Thanks a lot it works fine!

I had tried the mail command, but with echo and not printf.

Thanks again!
 

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
The following will work in both FreeNAS 0.7 and FreeNAS 8.0.1:

Code:
#!/bin/bash

FROM=me@mydomain.com
TO=me@mydomain.com
SUBJECT="`hostname`: Test Email"
BODY=""

PRINTF=/usr/bin/printf
MSMTP=/usr/local/bin/msmtp
MSMTPCONF=/var/etc/msmtp.conf
#...
BODY="Something"
#...
# Send email in FreeNAS 0.7.x
[   -f $MSMTP ] && $PRINTF "From:$FROM\nTo:$TO\nSubject:$SUBJECT\n\n$BODY" | $MSMTP --file=$MSMTPCONF -t
# Send email in FreeNAS 8.0.1+
[ ! -f $MSMTP ] && $PRINTF "$BODY" | mail -s "$SUBJECT" $TO

Thanks for sharing... Does it work also for FreeNAS 8.0.2?
 

William Grzybowski

Wizard
iXsystems
Joined
May 27, 2011
Messages
1,754
Just for clarification.
Since 8.0.2 we do not ship msmtp as part of the installation anymore. Hence the check for msmtp.
From now one should just "mail" for that matter.
 

Milhouse

Guru
Joined
Jun 1, 2011
Messages
564
Just for clarification.
Since 8.0.2 we do not ship msmtp as part of the installation anymore. Hence the check for msmtp.
From now one should just "mail" for that matter.

Pretty sure msmtp wasn't there in 8.0.1 either, and maybe not versions prior to that.

If you are interested in supporting only FreeNAS 8.x then sure, just use mail, but if you are supporting a mixed environment (FreeNAS 0.7.x and 8.0.x) then the example script is what you need to support both platforms with a common code base.
 
Status
Not open for further replies.
Top