PHP Gmail API发送HTML电子邮件

时间:2018-09-07 18:56:03

标签: php email gmail gmail-api

我正在尝试使用GMAIL API发送HTML电子邮件。这是我现在拥有的代码:

    $boundary = uniqid(rand(), true);
    $objGMail = new \Google_Service_Gmail($googleClient);

    $subjectCharset = 'iso-8859-1';
    $strToMailName = "Recipient name";
    $strToMail = "test@gmail.com";
    $strSesFromName = "Mike";
    $strSesFromEmail = "myemail@gmail.com";
    $strSubject = "Some subject";
    $body = "<p>Hi, man. How are you?</p><a href='#'>Some link</a>";
    $strRawMessage = "";
    $strRawMessage .= 'To: ' . $strToMailName . " <" . $strToMail . ">" . "\r\n";
    $strRawMessage .= 'From: '. $strSesFromName . " <" . $strSesFromEmail . ">" . "\r\n";
    $strRawMessage .= 'Subject: =?' . $subjectCharset . '?B?' . base64_encode($strSubject) . "?=\r\n";
    $strRawMessage .= 'Content-Type: text/html; charset=' . $subjectCharset . "\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: 7bit' . "\r\n";
    $strRawMessage .= "--{$boundary}\r\n";
    $strRawMessage .= 'Content-Type: text/html; charset=' . $subjectCharset . "\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
    $strRawMessage .= $body . "\r\n";

    try {
        $mime = strtr(base64_encode($strRawMessage), '+/', '-_');
        $msg = new \Google_Service_Gmail_Message();
        $msg->setRaw($mime);
        $objSentMsg = $objGMail->users_messages->send("me", $msg);
    } catch (\Exception $e) {
        //handler
    }

问题是我正在接收HTML格式的消息(没关系),但是在消息顶部,我有以下内容:

--2879837965b92c7e9524748.36333789 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Date: Fri, 7 Sep 2018 11:48:09 -0700 Message-Id:

邮件的“正文”中似乎包含了邮件的某些部分。但是我不知道为什么会这样。请参见下面的屏幕截图: enter image description here

0 个答案:

没有答案