RDS
Posts: 783
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Sending email from RPi

Mon Nov 16, 2015 10:27 am

I have tried at least 3 different methods of sending an email from my Pi gmail address, all without success.
Could someone please advise the easiest way to achieve this.
I would like to send plain text and also email with an attachment.

fredoll
Posts: 228
Joined: Thu Jan 10, 2013 10:30 am

Re: Sending email from RPi

Mon Nov 16, 2015 10:39 am

ssmtp package is the easiest way I know of ...
you only have to enter a valid smtp account information in the config file ...

RDS
Posts: 783
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Sending email from RPi

Mon Nov 16, 2015 5:19 pm

@fredoll
I have tried SMTP, without success so far.
Is SSMTP different?

One of the lines in one of the Programs that I was unsure about was:
smtp_user = ????
as I did not know what to enter after the =.
Any advice would be appreciated.

User avatar
DougieLawson
Posts: 40594
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Sending email from RPi

Mon Nov 16, 2015 5:25 pm

sudo apt-get install postfix heirloom-mailx
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Sending email from RPi

Mon Nov 16, 2015 5:46 pm

What 3 methods did you try? What were the exact symptoms? Any error messages?

Well, the Python docs https://docs.python.org/2/library/email-examples.html - looks pretty straightforward ...apart from the fact that you need an SMTP server to send through.

Then there is the Gmail variable... This one works perfectly for me (Python 2.7.9 in a Debian VirtualBox VM on a Windows laptop, from my own gmail to a friend's gmail - to my work email it does not seem to arrive but get's saved in my gmail sent folder...) http://www.nixtutor.com/linux/send-mail ... th-python/
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

User avatar
emgi
Posts: 357
Joined: Thu Nov 07, 2013 4:08 pm
Location: NL

Re: Sending email from RPi

Mon Nov 16, 2015 5:58 pm

RDS wrote:I have tried at least 3 different methods of sending an email from my Pi gmail address, all without success.
Could someone please advise the easiest way to achieve this.
I would like to send plain text and also email with an attachment.
Have you already tried mutt? I'm doing exactly what you seem to need and it's working like a charm after setting it up properly.
The 'secret' is that you need to run through the menu to get the certificates stored; after doing that it work unattended until the certificates expire. My command line for mailing an ebook as attachment looks like this:

Code: Select all

mutt me@kindle.com -a /ix2/ebook/EN/J/Jo\ Nesbo/Midnight\ Sun\ -\ Jo\ Nesbo.mobi < /dev/null
The < /dev/null serves to supress (acknowledge) all prompts.
Goodluck!

/emgi

RDS
Posts: 783
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Sending email from RPi

Mon Nov 16, 2015 6:01 pm

I have just checked on my Windows PC and found the following error message has been created against another gmail email address that I use.
Someone just tried to sign in to your Google Account xxxxxxx.xxxxxx@gmail.com from an app that doesn't meet modern security standards. (I have added the x's in place of actual email address, so it does appear that one of my tries may have produced something.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Sending email from RPi

Mon Nov 16, 2015 6:22 pm

Maybe there is a difference in the smtp lib security between versions.

Perhaps worth noting that I used my gmail full address as username and (obviously) my gmail password.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

BillStephenson
Posts: 256
Joined: Thu May 17, 2012 1:25 am
Location: SW Missouri Ozarks
Contact: Website

Re: Sending email from RPi

Mon Nov 16, 2015 6:38 pm

I think Google recently change the way you authenticate 3rd party send mail functions. The new routines use "OAuth 2.0" or something.

It used to be dead simple but I recently tried using some code I'd written in perl and used previously but it didn't work, so I looked into it a bit and moved on. I will have to get back to it though so I'll keep an eye on this thread to either find or offer a solution. In then meantime you might find something useful here:

https://developers.google.com/gmail/api/downloads

User avatar
DougieLawson
Posts: 40594
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Sending email from RPi

Mon Nov 16, 2015 8:40 pm

Here's a piece of Perl code that works with GMail.

Code: Select all

#!/usr/bin/perl
use Net::SMTPS;
use Authen::SASL;

my $mailargs = {
'Port' => 587,
'doSSL' => 'starttls',
'SSL_verify_mode' => 'SSL_VERIFY_NONE',
};

my $user = 'example@gmail.com';
my $pass = 'mySecretRedactedPassword';
my $smtp = Net::SMTPS->new('smtp.gmail.com', %$mailargs);
my $sasl = Authen::SASL->new(
'mechanisms' => 'Digest-MD5',
'callback' => { 'user' => $user, 'pass' => $pass, 'authname' => $user}
);
$smtp->auth($sasl);
$smtp->mail('example@gmail.com');
$smtp->to('example@uk2.net');
$smtp->data;
$smtp->datasend('Hello from perl on a RaspberryPi');
$smtp->dataend;
$smtp->quit;

1;
Needs sudo apt-get install libnet-smtps-perl to work.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

RDS
Posts: 783
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Sending email from RPi

Mon Nov 16, 2015 10:48 pm

@DougieLawson
I was really hopeful there for a short while!

I didn't realise that Perl was a different language until I noticed that I didn't recognise any of the details of the code.

Unfortunately, it needs to be in Python to work with the application I have in mind.

Thanks anyway.

RDS
Posts: 783
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Sending email from RPi

Mon Nov 16, 2015 10:49 pm

DougieLawson wrote:sudo apt-get install postfix heirloom-mailx
Sorry, I do not understand this at all.

RDS
Posts: 783
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: Sending email from RPi

Mon Nov 16, 2015 10:52 pm

@emgi
I am not doing very well with understanding some of the replies that you guys have kindly put together for me.

What is mutt?

User avatar
DougieLawson
Posts: 40594
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Sending email from RPi

Mon Nov 16, 2015 11:13 pm

Sending email in python is equally trivial.

Code: Select all

#!/usr/bin/python3

import time
from datetime import datetime
import smtplib
# from email.mime.image import MIMEImage

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

f_time = datetime.now().strftime('%a %d %b @ %H:%M')

toaddr = 'example@uk2.net'   
me = 'example@gmail.com' 
password = 'mySecretRedactedPassword' 
subject = 'Test message ' + f_time

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = me
msg['To'] = toaddr
msg.preamble = "Test message @ " + f_time
message = "Main body text here"
html = "<html><head></head><body><h1>Title</h1><p>Main text</body></html>"
plainText = MIMEText(message, 'plain')
htmlText = MIMEText(html,'html')
msg.attach(plainText)
msg.attach(htmlText)

try:
   s = smtplib.SMTP('smtp.gmail.com',587)
#   s.set_debuglevel(True)
   s.starttls()
   s.login(me,password)
   s.send_message(msg)
   s.quit()
except:
   print ("Error: unable to send email")
Mutt is a command line mail program, I prefer alpine (a clone of pine).
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
DougieLawson
Posts: 40594
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Sending email from RPi

Mon Nov 16, 2015 11:13 pm

RDS wrote:
DougieLawson wrote:sudo apt-get install postfix heirloom-mailx
Sorry, I do not understand this at all.
Open an LXTerminal, type in the command starting sudo ...
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

BillStephenson
Posts: 256
Joined: Thu May 17, 2012 1:25 am
Location: SW Missouri Ozarks
Contact: Website

Re: Sending email from RPi

Mon Nov 16, 2015 11:55 pm

DougieLawson wrote:Here's a piece of Perl code that works with GMail. ... Needs sudo apt-get install libnet-smtps-perl to work.
Thank you!

I'll have to play with that.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Sending email from RPi

Tue Nov 17, 2015 10:01 am

DougieLawson's examples in Perl and Python are essentially the same as the example I linked to (with the exception of the Python one sending MIME multipart - not just plain text email).

To verify I have just tried the same as yesterday in the VM now on Python 2.7.3 on a Windows 7 laptop and it works fine (at least between two gmail accounts).

Just to highlight some bits:

Code: Select all

import smtplib

# Using my own gmail address as sender
fromaddr = '...@gmail.com'

# Recipient(s) (in my test different from sender)
toaddrs  = '...@gmail.com'

# The email message 'body'
msg = 'Testing python smtplib!'


# Credentials to log in to gmail
# Full gmail email address - same as sender
username = fromaddr
# The password I use to login to gmail interactively (obviously)
password = '...'

# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

Also confirmed that sending the email worked to my work email too - the message just got blocked by the company email security software (possibly due to missing subject and maybe some other headers).
Last edited by -rst- on Tue Nov 17, 2015 10:08 am, edited 1 time in total.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Sending email from RPi

Tue Nov 17, 2015 10:06 am

DougieLawson wrote:
RDS wrote:
DougieLawson wrote:sudo apt-get install postfix heirloom-mailx
Sorry, I do not understand this at all.
Open an LXTerminal, type in the command starting sudo ...
I guess we may have more and more Raspberry Pi users who are not necessarily admin users. If this is not the case with RDS the commands 'sudo' and 'apt-get' are something worth learning about.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

User avatar
DougieLawson
Posts: 40594
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Sending email from RPi

Tue Nov 17, 2015 10:22 am

-rst- wrote:I guess we may have more and more Raspberry Pi users who are not necessarily admin users. If this is not the case with RDS the commands 'sudo' and 'apt-get' are something worth learning about.
I suspect that's one of the reasons why the (now very dead) PiStore experiment was started it gave the non-admin users a GUI way to install ready to run software without needing to know diddly squat about how their system works.

Replacing that with synaptic isn't an ideal solution, but at least it's got the GUI eye candy.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Sending email from RPi

Tue Nov 17, 2015 11:25 am

I've used SMTP in python with gmail. If you go this way, I think you have to enable "access for less secure apps".

See here: https://support.google.com/accounts/ans ... 0255?hl=en
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Sending email from RPi

Tue Nov 17, 2015 11:45 am

elParaguayo wrote:I've used SMTP in python with gmail. If you go this way, I think you have to enable "access for less secure apps".

See here: https://support.google.com/accounts/ans ... 0255?hl=en
Good to know. For some reason (maybe I just don't remember changing it for some earlier testing) mine was set to 'on'. Turning this to 'off caused the above code to fail with:

Code: Select all

smtplib.SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/ContinueSignIn?s...> Please
log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 z66sm10306337wmz.7 - gsmtp')
And I got an email with subject ' Sign-in attempt prevented'.

Tried also the Perl version courtesy of DougieLawson: (obviously) the same 'Access for less secure apps' needed for it to work.
Last edited by -rst- on Tue Nov 17, 2015 12:06 pm, edited 1 time in total.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

User avatar
DougieLawson
Posts: 40594
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Sending email from RPi

Tue Nov 17, 2015 11:47 am

My sample code works without setting that. But it needs my regular very secure Google password.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Sending email from RPi

Tue Nov 17, 2015 12:20 pm

DougieLawson wrote:
My sample code works without setting that. But it needs my regular very secure Google password.
Are you absolutely sure? I am getting the same SMTPAuthenticationError with your Python code and Perl code does nothing if I turn off the less secure apps. Both work if I turn the setting on. This is on a Debian (VirtualBox) VM.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

User avatar
DougieLawson
Posts: 40594
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Sending email from RPi

Tue Nov 17, 2015 12:22 pm

I turned the less secure option off last week.

Just checked and "Allow less secure apps: ON" is set.
Last edited by DougieLawson on Tue Nov 17, 2015 12:57 pm, edited 1 time in total.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Sending email from RPi

Tue Nov 17, 2015 12:29 pm

Hmm, interesting... I copied your code, just replaced the email addresses and the password - with the setting 'on' the code works fine - with 'off' it does not. What else (other than the OS we run the code in) could be different?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Python”