Sendgrid-csharp: Send mail fails with BadRequest when apostrophe used in sender name

Created on 13 Jun 2016  路  10Comments  路  Source: sendgrid/sendgrid-csharp

On line 26 of Example.cs in the demonstration project for betav3 we have the sender email address.

If we add a name, it mostly works unless you happen to have a surname with an apostrophe in.

For example, these work;
Email from = new Email("[email protected]");
Email from = new Email("[email protected]". "Ryan");

The following result in a BadRequest status code.
Email from = new Email("[email protected]", "Ryan O'Neill");
Email from = new Email("[email protected]", "Ryan O\'Neill");

The output from the console test app is;

BadRequest
{"errors":[{"message":"Bad Request","field":null,"help":null}]}
Connection: keep-alive
X-Frame-Options: DENY
Date: Sun, 12 Jun 2016 22:10:24 GMT
Server: nginx

I'm not certain where the issue is, in the client wrapper or at the server. I suspect both because I have been able to bypass the client and directly post to the server, generating an email by escaping the apostrophe but it comes out as a double apostrophe.

help wanted bug

Most helpful comment

I've been playing with this and can confirm that there is an error in the v3 API endpoint https://api.sendgrid.com/v3/mail/send.

There may be other errors in the client, but I am bypassing that for this test.

I have been posting the following JSON which results in a bad request response code. Removing the apostrophe in the senders surname makes it work.

{"personalizations":[{"to":[{"email":"[email protected]"}],"subject":"my subject"}],"from":{"email":"[email protected]","name":"Ryan O'Neill"},"content":[{"type":"text/plain","value":" "}],"template_id":"d6a75e38-1111-2222-904c-13e9103de025"}

The raw response from the API endpoint;

{"errors":[{"message":"Bad Request","field":null,"help":null}]}

A not great workaround is to use a back tick in place of the apostrophe.

All 10 comments

Hello @RyanONeill1970,

Thanks for bringing this issue to our attention. We will take a look and find out where the fix should happen.

I've been playing with this and can confirm that there is an error in the v3 API endpoint https://api.sendgrid.com/v3/mail/send.

There may be other errors in the client, but I am bypassing that for this test.

I have been posting the following JSON which results in a bad request response code. Removing the apostrophe in the senders surname makes it work.

{"personalizations":[{"to":[{"email":"[email protected]"}],"subject":"my subject"}],"from":{"email":"[email protected]","name":"Ryan O'Neill"},"content":[{"type":"text/plain","value":" "}],"template_id":"d6a75e38-1111-2222-904c-13e9103de025"}

The raw response from the API endpoint;

{"errors":[{"message":"Bad Request","field":null,"help":null}]}

A not great workaround is to use a back tick in place of the apostrophe.

Hi @RyanONeill1970,

I believe the "Bad Request" in this case is because the subject is inside of the personalizations block, instead do:

{
  "content": [
    {
      "type": "text/plain",
      "value": " "
    }
  ],
  "from": {
    "email": "[email protected]",
    "name": "Ryan O'Neill"
  },
  "personalizations": [
    {
      "to": [
        {
          "email": "[email protected]"
        }
      ]
    }
  ],
  "subject": "my subject",
  "template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932"
}

When I try this via a cURL request the apostrophe is handled correctly, meaning the issue is not with the api call. I will now test the library to see where the error is.

How were you trying the call outside of the library?

Here is how I did it:

curl -X "POST" "https://api.sendgrid.com/v3/mail/send" -H "Authorization: Bearer $SENDGRID_API_KEY" -H "Content-Type: application/json" -d @post.json

Where the post.json file has the above content.

The issue is that the single quote ( ' ) isn't support in the Content object either during my testing. The workaround for this issue is that you can use the ` character and your code should work. To be clear, that is the character that is under the tilde character on a US Keyboard layout.

The following (slightly modified from the Readme example) also generates BadRequest:
Content content = new Content("text/plain", "We'll eventually get more information in here!");
Changing "We'll" to "We will" fixes it.
I read in another issue recommending "encoding" it, but that was using text/html. This is text/plain. What sort of encoding should be used? I've tried C# string with \, HTML with ' and '... neither has worked for text/plain content.

I have a fix deploying today @RyanONeill1970 and @RayHAz, thanks for hanging in there with me :)

https://github.com/sendgrid/sendgrid-csharp/pull/257

@RyanONeill1970 @RayHAz,

Here is the fix: https://www.nuget.org/packages/Sendgrid/7.0.3

Thanks again!

Hello guys, I am also facing the similar problem while using apostrophe in the fromname field in one of our php project. When I use the text like "Webner's" in fromname, it just shows "Webners" as a sender name in the email sent. I am using both the methods (using curl-post request and using php library of sendgrid), but I am facing the same problem with both of them. Please provide any solution how should I do this. Thanks in advance.

Manju Kashyap

Hello @Manju-kk,

Would you mind opening a new issue with the following information?

  1. Version of this library you are using.
  2. Version of .NET you are using.
  3. A code snippet to give context.

Thanks!

If you are passing not string value then SendGrid will give you 馃憞

{"errors":[{"message":"Bad Request","field":null,"help":null}]}

Convert your all non-string value to string before sending to the template file.

Happy Coding 馃槉

Was this page helpful?
0 / 5 - 0 ratings