Search code examples
pythonsendgrid

Python Sendgrid add CC to email


I am using SendGrid for Python. I want to CC some people in an email. It seems like they may no longer support CC'ing on emails, though I'm not positive if that's true? But surely there is a work around to it somehow, but I am surprised I can't find much support on this.

Here is my basic code:

sg = sendgrid.SendGridAPIClient(apikey='*****')
from_email = Email(sender_address, sender_name)
to_email = Email(email_address)
subject = subject
content = Content("text/plain", email_message)
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())

How can I modify this so it will CC someone on an email?


Solution

  • I resolved it. Santiago's answer got me mostly there, but here is what I needed to do:

    sg = sendgrid.SendGridAPIClient(apikey='****')
    from_email = Email(sender_address, sender_name)
    to_email = Email(to_email)
    cc_email = Email(cc_email)
    p = Personalization()
    p.add_to(to_email)
    p.add_cc(cc_email)
    subject = subject
    content = Content("text/plain", email_message)
    mail = Mail(from_email, subject, to_email, content)
    mail.add_personalization(p)
    response = sg.client.mail.send.post(request_body=mail.get())
    

    If you don't include the p.add_to(to_email) it rejects it because there is no "to email" in the personalization object. Also, if you don't include the "to_email" inside the mail object it rejects it because it is looking for that argument, so you have to be a bit redundant and define it twice.