发送原始电子邮件(附件)到多个收件人 [英] Send Raw Email (with attachment) to Multiple Recipients

查看:607
本文介绍了发送原始电子邮件(附件)到多个收件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用Python 2.7,并试图用附件(CSV是精确的)与宝途SES多个地址发送电子邮件生我。我可以用 SEND_EMAIL()发送一个正常的电子邮件,但我不断收到尝试时发送给多人通过 send_raw_email()中的错误

I am currently using Python 2.7 and trying to send a raw email with an attachment (CSV to be exact) to multiple addresses with Boto SES. I can send a normal email with send_email(), but I keep getting an error when trying to send to multiple people via send_raw_email().

这是我得到一个逗号分隔的收件人字符串错误:

This is the error that I get with a comma-separated string of recipients:

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>

这是使用此code:

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']
    )

此外,这里是我使用的字符串数组的接收者收到错误:

Also, here is the error I get from using an array of strings for recipients:

Error sending email: 'list' object has no attribute 'lstrip'

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

It works fine if I have just one recipient, so it just throws the error when I have an array of recipients and a comma-separated string of recipients. Anyone have any experience with this?

推荐答案

我结束了它在看一些文件后得到它和一些审判和放大器;错误。事实证明,我刚加入的电子邮件字符串数组的味精['要'] ,然后我可以通过电子邮件阵列中的目的地参数。

I ended it getting it after looking at some docs and some more trial & error. It turns out that I just had to join the array of email strings for the msg['To'], and then I was able to pass in the email array for destinations parameter.

下面是我所做的:

to_emails = "me@example.com, them@example.com"

COMMASPACE = ', '

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

这篇关于发送原始电子邮件(附件)到多个收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆