Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add unsubscribe header? #1045

Open
addsimm opened this issue Feb 17, 2022 · 8 comments
Open

How to add unsubscribe header? #1045

addsimm opened this issue Feb 17, 2022 · 8 comments
Labels
status: waiting for feedback waiting for feedback from the submitter type: getting started question while getting started

Comments

@addsimm
Copy link

addsimm commented Feb 17, 2022

Issue Summary

I'm trying to add an unsubscribe header and the docs are not very clear, then tech support told me to ask here ...

Here is my code from Django 3.9, which is working fine:

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

    for lead_page in lead_paginator.page_range:
        sents = sents + '----------------lead_page: ' + str(lead_page)
        to_emails = []
        for lead in lead_paginator.page(lead_page).object_list:
            pixel_url = 'e=' + lead.lead_email + '&s=' + e_archive.subject_slug

            one_to_email = To(email=lead.lead_email,
                    name=lead.first_name,
                    substitutions={
                            '<< first_name >>': first,
                            '<< pixel_url >>': pixel_url
                    },
                    subject=e_archive.subject)

            to_emails.append(one_to_email)

        message = Mail(
                from_email=('xxxxx@xxxxx.com', 'xxxxx'),
                to_emails=to_emails,
                subject=e_archive.subject,
                html_content=e_html,
                is_multiple=True)

So where do I add an unsubscribe header based off of the pixel_url, so its personalized; something like:

  "headers": {
    "List-Unsubscribe": "<https://xxxxx.com/download/?<< pixel_url >>&p=unsubscribe>"
  }
@childish-sambino
Copy link
Contributor

Does this article help to achieve what you're looking to implement?

https://dgorski.medium.com/setup-unsubscribe-with-sendgrid-api-dynamic-templates-3b942d325d3d

@childish-sambino childish-sambino added status: waiting for feedback waiting for feedback from the submitter type: getting started question while getting started labels Mar 11, 2022
@rakatyal
Copy link
Contributor

Closing due to lack of activity.

@addsimm
Copy link
Author

addsimm commented Apr 12, 2022 via email

@addsimm
Copy link
Author

addsimm commented Apr 12, 2022 via email

@rakatyal rakatyal reopened this Apr 12, 2022
@rakatyal
Copy link
Contributor

Reopening due to user request. Please let us know how we can further help you.

@addsimm
Copy link
Author

addsimm commented Apr 12, 2022 via email

@addsimm
Copy link
Author

addsimm commented Apr 25, 2022

Still waiting on a response, the article cited is too generic to be useful

@tparkertwilio
Copy link

Hello,

I hope you are doing well today.

I read through the primary issue for this query of wanting to add a substitution value into a list-unsubscribe header for the mail send API. I tested this using the following code (which does allow the header to be added of "X-Test-Header", but the value is not substituted):

{
"personalizations": [{
"to": [{
"email": "sendgridtesting@gmail.com"
}],
"subject": "Testing Header Substitution",
"substitutions": {
"-Test-": "12345"
}
}
],
"from": {
"email": "from@example.net"
},
"headers": {
"X-Test-Header": "-Test-"
},
"content": [
{
"type": "text/html",
"value": "<title></title>Here is HTML text."
}
]
}

The above code is similar to the published mail send API for Python here: https://github.com/sendgrid/sendgrid-python/blob/main/examples/mail/mail.py#L95-L131 I added the test header and substitutions. Note that I also tried nesting the header directly in the personalization block of the code and the result was the same (substitution does not occur).

I know that substitutions will not work for every parameter of a mail payload (regardless of if Python VS C#, etc. are used). I recognize that exceptions could be made more clear as to give you understanding for what is and is not possible when using the API. You can include a list-unsubscribe header, for instance; however, usage of the substitution does not work in these headers from my testing and looking over other support articles and issues historically (i.e. looking for other similar cases, not a specific article or issue in which it is directly addressed).

In the interest of helping achieve a similar result, please note that you can add substitutions into HTML and subjects. Because the header is not being replaced, consider making the substitution with a custom unsubscribe link in the HTML of the email instead if it suits as a workaround:

{
"personalizations": [{
"to": [{
"email": "sendgridtesting@gmail.com"
}],
"subject": "Testing Header Substitution",
"substitutions": {
"-Test-": "12345"
}
}
],
"from": {
"email": "from@example.net"
},
"content": [
{
"type": "text/html",
"value": "<title></title>Here is some plain text.<a href="https://www.google.com?unsub_link=-Test-">Here is a link!"
}
]
}

I copied this in the existing support ticket as well for visibility. As it relates to inconsistencies or lack of documentation for how to use the API, I would open a separate issue aside from the one that exists for the header substitution using the mail send API.

Thank you,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for feedback waiting for feedback from the submitter type: getting started question while getting started
Projects
None yet
Development

No branches or pull requests

4 participants