我有一个django应用程序,我使用mandrill发送邮件。 直到最近,我发送的是简单的邮件,而且效果很好。 但现在,我想发送带附件的邮件,所以我改变了我的信息 建设到这个:
def construct_message(self):
content = self.get_content()
attachments = self.kwargs.get('attachments', None)
message = {} if not attachments else MIMEMultipart()
message['subject'] = content['subject']
message['text'] = content['txt']
message['html'] = content['html']
message['from_email'] = self.sender
message['from_name'] = '***'
recipients = self._get_recipients()
if attachments:
message['to'] = ", ".join([r["email"] for r in recipients])
else:
message['to'] = recipients
message['cc'] = []
message['bcc'] = []
if attachments:
for a in attachments:
part = MIMEApplication(open(a,"rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(a))
message.attach(part)
message = message.as_string()
return message
def send_message(self, message):
"""Actually send the message."""
api_key = settings.MANDRILL_API_KEY_INTERNAL if self.is_internal else settings.MANDRILL_API_KEY_EXTERNAL
key_type_str = "internal" if self.is_internal else "external"
logging.debug("Sending mail through %s API key %s" % (key_type_str, api_key))
mandrill_client = mandrill.Mandrill(api_key)
return mandrill_client.messages.send(message=message)
从那时起,我从mandrill API调用得到以下响应:
ValidationError:验证错误:{“message”:“请输入数组”}
你有什么想法,我做错了什么? 谢谢Alex A。