mailgun提供的HTTP API 可以通过python直接调用,与django结合起来的话只有mailgun 0.2 需要设置如下
在setting.py中加入:
EMAIL_BACKEND = 'django_mailgun.MailgunBackend'
MAILGUN_ACCESS_KEY = 'ACCESS-KEY'#your mailgun api key
MAILGUN_SERVER_NAME = 'SERVER-NAME'#your mailgun domain
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
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
这样就可以进行群发了。
修改___init__.py后 需要对django-mailgun重新setup,
python2.6 setup.py build
python2.6 setup.py install
如果install 时报错 需要找到django_mailgun-0.2-py2.6.egg 后rm
重启django,就可以运行了