'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 1:[1]

Using the SendGrid's Personalization() or Email() class did not work for me. This is how I got it to work:

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Cc

# using a list of tuples for emails
# e.g. [('[email protected]', '[email protected]'),('[email protected]', '[email protected]')]
to_emails = []
for r in recipients:
  to_emails.append((r, r))

# note the Cc class
cc_emails = []
for c in cc:
  cc_emails.append(Cc(c, c))

message = Mail(
  from_email=from_email,
  to_emails=to_emails,
  subject='My Subject',
  html_content=f'<div>My HTML Email...</div>'
)

if cc_emails:
  message.add_cc(cc_emails)

try:
  sg = SendGridAPIClient(os.getenv('SENDGRID_API_KEY'))            
  sg.send(message)  
except Exception as e:            
  print(f'{e}')

Hopefully this helps someone.

Solution 2:[2]

I've been looking at the code: https://github.com/sendgrid/sendgrid-python/blob/master/examples/mail/mail.py

And it looks like you can do that by adding a personalization to the mail, for example:

cc_email = Email(cc_address)
p = Personalization()
p.add_cc(cc_email)
mail.add_personalization(p)

Solution 3:[3]

Based on the answers here you can CC to email if you add another email to 'to_email'.

Solution 4:[4]

If you want to cc multiple user then in djanogo using sendgrid you need to import the below line enter image description here

the function that will be used to send the mail enter image description here

and finally how you ned to send the data paramters to the above function so that it can CC the person

email = send_sandgridmail(sender=sender,receiver=receivers,subject=subject,content=message,reply_to=sender,cc=[admin_mail_account_mail,"[email protected]"],attachment=None)

Solution 5:[5]

i hope this'll help.simplified from @anurag image script

import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import To,Mail,ReplyTo,Email,Cc
def send_sandgridmail (sender, receiver, subject, content, reply_to=None, cc=[], attachment=None) :
    # content = convert_safe_text(content)
    # to email = To(receiver)
    message = Mail(
        from_email=str(sender),
        to_emails=receiver,
        subject= str(subject),
        html_content = content)

    if reply_to:
        message.reply_to= ReplyTo(reply_to)

    if attachment:
        message.add_attachment (attachment)

    if len(cc):
        cc_mail = [] 
        for cc_person in cc:
            cc_mail.append(Cc(cc_person, cc_person)) 
        message.add_cc (cc_mail)

    try:
        SENDGRID_API_KEY = 'your sendgrid api key'
        sg= SendGridAPIClient (SENDGRID_API_KEY)
        response= sg.send(message)
        print (response.status_code)
        # print (response.body)
        # print (response.headers)
    except Exception as e:
        print(e)
        return response

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nathan
Solution 2 Santiago Bruno
Solution 3 Ashutosh Chapagain
Solution 4 Anurag
Solution 5 iampritamraj