Thursday, September 26, 2013

Sending Mail From a Raspberry PI

Remember, one of the reasons I got the Pi was to be able to send mail to myself when something bad happened?  Finally got around to looking at that.  What a royal red pain in the butt!  First, regular mail is a configuration nightmare that I gave up on after looking at it for a while.  Then, I went looking for a simple client that could be scripted or had some kind of api I could use...no luck.  Then I ran across two pretty good possibilities: sendEmail  and smtp.cli.  So, googling around I decided to try sendEmail because it had some really good posts out there.

I installed it using apt-get and gave it a shot: failed.  There was a message about not having the right TCP libraries, so I went to the author's site to see what to do and he said to install a couple of perl libraries and give it another shot.  Did some searching and there's an installation tool in perl that could do it, so I tried it.  About an hour later after a ton of compiles, and a hundred miles of console output, I tried it again:  it failed.  OK, FINE, maybe the version of perl isn't the right one, I'll just upgrade perl to get the latest.  apt-get upgrade perl... about an hour of wasted time watching the console being worked to death and got to try it again: it failed.

Now, I'm getting seriously annoyed, so I hunted some more.  Found someone that had exactly the same set of problems which isn't surprising since this is a vanilla Pi (pun intended) and somebody out there must have had the problem.  There were solutions in this post that actually made much more sense than the mess I had gotten myself into.  So, I followed the suggestions, and of course, they were wrong for my particular system.

It wasn't the author's fault, he did the best he could describing it, but I had to allow for different versions of perl, slight directory differences, different source files for things, etc.  I finally found the offending file and edited it to fix the bug and ... wait for it ... it worked.

Remember, your file locations and the actual line that is messed up will vary by location, so if you try this, use this as a guide, not a tutorial.

After installing sendEmail using "sudo apt-get install sendEmail", I ran it with this line:

sendEmail -f "myname@gmail.com" -t "myname@gmail.com" -u "this is a subject" -m "inside the message" -s "smtp.gmail.com":587 -o tls=yes -xu "meagain" -xp "mypassword"

And sendEmail nicely told me:

sendEmail[14590]: ERROR => No TLS support!  SendEmail can't load required libraries. (try installing Net::SSLeay and IO::Socket::SSL)

First, what the heck kind of message is that?  What the heck language is it written in?  Ancient sanscrit makes much more sense to look at than this thing.  So, I searched for the terms and found out that perl has a tool to get updates.  This is where I tried using perl to install the packages and sat forever watching it install, test, print reports, and generally mess around wasting my time.  But, it finished, so I tried it again:  same result.  So, I upgraded perl.  If you're not using perl, don't do this.  It takes forever.  And it didn't help a bit.

This is where I found the post that was actually some help <link>, so I used good ol' apt-get to load the darn libraries that were needed:

sudo apt-get install libnet-ssleay-perl libio-socket-ssl-perl

Which only took a a couple of minutes, and I was back to trying the sendEmail command again.  It failed, but the message was different this time:

invalid SSL_version specified at /usr/local/share/perl/5.14.2/IO/Socket/SSL.pm line 418

I guess you can call this progress.  At least it makes a little more sense and at looks a little different from random characters, and was talked about in the post I mentioned above.  So, I hunted for the SSL.pm file (it was right where the message above said it would be) and edited it.  I found the line that the post talks about around line 1640 or so and changed it.  Now, I'm ready to try it again:

 Email was sent successfully!

Whew!  Now, go check my mail; it was there in all its glory.

I didn't even get to trying smtp.cli darn it.  However after spending so much time getting this to work, I think I'll let someone else do that for me.

4 comments:

  1. Hi
    Take a look at this bogdanioanliviu.no-ip.org i have written there about the problem with TLS on gmail and how i fix it using postfix. Hope it helps.
    Nice blog by the way .

    ReplyDelete
  2. Thanks for the link. If I had found that before I just gave up on mail, I might have taken a different path. Since I got sendEmail working, I'm going to stick with it for this project, but there will be other projects and this post will be on the internet forever so I can come back and look at it again.

    ReplyDelete
  3. Hey Dave,
    Congrats on how far you've come already! I've started reading a few months back and finally got the time to continue reading.
    A great alternative may be to use pushbullet notifications which should make your undertaking a lot easier. If you're not familiar with pushbullet: It sends you push notifications to your phone or from your phone to your desktop (so when receiving text messages or notifications on your phone they are shown on your desktop). There is a way to use pushbullet from the bash (and include it in python) so you can send alarm notifications from your pi to your smartphone.

    Cheers,
    Bjoern

    ReplyDelete