Bootstrap FreeKB - SendGrid - Sending email with attachment using Python
SendGrid - Sending email with attachment using Python

Updated:   |  SendGrid articles

This assume you are already familiar with sending a basic sendgrid email template using Python. If not, check out my article Sending email using Python.

Here is an example of how to send an email with an attachment. This is just the boilerplate code, no error handling, just to focus on the most important markup.

#!/usr/bin/python3
import base64
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (Mail, Attachment, FileContent, FileName, FileType, Disposition)

message = Mail(
    from_email='from@example.com',
    to_emails='to@example.com',
    subject='hey',
    html_content='Hello <strong>World</strong>')

file_to_attach = "/path/to/attachment.pdf"

with open(file_to_attach , 'rb') as file:
    data = file.read()
    file.close()
    
encoded_file = base64.b64encode(data).decode()

attachedFile = Attachment(
    FileContent(encoded_file),
    FileName(file_to_attach ),
    FileType('application/pdf'),
    Disposition('attachment')
)

message.attachment = attachedFile

sg = SendGridAPIClient('YOUR API KEY')
response = sg.send(message)

print(f"response.status_code = {response.status_code}")
print(f"response.body        = {response.body}")
print(f"response.headers     = {response.headers}")

 

Notice in the prior example that FileType is application/pdf. There are different FileTypes, such as

  • application/pdf
  • application/yaml
  • application/json
  • image/png
  • text/plain

mimetypes or magic can be used to determine the FileType.

#!/usr/bin/python3
import base64
import magic
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (Mail, Attachment, FileContent, FileName, FileType, Disposition)

message = Mail(
    from_email='from@example.com',
    to_emails='to@example.com',
    subject='hey',
    html_content='Hello <strong>World</strong>')

file_to_attach = "/path/to/attachment.pdf"

with open(file_to_attach , 'rb') as file:
    data = file.read()
    file.close()
    
encoded_file = base64.b64encode(data).decode()

mime = magic.Magic(mime=True)
mimetype = mime.from_file(file_to_attach)

attachedFile = Attachment(
    FileContent(encoded_file),
    FileName(file_to_attach ),
    FileType(mimetype),
    Disposition('attachment')
)

message.attachment = attachedFile

sg = SendGridAPIClient('YOUR API KEY')
response = sg.send(message)

print(f"response.status_code = {response.status_code}")
print(f"response.body        = {response.body}")
print(f"response.headers     = {response.headers}")

 

Here is an example of how to attach multiple files.

#!/usr/bin/python3
import base64
import magic
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (Mail, Attachment, FileContent, FileName, FileType, Disposition)

message = Mail(
    from_email='from@example.com',
    to_emails='to@example.com',
    subject='hey',
    html_content='Hello <strong>World</strong>')

attached_files = []

for myfile in ["/path/to/file1.txt", "/path/to/file2.docx"]

  with open(file_to_attach , 'rb') as file:
    data = file.read()
    file.close()
    
  encoded_file = base64.b64encode(data).decode()

  mime = magic.Magic(mime=True)
  mimetype = mime.from_file(file_to_attach)

  attachedFile = Attachment(
    FileContent(encoded_file),
    FileName(file_to_attach ),
    FileType(mimetype),
    Disposition('attachment')
  )

  attached_files.append(attachedFile)

message.attachment = attached_files

sg = SendGridAPIClient('YOUR API KEY')
response = sg.send(message)

print(f"response.status_code = {response.status_code}")
print(f"response.body        = {response.body}")
print(f"response.headers     = {response.headers}")

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b6ed2b in the box below so that we can be sure you are a human.