如何修复“554,交易失败:重复 header 'Subject'”?
How to fixed "554, Transaction failed: Duplicate header 'Subject' "?
发送大量电子邮件时回复此错误554, Transaction failed: Duplicate header subject
。我正在使用 smtplib
+ aws SES。对于所有消息,header 必须相同。我该如何解决这个错误?如果发送没有主题的消息,一切正常。
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())
使用 aws-lambda
时不能使用全局变量。错误在于将重复消息写入变量 msg
.
发送大量电子邮件时回复此错误554, Transaction failed: Duplicate header subject
。我正在使用 smtplib
+ aws SES。对于所有消息,header 必须相同。我该如何解决这个错误?如果发送没有主题的消息,一切正常。
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())
使用 aws-lambda
时不能使用全局变量。错误在于将重复消息写入变量 msg
.