如何修复" 554,交易失败:重复标题'主题' &#34 ;?

时间:2017-03-16 17:03:32

标签: python python-2.7 amazon-ses smtplib

发送大量计算电子邮件时,会发出此错误554, Transaction failed: Duplicate header subject。我使用smtplib + aws SES。对于所有消息,标头必须相同。我该如何解决这个错误?如果发送没有主题的消息,则全部正常工作。

import smtplib
import json


from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

args = []
msg  = MIMEMultipart('alternative')
msg['From'] = 'noreply@example.com'
html = open('mail.html').read()


EMAIL_HOST = 'email-smtp...'
EMAIL_HOST_USER = 'sss'
EMAIL_HOST_PASSWORD = 'ssssss'
EMAIL_PORT = 587

def lambda_handler(event, context):
    body = event['Records'][0]['Sns']['Message']
    global args
    args = json.loads(body)['args']
    set_worker(json.loads(body)['method'])()

    return 'success'


def set_worker(method):
    return {
                'email'           : email
            }.get(method, 'Not found')


def email():
    global msg, html

    name = args[0]
    title          = args[1]
    msg_body       = args[2]
    email          = args[3]
    url            = args[4]
    subject        = "Test"

    msg['Subject'] = subject
    msg['To'] = email

    html = html.format(title, community_name, title, msg_body, community_name)
    mime_text = MIMEText(html, 'html')
    msg.attach(mime_text)
    send_message()

def send_message():
    mail = smtplib.SMTP(EMAIL_HOST, EMAIL_PORT)
    mail.ehlo()
    mail.starttls()
    mail.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
    mail.sendmail(msg['From'], msg['To'], msg.as_string())

1 个答案:

答案 0 :(得分:0)

使用aws-lambda时无法使用全局变量。错误在于重复的消息被写入变量msg