Send email - BitKnitting/should_I_water GitHub Wiki
At around 5AM each morning, I send an email to myself and my husband. I use gmail. My husband uses Outlook. Sadly, sending mail is not as simple as I thought it would be.
Gmail
I ended up using Google's mail apis. I did this because of the scary message I would get when I didn't:

It is the last sentence that bothers me the most: "You can continue to use this app by allowing access to less secure apps, but this may leave your account vulnerable. ...Ouch. That didn't sound good...onto Google's API...
I did this because of the OAUTH requirements. When I used what looked like a common way to send mail from a Raspberry Pi, I would get a scary gmail message:
Credentials
The "big deal" in safely using gmail is to get the OAUTH credentials right. This turned out to be not obvious to me.
Scope
Make sure the scope of activities is set correctly. I set this to:
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.send'
Thanks to Tanner's post, in order to get authorized credentials:
- Run the script with
--noauth_local_webserver. To do this, the script needs the code:
import argparse
parser = argparse.ArgumentParser(parents=[tools.argparser])
flags = parser.parse_args()
- See if the credential gunk is available and can be used. If not, tools.run_flow with flags will let Google know we need an OAUTH token. Google handles all this...
# Try to retrieve credentials from storage or run the flow to generate them
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, STORAGE, flags, http=http)
OK. That covers getting the credentials. Now I can send the message!....The rest of the gunk in the test script was found when googling for several examples.
Outlook.com
One of us uses Outlook.com for email. It turns out I can use the smtplib that comes with Python to send a mail. I ran into one challenge:
ssmtp: 554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. ...,
Next came a lot of gobly-gook. This error did not occur if I set the From: and To: of the message to the Outlook.com email address.