微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MSO 条件语句 [if mso] 在通过 Gmail API 发送电子邮件时不起作用

如何解决MSO 条件语句 [if mso] 在通过 Gmail API 发送电子邮件时不起作用

长期倾听者,第一次来访者,就这样吧。

很长一段时间以来,我一直使用 PHP 和 SwiftMailer 从 GSuite 帐户中的多个地址成功发送基本和更高级的 HTML 电子邮件。我一直通过 SMTP 发送这些电子邮件,并使用用户名和密码进行身份验证。这也意味着我需要在所有帐户上启用 Gmail 的安全性较低的应用程序 (LSA)。据我所知,这些电子邮件在任何地方都能很好地显示 - 包括旧版本的 Outlook。

确保 LSA 设置保持打开状态并且我们在系统中有最新的帐户密码,这足以成为想要切换到 API 方法的理由。此外,我已经读到 GSuite 帐户将不允许很快继续使用 LSA 方法——事实上,它应该已经发生了,只是由于大流行而被推迟了。 https://9to5google.com/2019/12/17/g-suite-less-secure-apps/

我认为现在是让 API 方法工作的好时机,而且我能够在 GSuite 中设置邮件服务帐户。我没有使用 Swiftmailer 进行发送,而是选择 Gmail 'quickstart' PHP 库。这在基本 HTML 格式的电子邮件中运行良好。当我说基本时,我指的是标题和强标签以及一些用于字体颜色的内联 css。

一个目标是发送更复杂的 HTML 电子邮件,其中包含更多样式、嵌入的远程图像和一些 MSO 条件语句。尽管正在发送和传递电子邮件,但 MSO 条件语句不起作用。事实上,就像其他标签包括 MS 特定的文档类型内容)一样,当我查看电子邮件的 HTML 源代码时,它似乎被剥离了。同样,同样的 HTML 在 SwiftMailer 和 SMTP 中运行良好。

电子邮件在 Gmail、我的 Android 电子邮件应用和 Thunderbird 中看起来仍然很棒。它们在我尝试过的任何 Microsoft 中看起来都不正确 - 到目前为止,Outlook 07 和 Win 10 Mail。我可能不再需要担心 Outlook 07,但如果 Win 10 Mail 不起作用,我担心的是 Outlook 365 等。

如果我不能使用这些 MSO 条件语句,那将是不幸的。这可能意味着一个新的、更简单的电子邮件模板。然而,根据我所读到的所有内容,这些条件听起来是有效的,尽管它们无效,但应该可以工作。

下面是我一直在尝试发送的非常基本的 HTML 电子邮件,但条件要么不起作用,要么被删除。我还包含了用于发送消息的 Google 服务代码。我已经拉了几天头发了,所以可能有一些明显的我遗漏了。

我尝试了各种编码选项、元标记和各种条件标记,但都没有成功。对此的任何见解表示赞赏。如果 Google 不使用 SMTP,Google 会用 API 剥离一些东西,这对我来说是没有意义的。也许关键是将 Swiftmailer 与 API 一起使用——我一直在努力避免另一个兔子洞。

无论如何,提前致谢。希望有人已经经历过这个并且对这个主题有一些见解。为保护无辜者改名!

代码

<?PHP               
    require_once 'Google/google-api-PHP-client/vendor/autoload.PHP';        
    
    $fromAddress = "validaccount@gsuite.com";
    
    $user_to_impersonate = $fromAddress;
    $sender = $user_to_impersonate;
                                                            
    putenv("GOOGLE_APPLICATION_CREDENTIALS=path_to_my_gmail_service_account_json_file");
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->setSubject($sender);
    $client->setApplicationName("Quickstart");
    $client->setScopes(["https://www.googleapis.com/auth/gmail.compose"]);
    $service = new Google_Service_Gmail($client);       
    
    $fromName = "Sender Name";
    $sender = "validaccount@gsuite.com";
    $toAddress = "me@me.com";
    $toName = "My Name";
    $replyName = "Sender Name";
    $replyAddress = "validaccount@gsuite.com";
        
    $subject = "API Test";
      
    $strBody .= '<!DOCTYPE html>';
    $strBody .= '<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">';
    $strBody .= '<head>';
    $strBody .= '<Meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <Meta name="viewport" content="width=device-width"> <!-- Forcing initial-scale shouldn\'t be necessary -->
        <Meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- Use the latest (edge) version of IE rendering engine -->
        <Meta name="x-apple-disable-message-reformatting">  <!-- disable auto-scale in iOS 10 Mail entirely -->
        <Meta name="format-detection" content="telephone=no,address=no,email=no,date=no,url=no"> <!-- Tell iOS not to automatically link certain text strings. -->
        <Meta name="color-scheme" content="light">
        <Meta name="supported-color-schemes" content="light">';     
    $strBody .= '<title></title>';  
    $strBody .= '</head>';
        
    $strBody .= "<body>";
    
    $strBody .= '<![if !mso]>  HTML meant for non-Outlook clients but it shows up everywhere  <![endif]>';
    
    $strBody .= '<![if mso]>  HTML meant for Outlook clients only but it shows up everywhere  <![endif]>';
    
    $strBody .= '<!--[if mso]><!-->  I read this might work better,but again it shows up everywhere  <!--<![endif]-->';
    
    $strBody .= '<!--[if (gte mso 9)|(IE)]> This had no affect either and shows up eveywhere <![endif]-->';
                
    $strBody .= "Everybody can see this";
    
    $strBody .= "</body></html>";
                                      
    $rawMsgStr = "From: =?utf-8?B?" . base64_encode($fromName) . "?=<{$sender}>\r\n";
    $rawMsgStr .= "To: =?utf-8?B?" . base64_encode($toName) . "?=<{$toAddress}>\r\n";
    $rawMsgStr .= "Reply-To: =?utf-8?B?" . base64_encode($replyName) . "?=<{$replyAddress}>\r\n";
    
    $rawMsgStr .= "Subject: =?utf-8?B?" . base64_encode($subject) . "?=\r\n";
    $rawMsgStr .= "MIME-Version: 1.0\r\n";
    //$rawMsgStr .= "Content-Language: en-US;\r\n";
    $rawMsgStr .= "Content-Type: text/html; charset=utf-8\r\n\r\r";
    //$rawMsgStr .= "Content-transfer-encoding: quoted-printable\r\n";
    //$rawMsgStr .= 'Content-transfer-encoding: base64' . "\r\n\r\n";
    //$rawMsgStr .= 'Content-transfer-encoding: 7bit\r\n\r\n';
    $rawMsgStr .= "{$strBody}\r\n";           
  
    // The message needs to be encoded in Base64URL
    $mime = rtrim(strtr(base64_encode($rawMsgStr),'+/','-_'),'=');
    $msg = new Google_Service_Gmail_Message();
    $msg->setRaw($mime);
    //return $msg;        
    
    $service->users_messages->send($sender,$msg);        
    
    echo "Complete!";         
?>

结果来源(Outlook 2007)

<html lang="en"><head><Meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <Meta name="viewport" content="width=device-width"> 
        <Meta http-equiv="X-UA-Compatible" content="IE=edge"> 
        <Meta name="x-apple-disable-message-reformatting">  
        <Meta name="format-detection" content="telephone=no,url=no"> 
        <Meta name="color-scheme" content="light">
        <Meta name="supported-color-schemes" content="light"><title></title></head><body>  HTML meant for non-Outlook clients but it shows up everywhere    HTML meant for Outlook clients only but it shows up everywhere    I read this might work better,but again it shows up everywhere  Everybody can see this</body></html>

Gmail 中的结果 HTML(Thunderbird 非常相似)

Content-Type: multipart/alternative; boundary="000000000000dc4b5d05bf2b6835"

--000000000000dc4b5d05bf2b6835
Content-Type: text/plain; charset="UTF-8"

 HTML meant for non-Outlook clients but it shows up everywhere HTML meant
for Outlook clients only but it shows up everywhere I read this might work
better,but again it shows up everywhere Everybody can see this

--000000000000dc4b5d05bf2b6835
Content-Type: text/html; charset="UTF-8"
Content-transfer-encoding: quoted-printable


<html lang=3D"en"><head><Meta http-equiv=3D"content-type" content=3D"text/h=
tml; charset=3DUTF-8">
=09    <Meta name=3D"viewport" content=3D"width=3Ddevice-width">=20
=09=09<Meta http-equiv=3D"X-UA-Compatible" content=3D"IE=3Dedge">=20
=09=09<Meta name=3D"x-apple-disable-message-reformatting"> =20
=09=09<Meta name=3D"format-detection" content=3D"telephone=3Dno,address=3Dn=
o,email=3Dno,date=3Dno,url=3Dno">=20
=09=09<Meta name=3D"color-scheme" content=3D"light">
=09=09<Meta name=3D"supported-color-schemes" content=3D"light"><title></tit=
le></head><body>  HTML meant for non-Outlook clients but it shows up everyw=
here    HTML meant for Outlook clients only but it shows up everywhere    I=
 read this might work better,but again it shows up everywhere  Everybody c=
an see this</body></html>

--000000000000dc4b5d05bf2b6835--

解决方法

好吧,我最终证实了这一点。 Gmail API 正在从 HTML 电子邮件中去除评论,并通过去除与 MSO 相关的内容来简化 doctype 标签。这不仅仅是 MSO 条件,它是电子邮件正文中的任何 HTML 注释。所以,我想这是以下内容的重复,正如该线程中的 OP 最近发现的那样,这个问题没有得到解决,在 Google 看来可能是设计使然。

Why does the GMail API strip the doctype and comments from outgoing messages?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。