GAE Receive Email and Parse it - python

I am wondering whether Flask app in Google App Engine Production server can receive email with attachment from gmail or any other third party.
As I am having a requirement now that I would receive a email with csv attachment, need to parse it - take the decision and change the content on the website.
I am thinking of automating the entire thing, but I do no how to setup mail with google app engine, that is my application whenever receive a mail with attachment, parse it, do the computations and change the page content and display on the website.
I have gone through google app engine document but it doesn't provide much information.
I have setup the inbound_email in app.yaml and also handlers for the email, now the only question is how to get the email to the handler script. enter link description here
Is there any way on doing the above requirements ???

Related

How to send mails from gmail account using heroku?

I'm trying to build a simple app in python which sends emails if stock/crypto prices increase or decrease by x percent. I'm using yagmail to send these emails through a Gmail account. I have already tested the code locally and now I want to move it to Heroku. I have created GitHub secrets for the email addresses and for the Google App Password for yagmail's SMTP server and now I can deploy the app by connecting it to my Github repo. As far as I can tell it runs just fine, but the emails are not being sent.
I'm thinking that the problem stems from the Google App Password for yagmail's SMTP server, but I'm not sure. This is my first app so it could be that I'm missing something really basic.
Here is a simple code sample which is working on my desktop but not through Heroku:
SENDER_EMAIL = os.getenv("SENDER_EMAIL")
SENDER_APP_PASSWORD = os.getenv("SENDER_APP_PASSWORD")
RECEIVER_EMAIL = os.getenv("RECEIVER_EMAIL")
test_sub = "Test mail"
test_cont = "Test content"
with yagmail.SMTP(SENDER_EMAIL, SENDER_APP_PASSWORD) as yag:
yag.send(RECEIVER_EMAIL, test_sub, test_cont)
You need to define the configuration parameters (SENDER_EMAIL, SENDER_APP_PASSWORD, RECEIVER_EMAIL) as Config Vars.
They will be injected at runtime in your Heroku Dyno (on GitHubActions your secrets can be use for your GitHubActions pipeline for example) where you can grab them using Python os package.

Flask-Mail on Google App Engine Flexible ENV

I'm trying to get Flask-Mail setup on in Flexible ENV on Google App Engine. Flask-Mail works on my localhost using the credentials for a domain I am trying to use to send the mail. However, when using it on GAE through my API it returns a 502 error, however it shows no error messages in the logs or console. Going through the documentation for GAE Flexible it doesn't mention anything about NOT being able to use it, however it doesn't show how one would setup Flask-Mail either.
I have this..
mail = Mail()
print('1') // We Get here
msg = Message("Hello",
sender="me#mydomain.com",
recipients=["me#mydomain.com"])
print('2') // We get here
msg.body = 'Testing'
print('3') // We get here
mail.send(msg)
print('4') // This never gets call because I timeout on a 502 before this
I can tell I am not getting any fatal errors because the app stays working. However this fails with the 502. I have tried adding my email to the list of authorized senders but it doesn't seem to have helped.
I would appreciate any feedback. If I forced to use a 3rd party service to send mail it may cause me to move the project off of GAE.
As Ivan posted on his comment, to send email from a GAE app you need to use a mail service. Right now there are 3 options for apps on a flexible environment: Mailgun, MailJet and SendGrid. Choose the one you see better for your app.
After setting up an account on the mail service you have chosen, you have to prepare your code by integrating the parts related to the mail service.
These tutorials should help you establish the mail service for your app:
Mailgun
MailJet
SendGrid
I've had the same error but on a virtual machine on the internet ( linode service ) and it turned out that it has some thing to do with rDNS and some domain name config that you have to set up for your Ip address to get things working correctly , check this
https://www.linode.com/community/questions/19082/i-just-created-my-first-linode-and-i-cant-send-emails-why

Gmail open tracking, set cookie with Django

I'm working on a simple app to embed a transparent pixel into emails being sent out manually with Gmail and then cookie the user.
I'm inserting this <img> into the email:
<img height="1" src="https://example.net/pixel.png?guid=1234" style="visibility:" width="1">
The intent is that when the email is opened it should request the image from example.net/pixel.png
The Django app with an endpoint of pixel.png has this view:
def set_cookie(request):
PIXEL_GIF_DATA = "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
data = base64.b64decode(PIXEL_GIF_DATA)
response = HttpResponse(data, content_type='image/gif')
response.set_cookie('some_cookie_name', 'some_cookie_value')
return response
If I visit `example.net/pixel.png?guid=1234' it's setting the cookie appropriately, so we're good there.
The only issue I'm running into is that when the email is opened the request is not being made out to my server. As the recipient if I go into the developer tools and watch the network requests I'm not seeing the request being made to mysite.net
However, if I view the original email, down in the footer I can see that my <img> tag is included.
If I try using an external image like static.example.net/images/sometest123.png the image does come through and is visible.
Gmail fetches the image and caches it on its servers, to prevent this kind of tracking and protect the recipient's privacy.

Sending mail python webapp2 GAE

I initially tried using the smptlib library but in order to send an e-mail I need to use SSL which costs me with Google App Engine. Other than that, smtplib was a perfect solution.
I found a link here which explains how to send an email using the GAE API.
However, I can't seem to figure out how one would log in to a gmail account say, in order to send the email.
The purpose of this is to send a verification email to addresses of those who are registering. But in the link it shows how to get the current_user and send the email using their credentials. How would I explicitly enter an email, password, and smtp server like I would do with smtplib in order to send the email from my desired address.
Thanks for any answers!
You can't send from an arbitrary email address. As the overview docs state, you can only send from:
The Gmail or Google Apps Account of the user who is currently signed in
Any email address of the form anything#appname.appspotmail.com or anything#appalias.appspotmail.com
Any email address listed in Email API Authorized Senders found in the App Engine Settings page of the Developers Console
If you are the owner of the email account you want to send from, and it is a GMail account, you can add it to the Email API Authorized Senders list from the App Engine console.
Once that's done, you can just use it as the sender address - you don't need to log into anything.

Need to send a HTTP request in Python that'll authenticate with Google Accounts

I have an app which amounts to a Python script, running on the user's phone, and a JS client, running in the user's browser. The Python script sends messages to App Engine as HTTP requests. The server then pushes the messages to the JS client.
The problem is authentication: The server can easily use Google Accounts to authenticate anything coming from the JS client as being sent by a particular user, but I do not know how to get the Python script to make HTTP requests which will also authenticate.
Any ideas?
According to its homepage, httplib2 has support for Google Account authentication, maybe that may help you?
Can you use OAUth to authenticate with Google, then use the OAuth token to ensure the messages are legitimate?

Categories