Add outbound email to your app

The GAE email service takes the form of “mail.send_mail(FROM, TO, SUBJECT, TITLE)” so I decided to use my blog form and add two further input fields for the From and To post input parameters. I just copied the original blog html form and added the two additional input fields, quite simple.

First off you must import the email function as such:


from google.appengine.api import mail

I created a new AppEmail class by copying my old NewBlog posting Class and making the following changes by adding this code;


user_from=self.request.get(cgi.escape("emailfrom"))

user_to=self.request.get(cgi.escape("emailto")

I then added additional parameter error handling as such;


if user_from =="":

params['error_from'] = "Please enter an admin email adress"

have_error = True

if user_to =="":

params['error_from'] = "Please enter a valid email adress"

have_error = True

Finally I needed to add the actual send email code as such;


else:

if user_from !="" and user_to!="" and user_title != "" and user_content !="":

mail.send_mail(user_from,user_to,user_title,user_content)

self.response.out.write('email sent')

And that’s it, except the first time I deployed I had forgot to add the mail.send_mail function as above and wondered why the email did not come through! A couple of points to remember.

The from email adress must be either from the GAE registered email account holder or an app administrator email account that was added to the app or an email from the app itself whereby it is in some form of valid email adress for the app such as string@appid.appspotmail.com.

Secondly there is an email limit before the charges start kicking in so you may want to check that as well. I think it is 100 per cycle but best check your app quota in the dashboard. Here is the link to the docs on Google, happy coding!

https://developers.google.com/appengine/docs/python/mail/overview#Receiving_Mail

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s