Send email from shell

Status
Not open for further replies.

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
Hello all,
I installed the latest FreeNAS 8 release in a test machine and I'm scratching my head on how can I send an email from shell.

I correctly configured all the mail fields on the GUI and I can send/receive the test mail, but I cannot figure how to manually send it from shell.

Could you please advice?

This is the mail config setup by GUI:
Code:
freenas# cat /usr/local/etc/msmtprc
account default
host smtp.gmail.com
port 25
protocol smtp
from root@freenas.local
syslog LOG_MAIL
tls on
tls_certcheck off
tls_starttls off
auth login
user ********
password ********


Thanks
Lucas
 

ProtoSD

MVP
Joined
Jul 1, 2011
Messages
3,348
You should be able to do 'mail -I root' which interactively lets you type a message, terminated by control-D, which then gets sent to root, which then is supposed to be redirected to your Gmail account like the TEST message.... but, you're right it doesn't work, it tries, and spits out an error message on the console after a little while saying 'the server sent an empty reply'.

BTW, this is what also happens if you get a disk error and email is supposed to notify you. You get an error in the log about an unspecified error prevented the message from being sent, and no warning about the disk failure gets sent. It seems like the 'TEST' message is just for 'demonstration purposes'?

If someone else doesn't have an answer, I'll play around with it a little more and see if I can't figure it out.
 

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
Well, my goal is code a simple bash script to check some system parameters and expecially disk state, and send them via mail. That's why I would like to know how to send mail using bash in non interactive mode.

I tried the following command (followed by CTRL-D):
sendmail -Am -v -flocalusername -- ******x@gmail.com

And that's the result: :(
freenas# msmtp: TLS handshake failed: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
msmtp: could not send mail (account default from /usr/local/etc/msmtprc)

In test mail the only way to send mail was enable TLS, maybe this protocol doesn't work on shell! :confused:

So I removed the TLS AUTH and tried again to send mail, that's the result:
freenas# msmtp: the server does not support authentication
msmtp: could not send mail (account default from /usr/local/etc/msmtprc)

Please help, I'm gonna crazy! :mad:
 

ProtoSD

MVP
Joined
Jul 1, 2011
Messages
3,348
I think there is a bug using TLS, there's an open ticket for it. Have you tried using SSL? (and port 465)?


Also, I was just suggesting the interactive method for testing, I realized you wanted to do a script.
 

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
Have you tried using SSL? (and port 465)?
Sure, from GUI it works, and the test mail is sent/received. Also, I got no error message from shell (using: mail -I root), but no mail received! :(

Also, I was just suggesting the interactive method for testing, I realized you wanted to do a script.
YEP! I'm using the interactive mode just for testing, then I'll switch to other mode for script purpose! ;)
 

Tekkie

Patron
Joined
May 31, 2011
Messages
353
Not to hijack the thread but the error
msmtp: TLS handshake failed: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
msmtp: could not send mail (account default from /usr/local/etc/msmtprc)
is what I get from the UI as well, no matter if I use SSL or TLS, port 465 or 587.

However the really funny thing is that in my gmail account I see failed to send emails from the system in my inbox... looks like another incomplete feature in FreeNAS8. :(
 

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
What I don't understand is why is so hard to send an email, at least, for ZFS Pool degraded?
I don't want to check my pool every day!!!! :(
 

Tekkie

Patron
Joined
May 31, 2011
Messages
353
I believe it has to do with the fancy FreeNAS8 UI / DB frontend not passing through all value changes to the underlying config files for the various services/daemons.
 

Scrat

Dabbler
Joined
Jul 25, 2011
Messages
12
Hi!

I use msmtp to send my daily rsync report to two different mail account at the end of my rsync script:

Code:
/usr/local/bin/msmtp --file=${msmtp_config:-"/etc/local/msmtprc"} user1@mail.de user2@mail.de < /mnt/NASVol0/Scripts/tmp/rsync-mail


I had to set port 587 and SSL in the GUI. My /etc/local/msmtprc:

Code:
account default
host smtp.***.de
port 587
protocol smtp
from ***@***.de
syslog LOG_MAIL
tls on
tls_certcheck off
tls_starttls on
tls_force_sslv3 on
auth login
user ***@***.de
Passwort ***


It runs every morning, without any problem. I hope this helps.

Greets, Peter

P.S. Runs only in sh and bash, in csh I get the error Bad : modifier in $ (-). (I'm not a programmer :confused: )
 

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
Thanks for your feedback scrat, it works!!!
...and finally I can send mails from shell (sh), but I recevice them with empty Subject and empty Body! :(

Could you please explain what's the content of /mnt/NASVol0/Scripts/tmp/rsync-mail file? Maybe the Subject & Body are there and in a specific format?

EDIT: YEP! I was right, the rsync-mail contains a specific format for subject and body:

Subject: This is a mail subject

Body mail with all the text
Line 2
Line 3

End of Mail

Thanks again scrat, you're my hero! :D

This post should be sticky! ;)
 

Scrat

Dabbler
Joined
Jul 25, 2011
Messages
12
Subject and body are in the textfile, in this case it's a log of my daily rsync task:

Code:
Subject: Rsync report from Linux-Server on Tue Jul 26 04:49:04 CEST 2011

2011/07/26 22:30:04 [2574] receiving file list
2011/07/26 22:30:10 [2574] 66962 files to consider
2011/07/26 22:30:10 [2575] .d..t...... ./
[...]


This is the part of my script, where subject and rsync log are written to the temporary Textfile (variables are defined earlier in the script):

Code:
echo "Subject: Rsync report from ${MACHINE} on `date`" > /mnt/NASVol0/Scripts/tmp/rsync-mail
echo >> /mnt/NASVol0/Scripts/tmp/rsync-mail
cat ${LOGFILE} >> /mnt/NASVol0/Scripts/tmp/rsync-mail
/usr/local/bin/msmtp --file=${msmtp_config:-"/etc/local/msmtprc"} ***@***.de ***@***.de < /mnt/NASVol0/Scripts/tmp/rsync-mail


In the same way I generate and mail my daily status report similar to FN7, which is missing in FN8 b4 as far as I know.

Greets, Peter

EDIT: Too late, you found it yourself while I was typing. :smile:
 

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
Not very important, but is there a way to send a mail with BOLD text and specific font?
 

ProtoSD

MVP
Joined
Jul 1, 2011
Messages
3,348
Lucas, you indicated you got this working, did you get it working with Gmail??
Would you post your msmtp config? (with the appropriate stuff x'd out of course!)
It still doesn't work for me, I can get the test message from the GUI only.
 

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
Yes, for me it's working with Gmail.

This is my msmtp config:
freenas# cat /etc/local/msmtprc
account default
host smtp.gmail.com
port 587
protocol smtp
from root@freenas.local
syslog LOG_MAIL
tls on
tls_certcheck off
tls_starttls on
auth login
user name.surname
password *********

This is the config from the GUI:
de100.png


And don't forget to update also the admin user (Account => Users => View all users => Change Admin user):
fclfgi.png


Warning, if you change the config from the GUI, FreeNAS will overwrite also the msmtp config file. Also, if you change only the msmtp config file, after reboot, again FreeNAS will overwrite the file.
For that reason, best way is change the parameters on the GUI, THEN change the msmtp config (if needed).

This is my simple script to check the pool (This is just a simple test, I'll update later with all other informations):

Code:
freenas# cat mail.sh 
#!/bin/sh
cd /mnt/tvixhd1
zpool status -x > /mnt/tvixhd1/scripts/tmp/zpoolstatus
DEGRADED=$(cat /mnt/tvixhd1/scripts/tmp/zpoolstatus | grep -cw DEGRADED)

if [ "$DEGRADED" > 0 ]; then
   echo "Subject: ATTENZIONE!!! ZPOOL in stato DEGRADED" > /mnt/tvixhd1/scripts/tmp/tmpmail
   printf "\nATTENZIONE RAID-Z e ZPOOL in stato DEGRADED..." >> /mnt/tvixhd1/scripts/tmp/tmpmail
   echo "CONTROLLARE SUBITO IL VOLUME LOGICO E LO STATO DEI DISCHI" >> /mnt/tvixhd1/scripts/tmp/tmpmail
   printf "\n" >> /mnt/tvixhd1/scripts/tmp/tmpmail
   cat /mnt/tvixhd1/scripts/tmp/zpoolstatus >> /mnt/tvixhd1/scripts/tmp/tmpmail
   /usr/local/bin/msmtp --file=${msmtp_config:-"/etc/local/msmtprc"} ******.******@gmail.com < /mnt/tvixhd1/scripts/tmp/tmpmail
fi
rm -f /mnt/tvixhd1/scripts/tmp/tmpmail
 

ProtoSD

MVP
Joined
Jul 1, 2011
Messages
3,348
Lucas,

Thanks for the nice details!

I think FreeNAS likes it better when you speak Italian to it!

Everything is the same for me except the port number. I tried port 587, but the test message from the GUI could not be sent. I changed it back to 465 and the test message worked again. I think the port number is different for you there.

I'm still not getting it to work, I get no email. I just rebooted to see if that helps even though I know I shouldn't need to.

UPDATE: rebooting didn't help... of course. The only thing I can tell that is different is that you said you were running 8.0 Release, I'm running 8.01 beta 4.
 

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
With port 587 I receive from GUI the following error:
Your test email could not be sent: [Errno 1] _ssl.c:499: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Btw, it works from the shell, so it's ok for me.

Don't forget to open the firewall (if you have one), I had to create a rule for port 587 otherwise the mail wasn't sent. Also disable the firewall software (I'm running VMWare under Windows Seven and again the firewall software denied the traffic on port 587).

After reboot check again the msmtprc file...
 

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
The only thing I can tell that is different is that you said you were running 8.0 Release, I'm running 8.01 beta 4.[/B]

Oh Yeah! That's why it doesn't work for you!
Surfing the web I read from many sources that from release 8.0.1b3 there is some problem sending mail with TLS and SSL....

Switch back to 8.0! :D
 

Tekkie

Patron
Joined
May 31, 2011
Messages
353
Lucas, you indicated you got this working, did you get it working with Gmail??
Would you post your msmtp config? (with the appropriate stuff x'd out of course!)
It still doesn't work for me, I can get the test message from the GUI only.
I have an even weirder situation, I can't send the test email but this morning I found 2 emails in my Gmail account that were sent from my account to 'root' from my FreeNAS system, however the delivery failed because it was sent to 'root', its better than it was before at least. :)
 

Lucas Rey

Contributor
Joined
Jul 25, 2011
Messages
180
I have an even weirder situation, I can't send the test email but this morning I found 2 emails in my Gmail account that were sent from my account to 'root' from my FreeNAS system, however the delivery failed because it was sent to 'root', its better than it was before at least. :)

Yep! I have similar behaviour.
If I run the script manually, I receive one "correct" mail.

If I use the crontab, the script runs and send one "correct" mail, but at the same time I receive another mail with subject: "Cron <root@freenas> /mnt/tvixhd1/mail.sh"

And following body:
Delivery to the following recipient failed permanently:

you@domain.com

Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the recipient domain. We recommend contacting the other email provider for further information about the cause of this error. The error that the other server returned was: 505 505 5.0.0 User unknown (state 14).
> ----- Original message ----- Received: by 10.217.2.202 with SMTP id p52mr5742256wes.28.1311755706904; Wed, 27 Jul 2011 01:35:06 -0700 (PDT) Return-Path: <*********x@gmail.com> Received: from localhost (******xx.it [******.49]) by mx.google.com with ESMTPS id ******x (version=TLSv1/SSLv3 cipher=OTHER); Wed, 27 Jul 2011 01:35:06 -0700 (PDT) Message-ID: <***xx@mx.google.com> Date: Wed, 27 Jul 2011 01:35:06 -0700 (PDT) From: root <******@gmail.com> To: root Subject: Cron <root@freenas> /mnt/tvixhd1/mail.sh X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/root> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=root> X-Cron-Env: <USER=root> zpool: not found


you@domain.com ??????

WTF take this address????!!!???
 
Status
Not open for further replies.
Top