Martin
Martin

Reputation: 351

Using Google App Engine's Mail API for django-allauth email

I'm working on a project hosted on Google App Engine, and using Django-allauth for my user system.

Right now I'm just using the following setup in settings.py

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = DEFAULT_FROM_EMAIL = '[email protected]'
EMAIL_HOST_PASSWORD = 'password'

But I would like to use GAE's Mail API instead, so that I can take use of all the quotas available.

To send an email with GAE's API I can do as follows:

sender_address = "[email protected]"
subject = "Subject"
body = "Body."
user_address = "[email protected]"
mail.send_mail(sender_address, user_address, subject, body)

As I understand it from the allauth documentation, I can "hook up your own custom mechanism by overriding the send_mail method of the account adapter (allauth.account.adapter.DefaultAccountAdapter)."

But I'm really confusing about how to go about doing this.

Does it matter where I place the overridden function?

Any additional tips would be greatly appreciated.


My Solution

What I did to get Django-allauth email system to work with Google App Engine mail API

Created a file auth.py in my 'Home' app:

from allauth.account.adapter import DefaultAccountAdapter
from google.appengine.api import mail


class MyAccountAdapter(DefaultAccountAdapter):

    def send_mail(self, template_prefix, email, context):
        msg = self.render_mail(template_prefix, email, context)

        sender_address = "[email protected]"
        subject = msg.subject
        body = msg.body
        user_address = email
        mail.send_mail(sender_address, user_address, subject, body)

In order to use your email as sender with GAE's mail API, it is important to remember to authorize the email as a sender

Lastly, as e4c5 pointed out, allauth has to know that this override exists, which is done as so in settings.py

ACCOUNT_ADAPTER = 'home.auth.MyAccountAdapter'

Upvotes: 3

Views: 551

Answers (1)

e4c5
e4c5

Reputation: 53774

You have to tell django-allauth about your custom adapter by adding the following line to settings.py

ACCOUNT_ADAPTER = 'my_app.MyAccountAdapter'

taking care to replace my_app with the correct name

Upvotes: 2

Related Questions