Sending from gmail with smtplib - python

I'm attempting to use smtplib to set up a python script that will email out alert notifications. I don't need anything fancy, so I figured I'd stick with the basics and use SMTP for my messages. Following one of the many available online examples, my attempt to do so begins
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login('username','password')
but at this point fails with an SMTPAuthenticationError 534, telling me to log on via my web browser. When I so, gmail has emailed me saying the sign in failed because it came from "an app that doesn't meet modern security standards."
I've seen some people having similar problems solved by changing their gmail account to accept logon from less secure apps, but did this myself and continued to get the same error message.
So is plain old SMTP not going to cut it and I need to get fancy? What am I doing wrong, or what can I do instead to make this work?

As it turns out, my attempts to set gmail to accept logon from less secure apps had (for whatever reason) not taken the first time I tried. Going to https://myaccount.google.com/security?pli=1 and turning this setting on allowed me to access successfully.
The comments pointing me towards oauth2 and app passwords are most helpful though, and I now that I've accomplished the bare bones I may look into those for more robust long-term approaches. Thank you!

Related

How gmail come to know that if user is trying to access it from a browser or from other application?

I've made one program in Python using Web.py to access my gmail account. It gave me following error in beginning "Please log in via your web browser and then try again." Then I went to gmail security settings and enable it for accessing gmail from less secure apps. It started working fine from the program I've developed.
( Security Setting link: https://www.google.com/settings/security/lesssecureapps)
My question is how Gmail comes to know that from where I am trying to access it? Also, if you can share similar scenarios with me where you have to access something via web browser only?
Thanks in advance.
when you access gmail,maybe the http protocol is used,in http protocol header,User-Agent represent our browse and operation system information.Hope this helpful
Is the gmail account a normal account or your own domain? If its ur domain then you need to login and change the outbound relay section in settings.
unlock captcha will help to fix this problem
Another way simple way is if you go to the email after trying to access without browser a yellow notification will tell you that something tried to access your account.Follow steps and you may have whay you want.
Hope it helps!!

GAE Python - Receive incoming email on #mydomain.com address

I am setting up a GAE app that triggers certain tasks by an email from the user. I've set up the app using the information on this page: https://developers.google.com/appengine/docs/python/mail/receivingmail. Everything works as expected, however, I would like to send the emails to a #mydomain.com email address, instead of a #myapp.appspotmail.com email address.
Currently I am using an automatic forward on the email address to get around this, however once this scales I expect to reach the daily limit on forwarded messages at some point (10.000 according to https://support.google.com/a/answer/166852?hl=en).
Is there any way that I can integrate my app with the #mydomain.com email address to prevent this from happening? Or would you know another solution for this issue?
Probably, you should check recently implemented GAE integration with SendGrid, I haven't used it myself (use the same solution you've described with email forwarding), but looks like it can help.
As LA_ mentioned, SendGrid offers a webhook for this. I ended up using MailGun, which has a similar functionality and calls a HTTP POST to my app whenever an email comes in on my #mydomain.com address.

Writing an email sending program in Python

I want to write an email sending script in Python. Most email programs have to connect to existing servers, like Gmail or Hotmail. I want my script to work independantly of those servers and just be able to send email itself (without having to logon anywhere else). The reason for this is because most email servers (like Yahoo) limit what you can do, such as controlling the sender address or sending certain types of files. So I wanted to write my own script to get around that. So what do I do? Where do I begin learning how to do this? Do I have to write my own server? If I do, how is that done?
You need an MTA (a.k.a. mail transfer agent), whether it's local or remote. SMTP servers talk to each other to deliver the mail through — if you don't want to connect to the remote one, you need to run a local one. Look into Postfix or exim — just be careful not to allow random people to connect to it. Go to Server Fault if you need help with configuring them.
This is language-agnostic, BTW.
The email-module of Python should give you a good starting point. Btw this was the first hit when googling Python email.
You'll essentially need to set up your own mail server. I prefer postfix but there are several alternatives, you'll have to google this one.
Once you can send email through your own server, look into smtplib or email-library for sending email with Python

Email integration

I was wondering if someone could help me out. In some web application, the app will send out emails, say when a new message has been posted. Then instead of signing into the application to post a reply you can just simply reply to the email and it will automatically update the web app with your response.
My question is, how is this done and what is it called?
Thanks
Generally:
1) Set up a dedicated email account for the purpose.
2) Have a programm monitor the mailbox (let's say fetchmail, since that's what I do).
3) When an email arrives at the account, fetchmail downloads the email, writes it to disk, and calls script or program you have written with the email file as an argument.
4) Your script or program parses the email and takes an appropriate action.
The part that's usually mysterious to people is the fetchmail part (#2).
Specifically on Mail Servers (iff you control the mailserver enough to redirect emails to scripts):
1-3) Configure an address to be piped to a script you have written.
4) Same as above.
You should take a look at Lamson; it'll enable you do what you've described, and more besides.
From your tags, I'll assume you're wanting to do this in Django.
There's an app out there called jutda-helpdesk that does exactly what you're looking for using poplib, which means you just have to set up a POP3 compatible email address.
Take a look at their get_email.py to see how they do it. You just run this script from cron.
This is an area where the Rails-world is ahead: Rails has built-in support for receiving emails. The mail sever configuration though is probably just the same.
To see a working example on how to receive emails in python and process then using django, check this: http://code.google.com/p/jutda-helpdesk/
A common tool used for this purpose is procmail.
You need to set up dedicated email address (which is the "from_email" address in your outgoing email). Then your MTA, such as postfix or qmail, will deliver mail to that address to procmail instead of an actual mailbox.
Procmail can then pass the email on to your python script that can do updates in the app. See standalone django scripts by James Bennett on how to code python scripts that can work with your app.

How to best implement simple crash / error reporting?

What would be the best way to implement a simple crash / error reporting mechanism?
Details: my app is cross-platform (mac/windows/linux) and written in Python, so I just need something that will send me a small amount of text, e.g. just a timestamp and a traceback (which I already generate and show in my error dialog).
It would be fine if it could simply email it, but I can't think of a way to do this without including a username and password for the smtp server in the application...
Should I implement a simple web service on the server side and have my app send it an HTTP request with the info? Any better ideas?
The web service is the best way, but there are some caveats:
You should always ask the user if it is ok to send error feedback information.
You should be prepared to fail gracefully if there are network errors. Don't let a failure to report a crash impede recovery!
You should avoid including user identifying or sensitive information unless the user knows (see #1) and you should either use SSL or otherwise protect it. Some jurisdictions impose burdens on you that you might not want to deal with, so it's best to simply not save such information.
Like any web service, make sure your service is not exploitable by miscreants.
I can't think of a way to do this without including a username and password for the smtp server in the application...
You only need a username and password for authenticating yourself to a smarthost. You don't need it to send mail directly, you need it to send mail through a relay, e.g. your ISP's mail server. It's perfectly possible to send email without authentication - that's why spam is so hard to stop.
Having said that, some ISPs block outbound traffic on port 25, so the most robust alternative is an HTTP POST, which is unlikely to be blocked by anything. Be sure to pick a URL that you won't feel restricted by later on, or better yet, have the application periodically check for updates, so if you decide to change domains or something, you can push an update in advance.
Security isn't really an issue. You can fairly easily discard junk data, so all that really concerns you is whether or not somebody would go to the trouble of constructing fake tracebacks to mess with you, and that's a very unlikely situation.
As for the payload, PyCrash can help you with that.
The web hit is the way to go, but make sure you pick a good URL - your app will be hitting it for years to come.
PyCrash?
Whether you use SMTP or HTTP to send the data, you need to have a username/password in the application to prevent just anyone from sending random data to you.
With that in mind, I suspect it would be easier to use SMTP rather than HTTP to send the data.
Some kind of simple web service would suffice. You would have to consider security so not just anyone could make requests to your service..
On a larger scale we considered a JMS messaging system. Put a serialized object of data containing the traceback/error message into a queue and consume it every x minutes generating reports/alerts from that data.

Categories

Resources