Skip to content

Can't attach csv #779

Description

@tomwwagstaff-product

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())

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions