我目前正在使用 Python 2.7 并尝试使用 Boto SES 将带有附件(确切地说是 CSV)的原始电子邮件发送到多个地址。我可以发送普通电子邮件send_email()
,但是当我尝试通过以下方式发送给多个人时,我不断收到错误消息send_raw_email()
.
这是我在使用逗号分隔的收件人字符串时遇到的错误:
Error sending email: SESIllegalAddressError: 400 Illegal address
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<Error>
<Type>Sender</Type>
<Code>InvalidParameterValue</Code>
<Message>Illegal address</Message>
</Error>
<RequestId>[the request ID]</RequestId>
</ErrorResponse>
这是使用这段代码的结果:
to_emails = "[email protected], [email protected]"
# create raw email
msg = MIMEMultipart()
msg['Subject'] = 'Email subject'
msg['From'] = '[email protected]'
msg['To'] = to_emails
part = MIMEText('Attached is an important CSV')
msg.attach(part)
part = MIMEApplication(open(fname, 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=fname)
msg.attach(part)
# end create raw email
conn = boto.ses.connect_to_region(
'[my region]',
aws_access_key_id=s3_access_key,
aws_secret_access_key=s3_secret_key
)
conn.send_raw_email(msg.as_string(),
source=msg['From'],
destinations=msg['To']
)
另外,这是我使用收件人字符串数组时得到的错误:
Error sending email: 'list' object has no attribute 'lstrip'
如果我只有一个收件人,它就可以正常工作,因此当我有一组收件人和一个以逗号分隔的收件人字符串时,它只会抛出错误。有人对此有经验吗?
在查看了一些文档和更多的试验和错误后,我结束了它。事实证明,我只需加入电子邮件字符串数组即可msg['To']
,然后我可以传递电子邮件数组destinations
范围。
这就是我所做的:
to_emails = "[email protected], [email protected]"
COMMASPACE = ', '
# create raw email
msg = MIMEMultipart()
msg['Subject'] = 'Email subject'
msg['From'] = '[email protected]'
msg['To'] = COMMASPACE.join(to_emails) ## joined the array of email strings
# edit: didn't end up using this ^
part = MIMEText('Attached is an important CSV')
msg.attach(part)
part = MIMEApplication(open(fname, 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=fname)
msg.attach(part)
# end create raw email
conn = boto.ses.connect_to_region(
'[my region]',
aws_access_key_id=s3_access_key,
aws_secret_access_key=s3_secret_key
)
conn.send_raw_email(msg.as_string(),
source=msg['From'],
destinations=to_emails ## passed in an array
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)