sendgrid-python icon indicating copy to clipboard operation
sendgrid-python copied to clipboard

400 Bad Request When Attaching PDF

Open apardes opened this issue 10 years ago • 15 comments

I'm trying to email a PDF generated by Weasyprint but receiving an error HTTP Error 400: Bad Request which while not very descriptive I believe is due to an issue with the encoding of the attachment.

html_page, css_page = generatePDF(url) # Generates HTML and CSS from URL
pdf = html_page.write_pdf(stylesheets=css_page) # Compiles PDF from HTML and CSS as bytes string

pdf = base64.b64encode(pdf).decode() # Base64 encodes PDF

data = {
    'personalizations' : [
        {
            'to' : [
                {
                    'email' : data['to']
                }
            ],
            'subject' : data['subject']
        }
    ],
    'from' : {
        'email' : data['from']
    },
    'content' : [
        {
            'type' : 'text/plain',
            'value' : data['text']
        },
        {
            'type' : 'text/html',
            'value' : '<html><p>{}</p></html>'.format(data['html'])
        }
    ],
    'reply_to' : {
        'name' : '{}'.format(sender_name),
        'email' : '{}'.format(sender_email)
    },
    'attachments' : {
        'content' : pdf,
        'filename' : data['filename'],
        'type' : 'application/pdf'
    }
}

sg = sendgrid.SendGridAPIClient(apikey = SENDGRID_API_SECRET)
rq = sg.client.mail.send.post(request_body = data)

I've found a similar issue here but the posted solution did not resolve my problem. Thanks.

Technical details:

  • sendgrid-python Version: 3.6.0
  • Python Version: 3.5.1

apardes avatar Oct 25 '16 03:10 apardes

Hello @apardes,

It looks like you are missing a few parameters for the attachments object, please see this example.

You may also want to try this for a better error message.

I hope that helps, Thanks!

thinkingserious avatar Oct 25 '16 21:10 thinkingserious

@thinkingserious Thanks for the response!

Per the documentation here the only required parameters for the attachments object are content and filename. Please advise if this is not the case.

apardes avatar Oct 25 '16 21:10 apardes

@apardes,

Thank you for responding so quickly. You are correct, those other parameters are not required.

  • What is the error message you are receiving after trying my suggestion?
  • To check if the encoding is correct, you may want to compare you output with the following (from your command line):

base64 -i <pdf> -o <encoded_pdf>

thinkingserious avatar Oct 25 '16 21:10 thinkingserious

@thinkingserious All I receive is HTTP Error 400: Bad Request and the encoding is correct.

apardes avatar Oct 28 '16 15:10 apardes

Can you please send the file that you are trying to send to [email protected] so we can investigate further? Thanks!

thinkingserious avatar Oct 28 '16 16:10 thinkingserious

I having that exact same problem with the new api. With different files, i can attach, but the generated pdf by Weasyprint i get

{"errors":[{"message":"The content value must be a string at least one character in length.","field":"content.0.value","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content.value"}]}

and I am pretty sure that the content value has at least one char in length and is a string.

rittersport3 avatar Oct 28 '16 20:10 rittersport3

@thinkingserious I have sent an example file to the provided email. Thanks for your help.

apardes avatar Oct 30 '16 21:10 apardes

@rittersport3 That's interesting. The errors I've been getting are not so explicit. What version of sendgrid-python are you using?

apardes avatar Oct 30 '16 21:10 apardes

@apardes sendgrid==3.6.0 But what would that have to do with the Api response? I imagine that message comes from sendgrid, not from the library.

rittersport3 avatar Oct 31 '16 12:10 rittersport3

@thinkingserious Were you able to learn anything from the example file I sent?

apardes avatar Nov 03 '16 15:11 apardes

Hello @apardes,

Regarding @rittersport3's comments (thanks!), you can extract those messages like so.

With regards to your mailed attachment, I have received it; however, this issue is still working its way up our backlog. I will be posting updates here as they are available.

Thanks!

thinkingserious avatar Nov 08 '16 19:11 thinkingserious

I have same issue. Even I have tried encode using base64 -i <pdf> -o <encoded_pdf> then I copied directly into attachment.set_content(data) still HTTP Error 400: Bad Request :( is there any progress about the problem?

altugank avatar Jan 06 '17 01:01 altugank

@altugank,

I have added your vote to this issue to help it raise higher on our backlog. Thanks!

thinkingserious avatar Jan 26 '17 04:01 thinkingserious

We now have an attachment example here: https://github.com/sendgrid/sendgrid-python/blob/master/USE_CASES.md#attachment

thinkingserious avatar Apr 18 '17 21:04 thinkingserious

The above link doesn't work. Here is the updated link https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/attachment.md

parokshsaxena avatar May 03 '19 10:05 parokshsaxena