I contacted support and they informed me that I could use Clicktrack SMTP filter with the X-SMTPAPI
header.
There are two filter options, one is the enable and disable the click-tracking setting and the other for enabling or disabling click-tracking links in the Plain-Text portion of the message.
The Sendgrid Python client has helper class ClickTracking to control these settings:
import json
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import *
sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
from_email = Email("team@example.com")
to_email = To("test@example.com")
subject = "Sending with ClickTracking plain text disabled"
content = Content(MimeType.text, "Test URL: https://www.google.com")
message = Mail(from_email, to_email, subject, content)
tracking_settings = TrackingSettings()
tracking_settings.click_tracking = ClickTracking(enable=True, enable_text=False)
message.tracking_settings = tracking_settings
response = sendgrid_client.send(message=message)
print(response.status_code)
A flask-mail example setting the `X-SMTPAPI' header with clicktrack filter:
from flask_mail import Mail
from flask_mail import Message
mail = Mail(app)
subject = "Sending with clicktrack plain text disabled"
body = "Test URL: https://www.google.com"
headers = {"X-SMTPAPI": json.dumps({
"filters" : {
"clicktrack" : {
"settings" : {
"enable" : 1,
"enable_text" : False
}
}
}
})}
message = Message(subject=subject, body=body, extra_headers=headers)
mail.send(message)