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

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.

Related

Django: Should sendgrid emails always be sent from Celery?

I'm using django-sendgrid-v5 and I read somewhere that it isn't good to send emails from the main webserver. Should I process emails from Celery? Or is it fine to call from the main app since I'm using an external service like Sendgrid anyways?
I don't know in which context you've read that, but I would guess it has something to do with reliability, spam and security in general.
Short answer: Yes, this should be fully okay as you are using an external email service.
Another option is to set up a Smart host on your webserver and let your main email server deliver it to the final recipients.
Long answer: Nowadays sending emails from a (web)server, which is not fully set up as an email server might be difficult in means of reliably sending emails.
Due to the massive amounts of spam and malware sent, most (or at least a lot) receiving email servers (Mail Exchangers) are trying to check if the emails they should deliver to their users, are legit.
This is done by several settings mostly on the server itself. To name only a few: RDNS, DKIM, Greylisting, etc.
In general a (web) server whos main purpose is not sending emails, does not have all these settings. This might result in difficulties to reach certain email addresses.

How to receive and parse email with Cloud Functions?

Google Cloud Functions allows you to easily activate a function upon a trigger (eg Firebase data change, HTTP request...).
I am looking for a way to execute a function when a user sends (or typically replies) to a email address. For instance, my dashboard sends an email, I would like to catch the reply, parse the content and upload it to Firebase as a comment in my Dashboard.
I understand that Google recommends to use Sendgrid. I however don't understand:
- How to setup the trigger upon a reply
- How to read the content and set the reading
I only found how to send emails here
One option if you use GSuite is to use the Gmail watch mechanism to listen to new emails in your inbox. The message can then be posted to a PubSub topic which can trigger a Cloud Function to parse the email and perform your necessary next steps.
Here is a good use case that explains this mechanism
https://cloud.google.com/blog/products/application-development/adding-custom-intelligence-to-gmail-with-serverless-on-gcp
Google Cloud Functions does not provide a permanent listener on an endpoint. There is also no event source for SMTP, which is the protocols involved with email delivery. So you can't simple respond to emails as they come in with Cloud Functions at the moment.
What you can do is direct the traffic to an existing SMTP server, and then use Cloud Functions to read from there at an interval.
An alternative is to use the Sendgrid Inbound Email API, which can call a webhook for every message it receives. And your webhook would then be a HTTP triggered Cloud Function.

Sending from gmail with smtplib

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!

Testing email sending? - Python/Django

I have a live site, but would like to run a periodic task that would periodically check if various pages and email sending work.
e.g. register page, forgot password page, etc...
I'm having quite a problem figuring out how to do this...
Any ideas? :)
At my workplace we use nagios to monitor, among many things, that the smtp service and web apps are running as it should. For crucial pages, we use the check_http nagios plugin to ensure that the pages are accessible and contain specific keywords.
You can set each check to run as frequently as you like, and have nagios send you an email if anything goes amiss.
As for checking whether email sending from django is working, that's a little tricky. None of my sites are mission critical, so I'm happy to assume that if my code base doesn't change and if the web apps and SMTP servers are working, then emails from django should be ok.
If it is critical that you check that django can send emails, you could have a view (restricted by source IP and perhaps a password in POST/GET) that triggers an outgoing email. You can then write your own plugin to access said view and check that the email is sent.
Here's a nice write-up on how to check your email service using nagios: http://www.linuxjournal.com/content/monitoring-email-nagios

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.

Categories

Resources