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

BCC return 400 bad request

Open gurmukhspider opened this issue 6 years ago • 10 comments

Bcc return 400 bad request

when i added the email in bcc method it return 400 bad request error.

Here is my snippet:

sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY) from_email = Email(self.from_address) to_list = Personalization() attachments = [] for email in emails: self.email = email to_list.add_to(Email(email))

this code working great but when i replace the last line of snippet "to_list.add_bcc(Email(email))" then it return 400 bad request error

gurmukhspider avatar Jan 24 '19 07:01 gurmukhspider

Hi @gurmukhspider,

Can you please share the request body? Thanks!

With Best Regards,

Elmer

thinkingserious avatar Jan 24 '19 19:01 thinkingserious

hi @thinkingserious,

sorry for delayed reply.. Here's the output of print(mail.get())

{
    "content": [
        {
            "type": "text/html",
            "value": "test...."
        }
    ],
    "from": {
        "email": "[email protected]"
    },
    "personalizations": [
        {
            "bcc": [
                {
                    "email": "[email protected]"
                },
                {
                    "email": "[email protected]"
                }
            ]
        }
    ],
    "subject": "HI This is testing"
}

gurmukhspider avatar Jan 29 '19 06:01 gurmukhspider

Hi @gurmukhspider,

I see the issue now, thanks!

Each personalization needs to have at least one to field.

With Best Regards,

Elmer

thinkingserious avatar Jan 29 '19 18:01 thinkingserious

i have added "to" field but still show 400 bad request error :(

{'from': {'email': '[email protected]'}, 'subject': 'HI This is testing', 'personalizations': [{'to': [{'email': '[email protected]'}], 'bcc': [{'email': '[email protected]'}, {'email': '[email protected]'}]}], 'content': [{'type': 'text/html', 'value': 'testing'}]}

gurmukhspider avatar Jan 30 '19 05:01 gurmukhspider

Can you please try printing out the error message? Thanks!

thinkingserious avatar Jan 30 '19 19:01 thinkingserious

Hi, I have the same error but with cc. I also received the 400 bad request error. In my case, the problem was that is not possible send email in cc if this email is the same that destination mail (to_emails). I'm reviewing your request and you also use {'email': '[email protected]'} in to_email and bcc_email. I think that it could be a bug, because any mail server allow send mails to an email with the same email in copy. Best regards,

p3r1c0 avatar Apr 09 '19 10:04 p3r1c0

Ah, good catch @p3r1c0,

Indeed, Twilio SendGrid does not allow duplicate emails in the Personalization object. Here is the documentation.

Thanks!

That said, I'm leaving this open so that we can return a useful error message.

With Best Regards,

Elmer

thinkingserious avatar Apr 09 '19 15:04 thinkingserious

I ran into this issue as well, but introducing code to prevent duplicates in Personalization does not resolve it. I still get a 400 bad request error even with a distinct cc email address. Here is the request body I am generating:

{
    "from": {
        "email": "[email protected]"
    },
    "personalizations": [
        {
            "to": [
                {
                    "email": "[email protected]"
                }
            ]
        },
        {
            "cc": [
                {
                    "email": "[email protected]"
                }
            ]
        }
    ],
    "subject": "subject",
    "content": [
        {
            "type": "text\/html",
            "value": "Addressed to: name1 and name2
-----

message"
        }
    ]
}

and here is the result:

Array\n(\n [0] => HTTP/1.1 100 Continue\n [1] => \n [2] => HTTP/1.1 400 Bad Request\n [3] => Server: nginx\n [4] => Date: Mon, 23 Sep 2019 19:35:13 GMT\n [5] => Content-Type: application/json\n [6] => Content-Length: 288\n [7] => Connection: keep-alive\n [8] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io\n [9] => Access-Control-Allow-Methods: POST\n [10] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl\n [11] => Access-Control-Max-Age: 600\n [12] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html\n [13] => \n [14] => \n)\n, referer: https://[redacted]

e-med avatar Sep 23 '19 20:09 e-med

@e-med
I think you need to add "to" filed in every index of personalisations as a property along with bcc and cc
Below is a typescript interface for your help ` type EmailData = string|{ name?: string; email: string; } type EmailJSON = { name?: string; email: string }

interface IPersonalizationData {
    to: EmailData | EmailData[], // important
    cc?: EmailData | EmailData[],
    bcc?: EmailData | EmailData[],
    subject?: string;
    headers?: { [key: string]: string };
    substitutions?: { [key: string]: string };
    dynamicTemplateData?: { [key: string]: any; };
    customArgs?: { [key: string]: string };
    sendAt?: number;

} `

mehmoodphaedra avatar May 13 '20 08:05 mehmoodphaedra

Not sure if exactly the same issue but the title certainly matches.

When you pass To and Bcc in the Mail constructor, you get this, and a 400 error:

{
  "from": { "name": "Company", "email": "[email protected]" },
  "subject": "Hello world",
  "personalizations": [
    { "to": [{ "email": "[email protected]" }] },
    { "bcc": [{ "email": "[email protected]" }] }
  ],
  "content": [{ "type": "text/html", "value": "Some Text" }]
}

If you pass the Bcc separately after construction using add_bcc(email), you get this, and success:

{
  "from": { "name": "Company", "email": "[email protected]" },
  "subject": "Hello world",
  "personalizations": [
    {
      "to": [{ "email": "[email protected]" }],
      "bcc": [{ "email": "[email protected]" }]
    }
  ],
  "content": [{ "type": "text/html", "value": "Some Text" }]
}

If you need a 'to' in every Personalization, it makes sense to me that this is a bug with the library as it should produce the second result in the first case.

audoh-tickitto avatar Jul 21 '21 13:07 audoh-tickitto