Hi!
I'm using Djrill as the default email backend for Django 1.6.5:
EMAIL_BACKEND = 'djrill.mail.backends.djrill.DjrillBackend'
and I'm using it to log errors via e-mail to the user if a task running in Celery failed.
try:
# Do something...
except (NotChangedFeed, ValueError) as e:
# ValueError happens when feed url is empty
pass
except Exception as e:
if send_email:
error_message = get_error_message(e)
error_logger = logging.getLogger('mymodule.tasks.email')
try:
error_logger.error(error_message)
except MandrillAPIError as inner_e:
raven_client.captureMessage(inner_e.response.content)
The EmailHandler
for logging does this to send the e-mail:
def _send_email(self, subject, body):
"""Sends the email if the user wants to"""
email = EmailMultiAlternatives(subject=subject,
from_email=self.fromaddr,
to=(self.search_engine.user.email,))
email.attach_alternative(body, "text/html")
email.tags = ["error"]
email.metadata = {'user': self.search_engine.user.username}
# send the email only if emailme_error_log is True
if self.search_engine.emailme_error_log:
email.send()
The response content is:
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.6.0</center>
</body>
</html>
I've added some prints to your code to see what's being sent. The URL is:
http://mandrillapp.com/api/1.0/messages/send.json
The method is POST and the request headers are:
User-Agent: python-requests/2.3.0 CPython/2.7.6 Linux/3.13.0-24-generic
Content-Type: application/json
Accept-Encoding: gzip, deflate
Accept: */*
And the body of the request (I've changed the target e-mail and API key) is:
{
"message":{
"tags":[
"error"
],
"text":"",
"from_email":"[email protected]",
"to":[
{
"type":"to",
"email":"[email protected]",
"name":""
}
],
"html":"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n <head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>[someapp] Error in SOME PART</title>\n\n </head>\n <body bgcolor=\"#D7D9DC\" style=\"padding:0; width:100%; -webkit-text-size-adjust:100%; margin:0; -ms-text-size-adjust:100%; background-color:#D7D9DC\" width=\"100%\">\n\n <table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" id=\"backgroundTable\" style=\"border-collapse:collapse; padding:0; width:100%; mso-table-rspace:0pt; mso-table-lspace:0pt; margin:0; line-height:100%\" width=\"100%\">\n <tr>\n <td align=\"center\" height=\"62\"> </td>\n </tr>\n <tr>\n <td align=\"center\">\n\n <table align=\"center\" width=\"598\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" id=\"messageTable\" style=\"border-collapse:collapse; mso-table-lspace:0pt; border:1px solid #BDBEC3; background-color:white; mso-table-rspace:0pt\" bgcolor=\"white\">\n <tr>\n <td colspan=\"3\">\n <!-- Header Starts -->\n <table width=\"598\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" id=\"hd\" bgcolor=\"#1B1B1B\" style=\"color:#FFFFFF; border-collapse:collapse; height:30px; mso-table-rspace:0pt; mso-table-lspace:0pt; background-color:#1B1B1B\" height=\"30\">\n <tr><td height=\"10\" colspan=\"2\" style=\"line-height:5px\"> </td></tr>\n <tr>\n <td width=\"22\"> </td>\n <td height=\"30\" valign=\"middle\">\n <a href=\"http://www.someapp.com\">\n <img src=\"https://app.someapp.com/media/images/logo-black.png\" alt=\"someapp\" width=\"117\" height=\"30\" border=\"0\" style=\"-ms-interpolation-mode:bicubic; text-decoration:none; border:none; display:block; outline:none\">\n </a>\n </td>\n <td>\n <div style=\"color:#353535; float:right; margin:0 10px 0 0; line-height:24px; font-family:helvetica, arial, sans-serif\">\n <a href=\"http://twitter.com/#!/someapp\" style=\"color:#FFFFFF; font-size:11px; text-decoration:none\"><img src=\"http://www.someapp.com/media/img/twitter.png\" style=\"-ms-interpolation-mode:bicubic; text-decoration:none; border:none; outline:none; margin-right:3px\">@someapp</a>\n <a href=\"http://www.facebook.com/pages/someapp/251257471600587?fref=ts\" style=\"color:#FFFFFF; font-size:11px; text-decoration:none\"><img src=\"http://www.someapp.com/media/img/facebook.png\" style=\"-ms-interpolation-mode:bicubic; text-decoration:none; border:none; outline:none; margin-right:3px\">follow us</a>\n </div>\n </td>\n </tr>\n <tr><td height=\"10\" colspan=\"3\" style=\"line-height:5px\"> </td></tr>\n <tr><td height=\"8\" colspan=\"3\" bgcolor=\"#E8711B\" style=\"line-height:5px\"> </td></tr>\n <tr><td height=\"30\" colspan=\"3\" bgcolor=\"#FFFFFF\"> </td></tr>\n </table>\n <!-- Header Ends -->\n </td>\n </tr>\n <tr>\n <td width=\"85\"> </td>\n <td>\n \n<h1 style=\"color:#353535; font-size:24px; font-family:helvetica, arial, sans-serif; margin:16px 0; line-height:24px\">We found an error!</h1>\n<p style=\"color:#353535; font-size:16px; font-family:helvetica, arial, sans-serif; margin:30px 0 0; line-height:24px\">Search Engine: some-place.net</p>\n\n<p style=\"color:#353535; font-size:16px; font-family:helvetica, arial, sans-serif; margin:30px 0 0; line-height:24px\"><code>Couldn't find the feed: HTTP Error 404: Not Found. Check that you can access the feeds's url..</code></p>\n\n<p style=\"color:#353535; font-size:16px; font-family:helvetica, arial, sans-serif; margin:30px 0 0; line-height:24px\">Please contact us if you need help.</p>\n\n<p style=\"color:#353535; font-size:16px; font-family:helvetica, arial, sans-serif; margin:30px 0 0; line-height:24px\">Thank you!</p>\n\n </td>\n <td width=\"85\"> </td>\n </tr>\n <tr><td colspan=\"3\" height=\"85\" bgcolor=\"#FFFFFF\"> </td></tr>\n <tr><td colspan=\"3\" height=\"12\" bgcolor=\"#1B1B1B\"> </td></tr>\n </table>\n\n </td>\n </tr>\n <tr>\n <td align=\"center\" height=\"62\">\n \n<p style=\"color:#353535; font-size:9px; font-family:helvetica, arial, sans-serif; margin:0; line-height:24px\">To stop receiving these emails from someapp, edit your <a href=\"https://app.someapp.com/admin/config/notifications?search_engine_id=170\" style=\"color:#E8711B\">preferences</a></p>\n\n <p style=\"color:#353535; font-size:9px; font-family:helvetica, arial, sans-serif; margin:0; line-height:24px\">©2014 someapp - Alberto Alcocer, 10 - 28036 Madrid - Spain</p>\n </td>\n </tr>\n </table>\n\n </body>\n</html>\n",
"metadata":{
"user":"USERID"
},
"subject":"[someapp] Error in some-place.net"
},
"key":"THE API KEY HERE"
}
If this is sent inside the task it raises a MandrillAPIError
but if I send it through a HTTP client like Paw it works fine.
I don't understand what's happening. We send transactional e-mails through Mandrill with the same module and everything is working fine.
Any help will be appreciated. Thank you very much.