问题
How to tell sendgrid to stop decorating plain text URLs?
Using sendgrid SDK for Python, if that matters.
sendgrid.SendGridClient(username, pwd)
mail = sendgrid.Mail()
mail.set_html(html_message)
mail.set_text(text_message_with_urls) # <-- urls are here
...
sendgrid_client.send(mail)
For HTML-side this already has an answer:
Is it possible to exclude links from tracking
Tracking still needed for HTML parts of mails.
回答1:
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)
来源:https://stackoverflow.com/questions/48397703/sendgrid-to-stop-changing-plain-text-links