Search code examples
pythonpython-3.xyagmail

yagmail how to hidden recipients


I'm trying to develop a mass email script by yagmail. My script works properly but what I want is to showing only the receiving recipient's Email in the 'To' field and not all others. E.g. If I send Emails to example1@email.com and example2@email.com, example1 should see example1@email.com and example2 should see example2@email.com in the 'To' field.

Is there a way to achieve this? Or maybe do I have to use loop over each recipient with send_mail?

Here's my script

import yagmail

with open("folder/email.txt") as f:
    recipients = f.read()

yag = yagmail.SMTP('my.username')

email_subject = 'A subject'
embedded_image = yagmail.inline("folder/image.png")
message1 = '<p>lorem impsum lorem ipsum</p>'
message2 = '<p>lorem impsum lorem ipsum</p>'
attachment1 = 'folder/image.png'
attachment2 = 'folder/file.pdf'


yag.send(to = recipients, subject = email_subject, contents = [message1, embedded_image, message2, attachment1, attachment2])

Solution

  • Indeed, there is no way for a single message to have different headers for different recipients. You have to loop over recipients and send a separate message to each.

    A possible alternative might be to use bcc and perhaps put yourself as the explicit to recipient.