Use of mailgun in Django

Source: Internet
Author: User

The http api provided by mailgun can be directly called through python. If it is combined with Django, only mailgun 0.2 needs to be set as follows:

Add the following to setting. py:

EMAIL_BACKEND = 'django_mailgun.MailgunBackend'MAILGUN_ACCESS_KEY = 'ACCESS-KEY'#your mailgun api keyMAILGUN_SERVER_NAME = 'SERVER-NAME'#your mailgun domain

After configuration, you can use send_mail to send emails. However, this version has bugs and does not support group sending, mainly because _ init _ in django_mailgun __. the _ Send () function of Py is as follows:

        try:            r = requests.\                post(self._api_url + "messages.mime",                     auth=("api", self._access_key),                     data={                            "to": recipients,                            "from": from_email,                         },                     files={                          "message": StringIO(email_message.message().as_string())                        }                     )        except:            if not self.fail_silently:                raise            return False

Here, the mime request is used. mailgun only processes the last email of the recipients. Therefore, you cannot send a group of emails. Instead, try to send an HTML message:

try:            r = requests.\                post(self._api_url + "messages",                     auth=("api", self._access_key),                     data={                            "to": recipients,                            "from": from_email,                            "subject":subject,#subject = unicode(email_message.message()['Subject'])                            "html":text,#text = email_message.body                         },                     )        except:            if not self.fail_silently:                raise            return False

In this way, the group can be sent.

Modify ___ init _. py and then re-setup Django-mailgun,

python2.6  setup.py buildpython2.6  setup.py install

If an error is reported during install, you need to find the django_mailgun-0.2-py2.6.egg after RM

Restart Django to run it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.