向多个收件人发送原始电子邮件(带附件)

2024-04-23 09:13:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用Python2.7,并试图发送一个带有附件(确切地说是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 = "me@example.com, them@example.com"

# create raw email
msg = MIMEMultipart()
msg['Subject'] = 'Email subject'
msg['From'] = 'me@example.com'
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'

如果我只有一个收件人,它就可以正常工作,所以当我有一个收件人数组和一个以逗号分隔的收件人字符串时,它就会抛出错误。有人有这方面的经验吗?


Tags: csvtokey字符串comsendrawaccess