2017-03-16 23 views
0

當發送大量電子郵件回覆此錯誤554, Transaction failed: Duplicate header subject。我使用smtplib + aws SES。對於所有消息,標題必須相同。我該如何解決這個錯誤?如果發送消息沒有主題,所有工作。如何解決「554,交易失敗:重複標題'主題'」?

import smtplib 
import json 


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

args = [] 
msg = MIMEMultipart('alternative') 
msg['From'] = '[email protected]' 
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()) 
+0

問題是具有相同主題的多條消息......它是一條消息中的兩個主題標題。檢查'msg.as_string()'的結果,它應該變得清楚到底發生了什麼。 –

+0

@ michael-sqlbot請你詳細說明一下嗎? – Andy

+0

記錄'msg.as_string()'返回的值。這是一封電子郵件,完整形成並準備好通過電子郵件發送出去,包含標題和正文,與在Gmail中查看郵件時看到的內容相同,然後單擊「顯示原始」 - 即低級別表示形式消息採用用於傳輸的格式。這個錯誤似乎表明電子郵件標題包含兩個'Subject:'行而不是一個,這將是錯誤的。編輯失敗電子郵件的'msg.as_string()'輸出到問題中。 –

回答

0

當使用aws-lambda時不能使用全局變量。錯誤在於重複消息被寫入變量msg