Skip to content

ICS file from string example #977

Open
@JoueBien

Description

@JoueBien

There doesn't seem to be an example of how to use FileContent with string data. Therefore I just wanted to drop an example for those looking to do the same

from sendgrid.helpers.mail import Email, Personalization, Mail, Attachment, FileContent, FileName, FileType, Disposition

# set up a reusable string
event_ics_string ="""BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
CLASS:PUBLIC
DTSTART:{}
DTEND:{}
LOCATION:{}
SUMMARY: Klyk | {}
DESCRIPTION:{}
STATUS:CONFIRMED
END:VEVENT
END:VCALENDAR"""

# new mail instance
mail = Mail()

# set up the string that is used for the file contence
ics = ""+event_ics_string
ics = ics.format(
    eventIsoStartString, 
    eventIsoEndString, 
    event_url,
    event_name,
    event_description
)

# convert to correct format for FileContent - becuase it's incapable of hadling a standard string 
data = base64.b64encode(bytes(ics, 'utf-8')).decode()

# set up file atachment
attachedFile = Attachment(
    file_content=FileContent(data),
    file_name= FileName('invite.ics'),
    file_type='text/calendar',
    disposition=Disposition('attachment')
)

# add file to mail
mail.add_attachment(attachedFile)

# continue with presonlisation and then send mail
sg.client.mail.send.post(request_body=mail.get())

of you looking for the date format

date_time_instance.strftime("%A %b %d at %k:%M %p")

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: work in progressTwilio or the community is in the process of implementingtype: docs updatedocumentation change not affecting the code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions