另一个程序向我的脚本发送已经完成的信:

http://pastebin.com/XvnMrKzE

因此,我解析from_emailto_email,对文本进行一些更改,然后使用mailjet发送。

当我用smtp执行此操作时:

def send(sender, to, message):
    smtp = smtplib.SMTP(SERVER, PORT)
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    smtp.login(USER,PASSWORD)
    logger.info('Sending email from %s to %s' % (sender, to))
    smtp.sendmail(sender, to, message)
    logger.info('Done')
    smtp.quit()


工作正常。然后,我需要使用mailjet。我创建了类似的功能:

def send_with_mailjet(sender, to, message):
    mailjet = Client(auth=('key', 'key'))
    email = {
        'FromName': 'Support',
        'FromEmail': sender,
        'Subject': 'Voice recoginition',
        'Text-Part': message,
        'Html-part': message,
        'Recipients': [{'Email': to},]
    }
    logger.info('Sending email from %s to %s' % (sender, to))
    result = mailjet.send.create(email)
    logger.info('Done. Result: %s' % result)


但是我收到文本,而不是邮箱附件。

最佳答案

您应该使用由Mailjet维护的API客户端的官方Mailjet包装器。根据文档中的指定,这是发送附件的方式:http://dev.mailjet.com/guides/?python#sending-with-attached-files

"""
This calls sends an email to the given recipient.
"""
from mailjet import Client
import os
api_key = os.environ['MJ_APIKEY_PUBLIC']
api_secret = os.environ['MJ_APIKEY_PRIVATE']
mailjet = Client(auth=(api_key, api_secret))
data = {
  'FromEmail': '[email protected]',
  'FromName': 'Mailjet Pilot',
  'Subject': 'Your email flight plan!',
  'Text-part': 'Dear passenger, welcome to Mailjet! May the delivery force be with you!',
  'Html-part': <h3>Dear passenger, welcome to Mailjet!</h3>May the delivery force be with you!',
  'Recipients': [{ "Email": "[email protected]"}],
  'Attachments':
        [{
            "Content-type": "text/plain",
            "Filename": "test.txt",
            "content": "VGhpcyBpcyB5b3VyIGF0dGFjaGVkIGZpbGUhISEK"
        }]
}
result = mailjet.send.create(data=data)
print result.status_code
print result.json()

关于python - mailjet以txt格式发送带有附件的电子邮件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37490401/

10-12 22:20