使用 gmail api 发送电子邮件时等号丢失

Equal symbol is lost when sending an email with gmail api

我正在使用 gmail-api 向数据库中的不同联系人发送电子邮件。 当我发送消息时,样式不起作用,因为等号“=”丢失了。

例如,在邮件中放入这个

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

但是我在放 =

的地方得到了这个
<img src"mysite.com/image.jpg"/>

这是我的函数的一部分,我在其中为消息制作字符串

    $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);

我在这里对消息进行编码

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;
}

我在我的函数 createMessage() 上尝试了不同的方法,但我仍然遇到这个问题。

为了解决这个问题,我将 = 转换为十六进制(= 十六进制是 3D)

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

这样我在发送消息时不会丢失字符。

由于您使用 "quoted printable" 作为内容编码,等号是转义字符,必须显式编码为 =3D

参见: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.