简体   繁体   English

Yagmail(Python)-如何使用密件抄送/密件副本发送电子邮件

[英]Yagmail (Python) - How to send e-mail with BCC / blind copy

I am trying to send e-mails to a list of contacts, along with a blind copy (BCC) to myself, using Yagmail and Python. 我正在尝试使用Yagmail和Python向联系人列表发送电子邮件以及对自己的密件抄送(BCC)。 I couldn't find any examples in the Yagmail documentation that described how to do this. 我在Yagmail文档中找不到任何描述如何执行此操作的示例。 I know it's possible, but I keep getting an error with my current code. 我知道有可能,但是我的当前代码不断出现错误。

Can anyone help me resolve this? 谁能帮我解决这个问题?

Note: This code works until I add "bcc" as a method-parameter. 注意:直到我将“ bcc”添加为方法参数,此代码才有效。

The Code: 编码:

yag = yagmail.SMTP(
            user={real_sender:alias_sender}, password="xxxxxx", host='smtp.xxxxxx.com', port='587',
            smtp_starttls=True, smtp_ssl=None, smtp_set_debuglevel=0, smtp_skip_login=False,
            encoding='utf-8', oauth2_file=None, soft_email_validation=True)

to = all_receivers ### list of contacts 1
bcc = all_receivers_bcc ### list of contacts 2
subject = 'SUBJECT HERE'
contents = 'HTML CONTENT HERE'

yag.send(to, bcc, subject, contents) ### FAILS HERE WHEN THE "bcc" is added

You need to tell python which parameter you are inputting. 您需要告诉python您要输入哪个参数。 If you don't, you need to make sure parameters are sent in the right order. 如果不这样做,则需要确保以正确的顺序发送参数。 Try this: 尝试这个:

yag.send(to=all_receivers, bcc=all_receivers_bcc , subject='SUBJECT HERE', contents='HTML CONTENT HERE')

I think this code will work, please test: 我认为这段程式码会运作,请测试:
Yagmail Usage Doc Yagmail使用情况文档
This example uses string interpolation to place the variables. 本示例使用字符串插值来放置变量。

yag = yagmail.SMTP(
            user={real_sender:alias_sender}, password="xxxxxx", host='smtp.xxxxxx.com', port='587',
            smtp_starttls=True, smtp_ssl=None, smtp_set_debuglevel=0, smtp_skip_login=False,
            encoding='utf-8', oauth2_file=None, soft_email_validation=True)

all_receivers = str(['aContact1@gmail.com','aContact2@gmail.com','aContact3@gmail.com']) #contacts list
all_receivers_bcc = str(['bbcContact1@gmail.com','bbcContact2@gmail.com','bbcContact3@gmail.com'])#contact list
subject = 'SUBJECT HERE'
contents = 'HTML CONTENT HERE'

yag.send(to='{all_receivers}', subject='{subjects}', contents='{contents}', bcc='{all_receivers_bbc}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM