Schwabinger
Posts: 40
Joined: Sun Jan 13, 2013 2:56 pm

sending email from the command line

Sat Feb 02, 2013 11:45 am

Searching for a simple way to send email messages from the command line (or when a button on my remote is pressed) I learned about "ssmtp".

Once installed you can do something like

Code: Select all

echo "this is a test" | mail -s Subject [email protected]
which will send an email to account [email protected] using the subject "Subject" and "this is a test" as mail text.

Just install ssmtp first:
http://www.cyberciti.biz/tips/linux-use ... thost.html

I use ssmtp with my "Headless RaspRadio": Pressing a button on my remote sends me an email with the title of the song that is just playing ....

http://www.raspberrypi.org/phpBB3/viewt ... 35&t=29550

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: sending email from the command line

Sat Feb 09, 2013 12:52 am

Hi!

Thanks for sharing this!

I followed the guide (http://www.cyberciti.biz/tips/linux-use ... thost.html). When I tried to send an email, I got an authentication error. It turned out that instead of port 587 you need to use port 465 with Google. That's solved now, but my mails are still not sent out.

I send the mail from command line (sudo echo "Test3" | mail -s Subject [email protected]), I get no error message right away, but no success message either. After a few minutes an error pops up: "send-mail: Connection lost in middle of processing"

I've searched the internet a lot about this but I did not find a solution. Any idea what could be wrong?
Andras
http://iqjar.com

jamiesk
Posts: 95
Joined: Mon Nov 26, 2012 8:48 pm
Location: Ipswich, Suffolk, England, UK.

Re: sending email from the command line

Sun Feb 10, 2013 1:32 am

You have to be careful about the domain you are sending FROM. I use exim as the Mail Transport, so I am unable to help, but I do not use smarthost. As spammers use bad FROM fields and use badly configured devices to route their mail.
Pi1 (Nov 2012 loft)= 1KW immersion controller for Solar panel
Pi2 (Jan 2013 living room)=Play thing
Pi3 (Feb 2013 mobile)= Play thing with Tandy Ladder board,breakout board,Nokia display
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=28193

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: sending email from the command line

Sun Feb 10, 2013 9:05 am

My /etc/ssmtp/ssmtp.conf contains this:

Code: Select all

[email protected]
mailhub=smtp.gmail.com:465
hostname=raspberrypi
FromLineOverride=YES
[email protected]
AuthPass=mypass
UseSTARTTLS=YES
And my /etc/ssmtp/revaliases contains this:

Code: Select all

root:[email protected]:mail.google.com:465
Is there anything missing or not set correctly?
I still keep getting: "send-mail: Connection lost in middle of processing" :(

Thank you!
Andras
http://iqjar.com

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: sending email from the command line

Sun Feb 10, 2013 9:28 am

IT seems that the problem may be that I've tried using port 465 instead of 587. In the google help it says that 465 is SMTP with SSL and 587 is SMTP with TLS (whatever those are :) ). So now I'm back to 587, but with this one I get "Authorization failed (535 5.7.1 http://support.google.com/mail/bin/answ ... swer=14257 a1sm31024536eep.2 - gsmtp" :(
Andras
http://iqjar.com

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: sending email from the command line

Sun Feb 10, 2013 9:34 am

Fixed it... it was a very stupid mistake... a missing character from my username specified in the ssmtp.conf :|
Andras
http://iqjar.com

User avatar
Un4Seen
Posts: 330
Joined: Wed Oct 31, 2012 8:43 am
Location: Cluj-Napoca, Romania
Contact: Website

Re: sending email from the command line

Mon Feb 11, 2013 11:21 pm

Hi!

Should anybody be interested, I wrote a detailed guide/tutorial about configuring the Raspberry Pi to be able to send emails from command line, from shell scripts, and PHP. You can find it here:

http://iqjar.com/jar/sending-emails-fro ... pberry-pi/
Andras
http://iqjar.com

krashilili
Posts: 1
Joined: Fri Oct 18, 2013 4:07 pm

Re: sending email from the command line

Mon Feb 17, 2014 6:22 am

I had the same problem before and the way to solve it is kind of a trick. The bug is in the conf file. You shouldn't have any space after the equation symbol. For example, "UseSTARTTLS=YES" is all right. If you write "UseSTARTTLS= YES", then you'll get the problem. Hope you good luck and it works!

Vesco
Posts: 8
Joined: Wed Apr 15, 2015 3:02 pm

Re: sending email from the command line

Thu Mar 10, 2016 4:43 am

Hi all,

Thank you for everything you have shared.
I have followed all instruction in this post and I found myself trouble because everytime i try to send email, I always get this error message:
ssmtp: Authorization failed (535 5.7.8 https://support.google.com/mail/answer/14257 vy6sm1612868pac.38 - gsmtp)
I fill my configuration file with this:

Code: Select all

root=postmaster
mailhub=smtp.gmail.com:587
hostname=Raspberry-Test
AuthUser=<gmail_address>@gmail.com
AuthPass=<gmail_password>
FromLineOverride=YES
UseSTARTTLS=YES
I have changed those gmail_address and gmail_password into my real user name and password. I have alse turn on less secure option in my gmail. Can some one please tell me what i miss.

Thank you very much.

Regards,
Dimas

ghans
Posts: 7871
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: sending email from the command line

Thu Mar 10, 2016 11:24 am

Do you use 2 factor authentication ?

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

Vesco
Posts: 8
Joined: Wed Apr 15, 2015 3:02 pm

Re: sending email from the command line

Thu Mar 10, 2016 3:39 pm

Hi,

No, i don't use that feature. I tried to send email using phyton script and succeed but I still curious why i cannot send email using command line.

Regards,
Dimas

mx5jon
Posts: 8
Joined: Sat Feb 09, 2013 9:19 pm

Re: sending email from the command line

Sat Apr 02, 2016 6:09 pm

If you use gmail I've found that you have to go to gmail:

Go to Sign in & security on the left hand side, select Sign in & security, scroll down the right hand side and select Allow less secure Apps.

crisspi
Posts: 1
Joined: Thu Apr 14, 2016 9:57 am

Re: sending email from the command line

Thu Apr 14, 2016 10:01 am

that I email sending SMTP server with the following error appear: "mail: can not send message: Process has exited with non-zero status"
and in the / var / log I have the following information:
Unable to connect to 'smtp.gmail.com' port 587.
Can not open smtp.gmail.com:587
thank you for helping me this is urgent :)

boyddensmore
Posts: 1
Joined: Wed Jun 01, 2016 7:12 pm

Re: sending email from the command line

Wed Jun 01, 2016 7:15 pm

Not sure if this'll help you or not, but my password had a hash (#) in it. I changed my gmail password to one which does not have a # and I no longer get this error.

Now I get no error and no email on the other end, but that's another matter.

markfickett
Posts: 1
Joined: Mon Dec 19, 2016 4:06 am

Re: sending email from the command line

Mon Dec 19, 2016 4:30 am

I was never able to get ssmtp to work, for example following this guide. However, I was able to use SMTP either via openssl (interacting in a similar way to telnet, following this reference), or using the Python smtplib. I'll leave those examples here. (In my application I could have used command-line mail, but Python will actually be even easier.)

Code: Select all

$ openssl s_client -debug -starttls smtp -crlf -connect smtp.gmail.com:587
EHLO raspberrypi
STARTTLS
AUTH LOGIN dXNlcm5hbWU=
cGFzc3dvcmQ=
MAIL FROM: <[email protected]>
RCPT TO: <[email protected]>
DATA
Subject: Testing SMTP!
Hello!
Testing how to negotiate SMTP with GMail.
.

In the above example, in "AUTH LOGIN dXNlcm5hbWU=" I'm providing my username; as an example, for [email protected] it would be just pihacker. It's base64 encoded, and you can base64 encode a string by opening up a javascript console and typing btoa('pihacker'). Then the next line is the base64-encoded password by itself.

In the Python below, the username and password are provided in plaintext.

Code: Select all

$ python
>>> import smtplib
>>> s = smtplib.SMTP('smtp.gmail.com', 587)
>>> s.starttls()
(220, '2.0.0 Ready to start TLS')
>>> s.ehlo()
(250, 'smtp.gmail.com at your service, [209.6.196.37]\nSIZE 35882577\n8BITMIME\nAUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8')
>>> s.login('pihacker', 'hunter42')
(235, '2.7.0 Accepted')
>>> s.sendmail('[email protected]', '[email protected]', 'Subject: Test from PySMTP\nHello! How is it going?')
{}

tpylkko
Posts: 381
Joined: Tue Oct 14, 2014 5:21 pm

Re: sending email from the command line

Mon Dec 19, 2016 7:19 am

You should not post your user name and password on a public forum! Anyone that read that now has your crendentials

One problem not mentioned by people here is that if you store your password without encryption on your pi, there is the risk that it is stolen. Either remotely by hacker or if you loose the sd-card

There is a python module called yagmail (for gmail and smtpbased mail) that is ridiculously easy to use but that uses your keyring so thay the password is not put into any file locally.

https://github.com/kootenpv/yagmail

Another solution is to make a gmail account only for your pi, so that if it stolen, you can just forget it.

scign
Posts: 2
Joined: Thu Jan 08, 2015 9:07 am

Re: sending email from the command line

Mon Jan 09, 2017 6:07 pm

LOL clearly not a bash.org fan :-) hunter42 / hunter2 ... definitely made my day X,-D
Check out http://knowyourmeme.com/memes/hunter2 for background.

PS: thanks for the good link with the overview of setting up mail from the raspi.

I set one up with email about a year ago but can't remember how I did it. I never got emails from it until recently when I set up a RAID array on it and just started getting status emails from mdstat via root but don't know how!

I'm going to be fiddling with another pi this evening to set up emails and I'll let you know how that goes. Incidentally, I also use Twilio to allow the pi to send me text messages. Check that out as a possible alternative :-) Search Google for twilio and raspberry pi. LOTS of results!

ratatouille
Posts: 14
Joined: Thu Dec 29, 2016 11:17 am

Re: sending email from the command line

Mon Jan 08, 2018 7:17 pm

Schwabinger wrote:
Sat Feb 02, 2013 11:45 am
Searching for a simple way to send email messages from the command line (or when a button on my remote is pressed) I learned about "ssmtp".

Once installed you can do something like

Code: Select all

echo "this is a test" | mail -s Subject [email protected]
which will send an email to account [email protected] using the subject "Subject" and "this is a test" as mail text.

Just install ssmtp first:
http://www.cyberciti.biz/tips/linux-use ... thost.html

I use ssmtp with my "Headless RaspRadio": Pressing a button on my remote sends me an email with the title of the song that is just playing ....

http://www.raspberrypi.org/phpBB3/viewt ... 35&t=29550
I followed the instructions and installed ssmtp. But I get an error bash: mail: Kommando nicht gefunden when I execute echo "this is a test" | mail -s Subject [email protected]

There is a symlink from sendmail to ssmtp in /usr/sbin but where is mail command?

Regards

Andreas

pcmanbob
Posts: 6669
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: sending email from the command line

Tue Jan 09, 2018 11:08 am

You need to install ssmtp and mailutils

http://www.raspberry-projects.com/pi/so ... end-emails
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Networking and servers”