【问题标题】:Sending emails from PHP using Gmail API使用 Gmail API 从 PHP 发送电子邮件
【发布时间】:2017-05-25 14:51:27
【问题描述】:

我在使用 Gmail API 从 PHP 发送嵌入 HTML 的电子邮件时遇到问题。

代码如下:

$raw_message .= "To:".$email['name']." <".$email['email'].">\r\n";
$raw_message .= 'Subject: =?utf-8?B?' .base64_encode($email_array['subject']) . "?=\r\n";

// Set the right MIME & Content type
$raw_message .= "MIME-Version: 1.0\r\n";
$raw_message .= "Content-Type: text/html; charset=utf-8\r\n";
$raw_message .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";

$mime = rtrim(strtr(base64_encode($raw_message), '+/', '-_'), '=');
$message = new Google_Service_Gmail_Message();
$message->setRaw($mime);

$template = $this->load->view('email/default_email_template', $email_template_array, true);
$raw_message .= $template;

$mime = rtrim(strtr(base64_encode($raw_message), '+/', '-_'), '=');
$message = new Google_Service_Gmail_Message();
$message->setRaw($mime);

$response = $service->users_messages->send($user, $message);

我收到的电子邮件只是纯文本。

可以在这里使用一些帮助。

提前致谢!

编辑:这是我的模板

<body style="height: 100% !important; margin: 0 !important; padding: 0 !important; width: 100% !important;margin: 0 !important; padding: 0 !important;">

<!-- HEADER -->
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse !important;">
<tr>
<td bgcolor="#fff" align="center">
 <!--[if (gte mso 9)|(IE)]>
 <table align="center" border="0" cellspacing="0" cellpadding="0" width="500">
 <tr>
        <td align="center" valign="top" width="500">
        <![endif]-->
        <table border="0" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse !important;max-width: 500px;width: 100% !important; max-width: 100% !important;">
            <tr>
                <td align="center" valign="top" style="padding: 15px 0;margin: 0 auto !important;" class="logo">
                    <a href="https://www.example.com" target="_blank">
                        <img alt="Logo" src="https://example.com/home/images/logo.png" width="90" height="90" style="border: 0; height: auto; line-height: 100%; outline: none; text-decoration: none; display: block; font-family: Helvetica, Arial, sans-serif; color: #ffffff; font-size: 16px;" border="0">
                    </a>
                    <span style="font-size: 40px; color: #000000;">mad</span><span style="font-size: 40px; color: #e18a07;">gigs</span>
                </td>
            </tr>
        </table>
        <!--[if (gte mso 9)|(IE)]>
        </td>
        </tr>
        </table>
        <![endif]-->
    </td>
</tr>
<tr>
    <td bgcolor="#ffffff" align="center" style="padding: 0px 15px 20px 15px;padding: 50px 15px 50px 15px !important;" class="section-padding">
        <table border="0" cellpadding="0" cellspacing="0" width="500" class="responsive-table" style="border-collapse: collapse !important;width: 100% !important;">
            <tr>
                <td>
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-collapse: collapse !important;">
                        <tr>
                            <td>
                                <!-- COPY -->
                                <table width="100%" border="0" cellspacing="0" cellpadding="0" style="border-collapse: collapse !important;">
                                    <tr>
                                        <td align="center" style="font-size: 25px; font-family: Helvetica, Arial, sans-serif; color: #333333; padding-top: 30px;padding: 10px 5% 10px 5% !important;text-align: center" class="padding-copy">HI THERE</td>
                                    </tr>
                                    <tr>
                                        <td align="center" style="padding: 20px 0 0 0; font-size: 16px; line-height: 25px; font-family: Helvetica, Arial, sans-serif; color: #666666;padding: 10px 5% 10px 5% !important;text-align: center;" class="padding-copy">BODY OF THE EMAIL</td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </td>
</tr>
<tr>
    <td bgcolor="#ffffff" align="center" style="padding: 20px 0px;">
        <!--[if (gte mso 9)|(IE)]>
        <table align="center" border="0" cellspacing="0" cellpadding="0" width="500">
        <tr>
        <td align="center" valign="top" width="500">
        <![endif]-->
        <!-- UNSUBSCRIBE COPY -->
        <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" style="border-collapse: collapse !important; max-width: 500px;width: 100% !important;" class="responsive-table">
            <tr>
            </tr>
        </table>
        <!--[if (gte mso 9)|(IE)]>
        </td>
        </tr>
        </table>
        <![endif]-->
    </td>
</tr>
<tr>
    <td bgcolor="#DFE4E6" align="center" style="padding: 20px 0px;">
        <!--[if (gte mso 9)|(IE)]>
        <table align="center" border="0" cellspacing="0" cellpadding="0" width="500">
        <tr>
        <td align="center" valign="top" width="500">
        <![endif]-->
        <!-- UNSUBSCRIBE COPY -->
        <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" style="border-collapse: collapse !important;max-width: 500px;width: 100% !important;" class="responsive-table">
            <tr>
                <td align="center" style="font-size: 12px; line-height: 18px; color:#666666;">

                </td>
            </tr>
        </table>
        <!--[if (gte mso 9)|(IE)]>
        </td>
        </tr>
        </table>
        <![endif]-->
    </td>
</tr>

【问题讨论】:

  • 请分享您的电子邮件/default_email_template 代码。你在使用 css 类吗?
  • 您好,我对整个身份验证/发送过程的源代码感兴趣。我们可以谈谈吗?

标签: php gmail-api url-encoding


【解决方案1】:

在对此进行了数小时的研究后,我终于弄明白了。 我的原始消息中的charset 必须设置为Content-Type: text/html; charset=iso-8859-1 而不是utf-8

更多关于此主题的信息 -

Stackoverflow answer on the difference between UTF-8 and ISO-8859-1

Original article from Wikipidea on ISO-8859-1

现在,我的邮件正在发送,所有样式都很完整。检查了不同的客户,并且在所有客户上都运行良好!

【讨论】:

  • 感谢您回来并为您自己的问题提供答案。这里有一些很好的信息。我还从之前的答案中找到了the third link,这对我的具体情况很有用。
【解决方案2】:

使用内联样式来设计 email/default_email_template 页面。不要使用 css 类。如果您使用类,您的设计将在 Outlook 和其他电子邮件服务工具中可见。但不是在gmail中。 因为 gmail 在 CSS 类名中添加了前置字符。

【讨论】:

    猜你喜欢
    • 2014-10-30
    • 2014-08-27
    • 2018-08-20
    • 2015-10-22
    • 2018-01-26
    • 2021-09-11
    • 2014-09-16
    • 2015-01-02
    • 2017-04-14
    相关资源
    最近更新 更多