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

落爺英雄遲暮 提交于 2019-12-10 17:55:34

问题


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 = 'myMail@gmail.com'
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 = "myMail@gmail.com"
subject = "Subject"
body = "Body."
user_address = "user@gmail.com"
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 = "myEmailAddress@gmail.com"
        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'

回答1:


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



来源:https://stackoverflow.com/questions/36714106/using-google-app-engines-mail-api-for-django-allauth-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!