Sendgrid-python: Attaching an ics file to the email

Created on 27 Jun 2016  Â·  9Comments  Â·  Source: sendgrid/sendgrid-python

Hello there, I would like to ask how I may go about attaching an ics file to an email that I'm sending out. Currently here's what i'm doing to attach the ics file.

  1. First we make the content:

attachment_content = get_attachment_content(meetings, event_fullname, event_query.meeting_length, event_query.time_offset)

  1. Then we give it a base64 encoding:
    attachment_content = base64.b64encode(attachment_content)
  2. Then we start adding the attachment to the email:
    mail = SGMail.Mail(from_email, subject, to_email, content)
    attachment = SGMail.Attachment()
    attachment.set_content(attachment_content)
    attachment.set_filename("invite.ics")
    mail.add_attachment(attachment)
  3. Here's the result:
    screen shot 2016-06-27 at 3 21 44 pm

And when I click on download, it works and it is added to my calendar. However, when we were doing it this way:

msg = MIMEMultipart('mixed')
filename = "invite.ics"
attachmentContent = get_attachment_content(meetings, event['eventFullName'], event['meetingLength'], event['timeOffset'])
icsFile = MIMEBase('text', "calender", method="REQUEST", name = filename)
icsFile.set_payload(attachmentContent.encode("utf-8"))
Encoders.encode_base64(icsFile)
icsFile.add_header('Content-Description', filename)
icsFile.add_header('Content-class', "urn:content-classes:calendarmessage")
icsFile.add_header('Filename', filename)
icsFile.add_header('Path', filename)
icsFile.add_header('Charset', 'utf-8')
msg.attach(icsFile)

Our attachment turns out like this:

screen shot 2016-06-27 at 3 26 29 pm

It provides the option of adding to Google Drive while the previous one doesn't.

We manage to send the ics file and it manages to download but I'm wondering whether I could be missing any steps in attaching the ics file. Would appreciate the help. Thanks.

help wanted question

Most helpful comment

Hello Elmer Thomas @thinkingserious ,

Apologies for not replying to the previous messages. Yes, it did work.
Thank you so much for your help!

On Wed, Jul 20, 2016 at 3:29 AM, Elmer Thomas [email protected]
wrote:

Hi @bendtheji https://github.com/bendtheji,

Did your problem get resolved?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/sendgrid/sendgrid-python/issues/179#issuecomment-233740250,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AIMKumNWRVA9tRvdSaZjBmjazWufN9CAks5qXSW2gaJpZM4I-0wK
.

All 9 comments

Hello @bendtheji,

Can you try adding:

attachment.set_type("text/calendar")

@bendtheji

Just checking back in with you.

Hi @bendtheji,

Did your problem get resolved?

Hello Elmer Thomas @thinkingserious ,

Apologies for not replying to the previous messages. Yes, it did work.
Thank you so much for your help!

On Wed, Jul 20, 2016 at 3:29 AM, Elmer Thomas [email protected]
wrote:

Hi @bendtheji https://github.com/bendtheji,

Did your problem get resolved?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/sendgrid/sendgrid-python/issues/179#issuecomment-233740250,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AIMKumNWRVA9tRvdSaZjBmjazWufN9CAks5qXSW2gaJpZM4I-0wK
.

Excellent! Thanks for the follow up.

@bendtheji while attachment.set_type("text/calendar") fixes the issue for GMail - things still don't work right in Outlook without the method="REQUEST" part (as shown in your second example). Were you able to find a way to append method="REQUEST" to Content-Type: text/calendar?

Is there may be a way to use the result of MIMEMultipart() (which generates the Content-Type the proper way) in SendGrid APIv3?

Hello @MihailRussu,

Please follow this thread for updates on this issue: https://github.com/sendgrid/sendgrid-php/issues/250

Currently, the issue is in our backlog and your vote has been added. Thanks!

@thinkingserious @bendtheji I followed this guide and was able to successfully send the email with the invite.

But Gmail is not automatically adding that event to my calendar.
The option to automatically add events from Gmail is enabled in my calendar settings. However, it usually works when some other websites (such as meetup.com, splash-work.com) send the invite, it gets added automatically to my calendar.

Any workaround for this?

Hello @hmharshit,

I'm not aware of any workaround, but suggest that you post this query on StackOverflow as I don't believe this is a SDK issue. I think it's more of a general email client issue.

With Best Regards,

Elmer

Was this page helpful?
0 / 5 - 0 ratings