sendgrid/sendgrid-python

I cannot send the email unsubscribe variable

lucasthaynan opened this issue · 1 comments

Issue Summary

I have a SendGrid API function to send an email to each contact in the list with a dynamic template. However, I cannot send the variable needed to activate the user's unsubscribe from the newsletter. This below is an example of the Python code I'm using to send the emails.

I get the list of contacts directly from the SendGrid API.

Code Snippet

def send_email(variable_contact):    
    
    api_sendgrid = 'API_SENDGRID'
    
    to_emails = [
        To(email= variable_contact['email'],
           name= variable_contact['name'],
           dynamic_template_data = variable_contact)]
    
    message = Mail(
        from_email=('fromemail@site.com'),
        to_emails=to_emails,
        subject= 'title test', 
        is_multiple=True)
    
    message.template_id = 'ID_template_email_sendgrid'

    try:
        sendgrid_client = SendGridAPIClient(api_sendgrid)
        response = sendgrid_client.send(message)
        print(response.status_code)
    except Exception as e:
        print(e.message)
        
        
for contact in list_contacts:
    
    variable_contact = {'email': contact['email'],
                    'name': contact['name'],
                    'data_x': contact['data_x'],
                    'data_y': contact['data_x']}
    
    # function send email
    send_email(variable_contact)
    

Technical details:

  • sendgrid-python version: 6.9.2
  • python version: 3.9.5