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

858 Views Asked by At

I am trying to send e-mails to a list of contacts, along with a blind copy (BCC) to myself, using Yagmail and Python. I couldn't find any examples in the Yagmail documentation that described how to do this. 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.

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
2

There are 2 best solutions below

1
On

You need to tell python which parameter you are inputting. 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')
0
On

I think this code will work, please test:
Yagmail Usage Doc
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(['[email protected]','[email protected]','[email protected]']) #contacts list
all_receivers_bcc = str(['[email protected]','[email protected]','[email protected]'])#contact list
subject = 'SUBJECT HERE'
contents = 'HTML CONTENT HERE'

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