Sendgrid-php: How to set the encoding for the mail?

Created on 22 Sep 2015  ·  13Comments  ·  Source: sendgrid/sendgrid-php

How to set the encoding (charset) for the mail ?

thx

help wanted bug

Most helpful comment

I also have this issue, @alberto-alvarez, regarding latin special chars in the Subject, in my case is with the word "Recuperação";

The curious thing about this, is that this unexpected behavior happens only if I use the "AddSubstitution" method, (also with special characters in the value).

I infer this due a simple test (commenting the lines containing the above mentioned method) that reproduces the error.

Original error:
image

Reproduction:
image

Raw Mail Data

1) Special chars ok
http://pastebin.com/XvFphMhA

2) With error
http://pastebin.com/P1Wev0q2

Comparison

1) Special chars ok

Message-ID: <[email protected]>
Subject: =?UTF-8?B?TmV4dG8gLSBSZWN1cGVyYcOnw6NvIGRlIFNlbmhh?=

2) With error

Message-ID: <[email protected]>
Subject: ***** - Recupera??o de Senha

Solution

In order to avoid this bug, I had to normalize the subject string with _Normalization Form Compatibility Decomposition_, which is done through the String class' Normalize method:

subject = subject.Normalize(System.Text.NormalizationForm.FormKD);

image

Raw data after normalization:
http://pastebin.com/9NUfdFUL

_references:_
http://stackoverflow.com/a/17577154/1059803
http://stackoverflow.com/questions/20674577/how-to-compare-unicode-characters-that-look-alike/20674872#20674872
https://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms
https://msdn.microsoft.com/en-us/library/ebza6ck1.aspx?f=255&MSPPError=-2147217396

Regards,
Leonardo P. Baggio

All 13 comments

What encoding are you trying to use? Could you give me some example content?

@thinkingserious I'm trying to use charset=iso-8859-1

Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1
Date: Thu, 15 Oct 2015 14:35:19 +0000

@alberto-alvarez Can you give me some sample content? Thanks!

@thinkingserious I'm sorry but i don't have a sample content. The _three line HTTP header_ can't help ?
What exactly do you need?
thanks!

@alberto-alvarez

Just wanted some sample text that I can run through the system and make sure it exits as intended. But no worries, I'll create my own from here: http://www.w3schools.com/charsets/ref_html_8859.asp

Thanks for taking the time to answer!

New information

The body/message of the email is Ok (i guest that encoding was converted) but the real problem was with the subject (specially the character ç but i think there will be more ...)

Example subject content opened in browser:
image

Hope this helps!

Hello @alberto-alvarez,

thanks for the additional details!

Unfortunately, I was not able to reproduce the error, here is the code I used and the resulting received email:

<?php
require 'vendor/autoload.php';
require 'lib/SendGrid.php';

Dotenv::load(__DIR__);
$sendgrid_apikey = getenv('SG_KEY');
$sendgrid = new SendGrid($sendgrid_apikey);

$email = new SendGrid\Email();

$email
    ->addTo('[email protected]')
    ->setFrom('[email protected]')
    ->setSubject('Testing the PHP Library with special character ç')
    ->setText('I\'m text with special character ç!')
    ->setHtml('<strong>I\'m HTML with special character ç!</strong>')
;

$sendgrid->send($email);

Result: http://www.screencast.com/t/gfGowz6aTCf

Could you please provide more detail so that I may reproduce your error. Thanks!

@alberto-alvarez just checking in

@thinkingserious sorry for my long delay to answer you question.

Steps to reproduce my error:

  1. ensure your test file encoding is ISO-8859 text; with sublime editor is 'Western (Windows 1252)'
  2. change the Subject to 'Testing the PHP Library with special character ç inscrição e correções'
  3. just send it

Your content example works with me with UTF-8 file encoding. If a change the enconding and keep your subject, it's still work ;-)

The problem appears when i add the words inscrição e correções (or even probably more ..) to the subject and change the encoding to ISO-8859

thanks in advance

image

I also have this issue, @alberto-alvarez, regarding latin special chars in the Subject, in my case is with the word "Recuperação";

The curious thing about this, is that this unexpected behavior happens only if I use the "AddSubstitution" method, (also with special characters in the value).

I infer this due a simple test (commenting the lines containing the above mentioned method) that reproduces the error.

Original error:
image

Reproduction:
image

Raw Mail Data

1) Special chars ok
http://pastebin.com/XvFphMhA

2) With error
http://pastebin.com/P1Wev0q2

Comparison

1) Special chars ok

Message-ID: <[email protected]>
Subject: =?UTF-8?B?TmV4dG8gLSBSZWN1cGVyYcOnw6NvIGRlIFNlbmhh?=

2) With error

Message-ID: <[email protected]>
Subject: ***** - Recupera??o de Senha

Solution

In order to avoid this bug, I had to normalize the subject string with _Normalization Form Compatibility Decomposition_, which is done through the String class' Normalize method:

subject = subject.Normalize(System.Text.NormalizationForm.FormKD);

image

Raw data after normalization:
http://pastebin.com/9NUfdFUL

_references:_
http://stackoverflow.com/a/17577154/1059803
http://stackoverflow.com/questions/20674577/how-to-compare-unicode-characters-that-look-alike/20674872#20674872
https://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms
https://msdn.microsoft.com/en-us/library/ebza6ck1.aspx?f=255&MSPPError=-2147217396

Regards,
Leonardo P. Baggio

@tomahawkzx, thanks for sharing your solution!

@alberto-alvarez I was able to reproduce your error by changing the file's encoding as you suggested. The fix was:

$subject = utf8_encode("Hello World from the SendGrid PHP Library ç");

Here was my full test code:

<?php
require 'vendor/autoload.php';

$from = new SendGrid\Email(null, "[email protected]");
$subject = utf8_encode("Hello World from the SendGrid PHP Library ç");

$to = new SendGrid\Email(null, "[email protected]");
$content = new SendGrid\Content("text/plain", "some text here");
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo $response->headers();
echo $response->body();

wrong post . . . sorry

@thinkingserious I'm using Sendgrid to send marketing email, but I'm in trouble with email which have url in content. When my url have space in it, the space has been encode to '+'' instead of '%20' , I need my url encode to '%20'. How can I encode space to '%20' ?

Ex: 'This email have url : https://example.com/images/this is space.png'
What I received be like : 'This email have url : https://example.com/images/this+is+space.png'
What I want : 'This email have url : https://example.com/images/this%20is%20space.png'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

atsareva picture atsareva  ·  4Comments

buddyy93 picture buddyy93  ·  5Comments

solonifer picture solonifer  ·  3Comments

FilipLukac picture FilipLukac  ·  4Comments

iamanupammaity picture iamanupammaity  ·  4Comments