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

标签 php encoding google-api gmail-api google-api-php-client

我正在使用 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() 上尝试了不同的方法,但我仍然遇到这个问题。

最佳答案

由于您使用“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.

关于php - 使用 gmail api 发送电子邮件时等号丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47175810/

相关文章:

php - 在PHP中一遍又一遍打开MySQL连接有什么影响

vim - .vimrc 中的键映射(重音)和编码问题

flutter - 在向 Google Route API 发送 HTTP Post 请求期间发生错误,

java - 谷歌 api androidpublisher 错误代码 403

php - 无法使用 000webhost 连接到数据库

php - 完整的日历事件数据

linux - 如何连接来自 DVB 卡的 ATSC 流?

java - Desktop.Action.MAIL 为 mailto : in URI 正确编码主题和正文字符串

android - 从哪里获取 Google Pay 服务的 Java 软件包?

php - PDO Insert 仅插入一些数据