How to host your own mail server using Google appengine
30 Dec 2013 6 minsGoogle has an outstanding habit of bringing the power of elites to the masses. Firstly, they took over the world of smartphones with android, and now the buzzword everywhere is Google Appengine.
Once you get the basics of GAE and start looking beyond your first HelloWorld application, you will start appreciating the sheer power it provides compared to other paid hosting providers. GAE provides you the ability to send mails from your app without paying a single penny. Only if your app starts exceeding the usage quotas, you will have to pay in order to continue using the services.
An application of the above is creating and hosting your own email server through your app. Below is a single python script which acts as an http mail server that can route your email requests. It also doubles up as a test server so you can test the hosted service by appending the “/test” subdirectory to the url. You may pass http POST requests to your app including fields like username, to, cc, subject etc. once you build this script and host your app in the google cloud.
#main.py
import webapp2
import urllib2
from google.appengine.api import users,mail
class TestPage(webapp2.RequestHandler):
def get(self):
body = """
<form method="post" action="http://your-app-id.appspot.com">
Full Name: <input name="fullname" /><br />
user: <input name="username" /><br />
to: <input name="to" /><br />
cc: <input name="cc" /><br />
bcc: <input name="bcc" /><br />
subject: <input name="subject" /><br />
body: <textarea name="body"></textarea><br />
<input type="submit" />
</form>
"""
self.response.write(body)
class MainPage(webapp2.RequestHandler):
def post(self):
#TODO: check if password is correct.
#From will actually be a placeholder, since only your own appspotmail addresses would be allowed.
tfullname = self.request.get('fullname')
tusername = self.request.get('username')
tto = self.request.get('to')
tcc = self.request.get('cc')
tbcc = self.request.get('bcc')
tsubject = self.request.get('subject')
tbody = self.request.get('body')
tattachment = self.request.get('attachment')
send_mail(fullname=tfullname,username=tusername,to=tto,subject=tsubject,body=tbody,cc=tcc,bcc=tbcc,attachment=tattachment)
self.response.write("success")
#self.response.write(tto)
def get(self):
#self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write("""your-app-id http mail service version 1.0.<br />
post vars:
[password]
[fullname]
[username]
[to]
[cc]
[bcc]
[subject]
[body]
[attachment]
<br />
<br />
<a type="button" href="test">Click here for a demo</a>
""")
def send_mail(fullname,username,to,subject,body,cc=None,bcc=None,attachment=None):
sender=fullname + " <" + username + "@your-app-id.appspotmail.com>"
message = mail.EmailMessage(sender=sender)
message.to = to
if cc!=None and cc!='':
message.cc=cc
if bcc!=None and bcc!='':
message.bcc = bcc
message.subject = subject
message.body = body
message.send()
application = webapp2.WSGIApplication([
('/', MainPage),
('/test', TestPage)
], debug=True)
So now, you may bid farewell to third party mail services like mail-chimp and others - since you have your own mail server.