sendgrid-python: Can't attach csv

Issue Summary

Several months ago I wrote a script that involved sending a csv file in attachment. I tested this locally with success and uploaded it to a virtual machine to run regularly. Now I find that when I try to run this script locally it throws an error, although it continues to run fine on the virtual machine.

If I omit the attachment it runs fine on both systems.

I have no idea if this relates to issue #704

Also I don’t know what the difference is between the sendgrid module and the sendgrid-python module…

Steps to Reproduce

  1. Try to run the Python function below.

Technical details:

  • sendgrid Version: 5.4.1
  • Python Version: 3.6

Virtual machine (where it continues to run):

  • sendgrid Version: 5.4.0
  • Python Version: 3.6

Error: BadRequestsError: HTTP Error 400: Bad Request

Code

import sendgrid
from sendgrid.helpers.mail import Email, Content, Mail, Attachment, Personalization

def sendGrid(recipient, subject, body, file = None, filename = "attachment.csv", bcc = 0):
    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    from_email = Email("myemail@example.com")
    to_email = Email(recipient)
    content = Content("text/plain", body)
    
    mail = Mail(from_email, subject, to_email, content)
    
    if file:
        data = str.encode(file)
        encoded = base64.b64encode(data).decode()
        
        attachment = Attachment()
        attachment.content = encoded
        attachment.type = "text/csv"
        attachment.filename = filename
        attachment.disposition = "attachment"
        attachment.content_id = "Example Content ID"    
        
        mail.add_attachment(attachment)
        
    if bcc == 1:
        mail.personalizations[0].add_bcc(Email("myemail@example.com"))
    
    try:
        response = sg.client.mail.send.post(request_body=mail.get())
    except urllib.HTTPError as e:
        print(e.read())

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 22 (9 by maintainers)

Most upvoted comments

Ah, I was using v6.0.3.

Here is the example for v5.4.1.

Here is the full code I used along with the test file (note that I had to compress the .csv file to upload here, you will need to unzip it):

import base64
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (
    Mail, Attachment, FileContent, FileName,
    FileType, Disposition, ContentId)
try:
    # Python 3
    import urllib.request as urllib
except ImportError:
    # Python 2
    import urllib2 as urllib

message = Mail(
    from_email='dx@sendgrid.com',
    to_emails='elmer.thomas@sendgrid.com',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')
file_path = './test.csv'
with open(file_path, 'rb') as f:
    data = f.read()
    f.close()
encoded = base64.b64encode(data).decode()
attachment = Attachment()
attachment.file_content = FileContent(encoded)
attachment.file_type = FileType('application/pdf')
attachment.file_name = FileName('test.csv')
attachment.disposition = Disposition('attachment')
message.attachment = attachment
try:
    sendgrid_client = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sendgrid_client.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e.message)

test.csv.zip