Equal symbol is lost when sending an email with gmail api

Viewed 764

Im using gmail-api to send emails to different contacts from my database. When i send the message, the styles dont work as this is happening because the equal symbol "=" is lost.

For example, in putting this in the message

<img src="mysite.com/image.jpg"/>

But instead i got this where i put =

<img src"mysite.com/image.jpg"/>

This is the part of my function where i make the string for the message

    $strSubject = $data['subject'];
    //$strRawMessage = "From: myAddress<pblanco@mysite.com>\r\n"; 
    $strRawMessage = "From: <".$data['from'].">\r\n"; //email consultor
    //$strRawMessage .= "To: toAddress <pblanco@mysite.com>\r\n";
    $strRawMessage .= "To: <".$data['to'].">\r\n"; //email destinatario
    $strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=\r\n"; //asunto
    $strRawMessage .= "MIME-Version: 1.0\r\n";
    $strRawMessage .= "Content-Type: text/html; charset=utf-8\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
    $strRawMessage .= $data['message']."\r\n"; //mensaje
    // The message needs to be encoded in Base64URL
    $msg = $this->createMessage($strRawMessage);

And here i do the encoding for the message

public function createMessage($string){
    //$mime = rtrim(strtr(base64_encode($string), '+/', '-_'), '=');
    $mime = strtr(base64_encode($string), array('+' => '-', '/' => '_'));
    $message = new Google_Service_Gmail_Message();
    $message->setRaw($mime);
    return $message;
}

I tried with different methods on my function createMessage() but im still having this issue.

2 Answers

Since you're using "quoted printable" as the content encoding, equal signs are escape characters and have to be explicitly encoded as =3D.

See: https://en.wikipedia.org/wiki/Quoted-printable

Any 8-bit byte value may be encoded with 3 characters: an = followed by two hexadecimal digits (0–9 or A–F) representing the byte's numeric value. For example, an ASCII form feed character (decimal value 12) can be represented by "=0C", and an ASCII equal sign (decimal value 61) must be represented by =3D. All characters except printable ASCII characters or end of line characters (but also =) must be encoded in this fashion.

To solve this issue i convert = to hex (= hex is 3D)

    $strRawMessage .= strtr($data['message'], array('=' => '=3D'))."\r\n"; 

This way i dont lose the character when the message is sended.

Related