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

ICS file from string example #977

Open
JoueBien opened this issue Mar 10, 2021 · 3 comments
Open

ICS file from string example #977

JoueBien opened this issue Mar 10, 2021 · 3 comments
Labels
status: work in progress Twilio or the community is in the process of implementing type: docs update documentation change not affecting the code

Comments

@JoueBien
Copy link

JoueBien commented Mar 10, 2021

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")
@JenniferMah JenniferMah added status: work in progress Twilio or the community is in the process of implementing type: docs update documentation change not affecting the code labels Mar 10, 2021
@JenniferMah
Copy link
Contributor

Hi @JoueBien thanks for taking the time to create this example! Would you mind opening a PR to add this to our use-cases directory.

@JenniferMah JenniferMah added status: waiting for feedback waiting for feedback from the submitter and removed status: work in progress Twilio or the community is in the process of implementing labels Mar 11, 2021
@JoueBien
Copy link
Author

@JenniferMah No problem. I've just finished updating my own doc so I'll add a PR over the next 7 days.

@JenniferMah JenniferMah added status: work in progress Twilio or the community is in the process of implementing and removed status: waiting for feedback waiting for feedback from the submitter labels Mar 12, 2021
@JenniferMah
Copy link
Contributor

Ok perfect! Just make sure to include issue #977 in your PR description so that it can be linked to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: work in progress Twilio or the community is in the process of implementing type: docs update documentation change not affecting the code
Projects
None yet
Development

No branches or pull requests

2 participants