sklarsa / django-sendgrid-v5

An implementation of Django's EmailBackend compatible with sendgrid-python v5+
MIT License
318 stars 54 forks source link

Attachment Content-Type "method" Param Not Included #86

Closed tylerneu closed 2 years ago

tylerneu commented 3 years ago

A method Content-Type parameter, like intext/calendar; method=REQUEST;, is now an accepted parameter by the SendGrid API to support email clients displaying calendar invite RSVP controls but it is excluded during my use with this package.

Examples of SendGrid announcing method=REQUEST is now supported. I was not able to find reference in their release notes.

This script creates and sends a message with an attachment that has the method parameter using SendGrid's libraries.

import sendgrid
import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))

mail = Mail(
    Email("test@example.com"),
    To("tylerneu@live.com"),
    "Sending with SendGrid is Fun",
    Content("text/plain", "and easy to do anywhere, even with Python")
)

attachment = Attachment()
attachment.file_content = FileContent('FOO')
attachment.file_type = 'text/calendar; method=REQUEST;'
attachment.file_name = 'invite.ics'
attachment.disposition = Disposition('attachment')
attachment.content_id = ContentId('Example Content ID')

mail.attachment = attachment

response = sg.client.mail.send.post(request_body=mail.get())

Creating a MIMEBase message and using this library will not include the method parameter.

part = MIMEText('FOO', 'calendar', 'utf-8')
part.set_param('method', 'REQUEST')

part.add_header('Filename', 'invite.ics')
part.add_header('Content-Disposition', 'attachment; filename=invite.ics')
email_message.attach(part)

In my case, I see that django_attch.get_content_type() in _create_sg_attachment() is only going to return the maintype/subtype and not any additional parameters.

sklarsa commented 3 years ago

Thanks for reporting! I'll take a look and see if there's an easy way to implement this functionality

sklarsa commented 3 years ago

Would something like this work? https://github.com/sklarsa/django-sendgrid-v5/commit/19e09b392d08cee5f60d8de7c6dd20bf2813b2f4 This allows you to set a .method on a MIMEBase object that gets appended to the content type...

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

tylerneu commented 3 years ago

I think so. We're working around this with the following.

class MIMECalendarRequest(MIMEText):
     """
     A child of MIMEText to override get_content_type to ensure a "method" Content-Type param is included since
     email.message.get_content_type() does not consider addition params.
     """
     def get_content_type(self):
         return f'{super().get_content_type()}; method=REQUEST'
sklarsa commented 3 years ago

Nice! Let's get this into v1.1

On Tue, Jun 22, 2021 at 4:40 PM Tyler Neu @.***> wrote:

I think so. We're working around this with the following.

class MIMECalendarRequest(MIMEText): """ A child of MIMEText to override get_content_type to ensure a "method" Content-Type param is included since email.message.get_content_type() does not consider addition params. """ def get_content_type(self): return f'{super().get_content_type()}; method=REQUEST'

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sklarsa/django-sendgrid-v5/issues/86#issuecomment-866317921, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOXCRO3CA3RA4M2IODOUY3TUDYKTANCNFSM442Q5VJQ .

sklarsa commented 3 years ago

This slipped from 1.1 to 1.2.. will address once I have some time

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.