pythonhtmlemailsendgridsendgrid-templates

How to enable SendGrid Reserved Substitution Tags - Python API(Unsubscribe and Display mail in browser)?


I use SendGrid API in Python to send emails when for the Email body I use HTML file that I prepare in advance. I have two problems that are similar (In both I use substitution tags):

  1. I have a link to display the email in browser. I used SendGrid built in substitution tag - {{Weblink}} for the display the email in browser but when using the python API the link is not clickable.

    HTML:
<table class="module" role="module" data-type="text" border="0" cellpadding="0" cellspacing="0" width="100%" style="table-layout: fixed;" data-muid="9a42fcbe-df55-4250-a46c-1f0ce2366551" data-mc-module-version="2019-10-22" align="center">
    <tbody>
      <tr>
        <td style="padding:5px 5px 5px 10px ; line-height:22px; text-align:inherit;background-color:#FFFFFF;" height="100%" valign="top" bgcolor="#FFFFFF" role="module-content">
          <div><div style="font-family: inherit; text-align: center">
           <span style="font-size: 12px">Email not displaying correctly? </span>
           <a href="{{Weblink}}">
             <span style="font-size: 12px; color: #975ffa">View it in your browser</span></a><span style="font-size: 12px">.</span>
          </div></div>
        </td>
      </tr>
    </tbody>
</table>

Screen shot:

enter image description here

  1. I have a button of global unsubscribe that have the same problem - I use the subtitution tag {{{Unsubscribe}}}

HTML

<div data-role="module-unsubscribe" class="module" role="module" data-type="unsubscribe" style="color:#444444; font-size:12px; line-height:20px; padding:16px 16px 16px 16px; text-align:Center;" data-muid="4e838cf3-9892-4a6d-94d6-170e474d21e5">
    <div class="Unsubscribe--addressLine"><p class="Unsubscribe--senderName" style="font-size:12px; line-height:20px;"></p>
      <p style="font-size:12px; line-height:20px;"><span class="Unsubscribe--senderAddress"></span> <span class="Unsubscribe--senderCity"></span> <span class="Unsubscribe--senderState"></span>
         <span class="Unsubscribe--senderZip"></span></p></div><p style="font-size:12px; line-height:20px;">
          <a class="Unsubscribe--unsubscribeLink" href="{{{unsubscribe}}}" target="_blank">Unsubscribe</a>
        </p></div> 

Screen shot:

enter image description here


Python Code To Send Email

import sendgrid
from sendgrid.helpers.mail import *
from bs4 import BeautifulSoup

with open('file.html', 'r') as f:
    # read html file
    contents = f.read()
    # convert BeautifulSoup object
    soup = BeautifulSoup(contents)

# Insert API KEY
sg = sendgrid.SendGridAPIClient(api_key="API_KEY")

# Create Mail 
from_email = Email("SENDER_EMAIL",name="SENDER_NAME")
to_email = To("RECIEVER_EMAIL")
subject = "Title"
content = Content('text/html', str(soup))
mail = Mail(from_email, to_email, subject, content)

# Add BCC
mail.add_bcc("BBC_EMAIL")

# Send Email
res = sg.send(mail)

# Print status code
print(res.status_code)

In both of the problems I don't understand how to generate the correct links(for each case) that will replace the substitution tags - using the SendGrid Python API.

I didn't find an answer here to this problem. But if you do please send me a link and I'll remove the question.

Thanks in advance :)

References:

SendGrid substitution tags


Solution

  • So off the top, unfortunately the Weblink substitution tag is only available when sending email using Marketing Campaigns, not when using any other SendGrid substitution methods.

    In regards to the unsubscribe substitution, there are a few ways to do this with SendGrid but all of them require you first create an Unsubscribe Group. The Unsubscribe Groups page in the SendGrid documentation talks about Unsubscribe Groups and how to create them. Once you've created one you'd provide it to your API request like this:

    mail.message.asm = Asm(GroupId([YOUR_UNSUBSCRIBE_GROUP_ID)).

    Once you have an Unsubscribe Group created, you can focus on your email template.

    The best way to create a template, and the way I would suggest you do it, is via Dynamic Transactional Templates. Templates give you a lot of power to substitute values in your emails (including unsubscribe tags) using the simple Handlebars templating syntax.

    To use Templates you would start by logging into the SendGrid UI and creating a new Template (or uploading a template using the API). In your templates content you add the {{{unsubscribe}}} tag. Once you have a template created you use it by setting the template_id parameter:

    mail.template_id = '[YOUR_TEMPLATE_ID]'

    If you would prefer not to use Dynamic Templates, you can still add unsubscribe substitution to your emails via the API's content attribute but you'd use the global unsubscribe tag: <%asm_global_unsubscribe_raw_url%> instead of {{{unsubscribe}}}.

    For example:

    content = Content('text/html', '<html><head><title></title></head><body>' \
        '<a class="Unsubscribe--unsubscribeLink" \
        href="<%asm_global_unsubscribe_raw_url%>" \
        target="_blank">Unsubscribe</a>' \
        '</body></html>')
    

    Hope that helps.