Skip to content

Instantly share code, notes, and snippets.

@LiYChristopher
Created April 3, 2017 19:35
Show Gist options
  • Save LiYChristopher/46fe018ad91e86a242aae058c97df4b3 to your computer and use it in GitHub Desktop.
Save LiYChristopher/46fe018ad91e86a242aae058c97df4b3 to your computer and use it in GitHub Desktop.
Pseudo-code for Sending Attachments
'''
Pseudo-code example of sending an attachment via SendGrid's API lib. This assumes that
you're using one of our coding libraries with helper methods (though this is more or less a Python library example)
'''
import sendgrid
from sendgrid.helpers.mail import *
import datetime
import base64
mail = Mail()
attachment = Attachment()
with open(stats_file, 'rb') as data: # you must read the file in binary
attachment.set_content(base64.b64encode(data.read()).decode()) # Encode data in base-64
attachment.set_type('image/<file_extension>') # jpg, tiff etc.
attachment.set_filename('name_of_file')
attachment.set_disposition("attachment")
attachment.set_content_id("<arbitrary_value>")
mail.add_attachment(attachment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment