sklarsa/django-sendgrid-v5

Attachment Content-Type "method" Param Not Included

tylerneu opened this issue · 7 comments

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.

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

Would something like this work? 19e09b3 This allows you to set a .method on a MIMEBase object that gets appended to the content type...

stale commented

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.

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'

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

stale commented

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.