设置...
我以通常的方式通过PHP发送电子邮件...
代码块0
mail('', $subject, $message, $headers);
Run Code Online (Sandbox Code Playgroud)
...具有以下内容设置:
代码块1
$boundary = uniqid('np');
$message = '';
$subject = 'Email Subject';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: SURL <noreply@someurl.com>\r\n";
$headers .= "To: ".$email."\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
// Plain Text
$message .= "Content-Type: text/plain; charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= 'Hi, you handsome SOB!!
\n\n
We have ...
/n/n
And ...
\n\n
http://someurl.com/.../.../?a=' . $var1 . '&b=' . $var2;
$message .= "\r\n\r\n--" . $boundary . "\r\n";
// HTML
$message .= "Content-Type: text/html; charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$message .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Some Title</title>
<style type="text/css">
... Lots of styles...
</style>
</head>
<body>
<div class="message_container">
<div class="message_logo"></div>
<div class="message_leading_line">
Hi, you handsome SOB!!
</div> <!-- end message_leading_line -->
<div class="message_top_content">
We have ...
<br/>
And ...
<a href="http://someurl.com/.../.../?a=' . $var1 . '&b=' . $var2 . '" >visit this link</a>.
</div> <!-- end message_top_content -->
<div class="message_bottom_content">
If link doesn\'t work, copy and paste...
<pre>http://someurl.com/.../.../?a=' . $var1 . '&b=' . $var2 . '</pre>
</div> <!-- end message_bottom_content -->
</div> <!-- end message_container -->
</body>
</html>';
$message .= "\r\n\r\n--" . $boundary . "--";
Run Code Online (Sandbox Code Playgroud)
而且一切正常。电子邮件发送顺利。查看电子邮件的格式正确,所有内容都应放置在其中,包括所有动态添加的内容。
迄今为止的行为...
到目前为止,我已经尝试了许多测试用例,并且所有测试用例都按缩进格式发送。样式和所有。
这样一个成功的案例有:
代码块3-1、3-2
$var1 = '4pD9051LsVtQu96pLBH41019v28T0o4Z2I3U6urs';
$var2 = 'verPkBE415i447V6R9o';
Run Code Online (Sandbox Code Playgroud)
...因此,电子邮件中的链接为:
http://someurl.com/.../.../?a=4pD9051LsVtQu96pLBH41019v28T0o4Z2I3U6urs&b=verPkBE415i447V6R9o
Run Code Online (Sandbox Code Playgroud)
电子邮件发送正确,链接显示正确,链接有效(单击并复制/粘贴)。完善!
问题...
对于最近的测试:
代码块4-1、4-2
$var1 = '73jbzUN90j27ME5N6W4jh24o992V91m3R632Hlu0';
$var2 = 'avr1owgJAAB3h4l1brw';
Run Code Online (Sandbox Code Playgroud)
...因此链接是:
http://someurl.com/.../.../?a=73jbzUN90j27ME5N6W4jh24o992V91m3R632Hlu0&b= avr1owgJAAB3h4l1brw
Run Code Online (Sandbox Code Playgroud)
这次,电子邮件发送正常了吗?发送时没有明显问题。乍一看,一切都应有的样子。所有样式都是应有的样式,所有内容都在需要的地方。
注意:显示的Gmail版本是后一个版本,所有标记均已删除并以纯文本形式显示。仍然显示了错误的URL。
但是,经过仔细检查,电子邮件客户端(Apple Mail和Gmail,两个单独的帐户)中显示的链接为:
http://someurl.com/.../.../?asjbzUN90j27ME5N6W4jh24o992V91m3R632Hlu0&b=ver1owgJAAB3h4l1brw
Run Code Online (Sandbox Code Playgroud)
区别在于问号之后。应该?a=73jb是?asjb。单击该链接无效,也无法复制和粘贴它。出于明显的原因-h!
怪癖...?
奇怪的是,如果查看与来源相同的电子邮件(“查看”->“消息”->“原始来源”),则链接与应有的链接完全相同...
原始资料1
To:
Subject: Email Subject
X-PHP-Originating-Script: 2181:email.php
MIME-Version: 1.0
From: SURL <noreply@someurl.com>
To: email@email.com
Content-Type: multipart/alternative;boundary=np57849efa2a13b
X-Identified-User: {:box895.bluehost.com:...:...e.com} {sentby:program running on server}
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Hi, you handsome SOB!!
\n\n
We have ...
/n/n
And ...
\n\n
http://someurl.com/.../.../?a=73jbzUN90j27ME5N6W4jh24o992V91m3R632Hlu0&b=avr1owgJAAB3h4l1brw
--np57849efa2a13b
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Some Title</title>
<style type="text/css">
... Lots of styles...
</style>
</head>
<body>
<div class="message_container">
<div class="message_logo"></div>
<div class="message_leading_line">
Hi, you handsome SOB!!
</div> <!-- end message_leading_line -->
<div class="message_top_content">
We have ...
<br/>
And ...
<a href="http://someurl.com/.../.../?a=73jbzUN90j27ME5N6W4jh24o992V91m3R632Hlu0&b=avr1owgJAAB3h4l1brw" >visit this link</a>.
</div> <!-- end message_top_content -->
<div class="message_bottom_content">
If link doesn\'t work, copy and paste...
<pre>http://someurl.com/.../.../?a=73jbzUN90j27ME5N6W4jh24o992V91m3R632Hlu0&b=avr1owgJAAB3h4l1brw</pre>
</div> <!-- end message_bottom_content -->
</div> <!-- end message_container -->
</body>
</html>
--np57849efa2a13b--
Run Code Online (Sandbox Code Playgroud)
发生什么了?发送的数据看似正确,但是在非常特殊的情况下,所有cattywampus都将显示该数据。为什么要=73转换为s(或也许转换a=73为as...)?
我检查了ASCII表和HTML代码。在这两种情况下,唯一与之相关的73是大写字母“ i”。
编辑1
您是否知道发布此问题后马上就偶然发现《HTML URL编码参考》。
在该处,您会发现%73与关联s。
但是,我还是不太清楚如何=73...变得%73...然后s...。当然似乎与utf-8。
我当然不是第一个发送电子邮件的人,一定有办法...
编辑2-解决方案!
解决方案和解释太长,因此将其发布为答案。
问题出在热线:
$message .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
Run Code Online (Sandbox Code Playgroud)
根据Good ol'Wiki:
QP通过使用等号“ =”作为转义字符来工作。
如果要保留quoted-printable编码,可以采用以下方法:
... ASCII等号(十进制值61)必须由“ = 3D”表示。除可打印ASCII字符或行尾字符以外的所有字符都必须以这种方式编码。
所以线...
http://someurl.com/.../.../?a=3D73jbzUN90j27ME5N6W4jh24o992V91m3R632Hlu0&b=3Davr1owgJAAB3h4l1brw
Run Code Online (Sandbox Code Playgroud)
...效果很好(请注意=3D)。
但这还不够!
如果要使用quoted-printable,则需要将每个等号设置为=3D。这包括您的标题,样式,脚本,html等。
例如,查看来自TeamTreehouse的“忘记密码”电子邮件的前几行,该电子邮件也使用quoted-printable编码:
... Headers and plain-text version above ...
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.=
w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=3D"http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF-8">
<title>Treehouse</title>
<style type=3D"text/css">
... some styles, no equals signs ...
</style>
</head>
<body leftmargin=3D"0" marginwidth=3D"0" topmargin=3D"0" marginheight=3D"0"=
offset=3D"0" style=3D"-webkit-text-size-adjust: none; background: #edeff0;=
margin: 0; padding: 0; width: 100% !important" bgcolor=3D"#edeff0">
<center>
<table border=3D"0" cellpadding=3D"0" cellspacing=3D"0" height=3D"100%"=
width=3D"100%" style=3D"background: #edeff0; color: #9ba6b0; font-family: =
Helvetica,sans-serif; font-size: 14px; height: 100% !important; margin: 0; =
padding: 0; width: 100% !important" bgcolor=3D"#edeff0">
Run Code Online (Sandbox Code Playgroud)
... 等等。
此编码类型具有以下限制:
带引号的可打印编码数据行不得超过76个字符。为了在不更改编码文本的情况下满足此要求,可以根据需要添加换行符。换行符在编码行的末尾包含“ =”。
这意味着原始内容最终可能看起来像:
...
2wtdG91WGhKTEVZVVpoWnZsamo4IiwidiI6MSwicCI6IntcInVcIjozMDA4Nzg2NixcInZcIjox=
LFwidXJsXCI6XCJodHRwOlxcXC9cXFwvdGVhbXRyZWVob3VzZS5jb21cXFwvXCIsXCJpZFwiOlw=
iN2VlMmFmYWZiNGYwNGY5MGE2Y2NjMGExZGQwODdiMWVcIixcInVybF9pZHNcIjpbXCJlMzdmNG=
JlNDQ5NzYyY2NjZDQ5MmZjNmUyZDgwMjFhMTUxODgyM2RkXCJdfSJ9">teamtreehouse.com</=
...
Run Code Online (Sandbox Code Playgroud)
如果您具有适当的编码集,那么对于解释器来说就没什么大不了的,但是用肉眼却不那么漂亮和易于理解。
但是为什么要使用引用可打印的!
继续我们的阅读,结果发现某些SMTP(简单邮件传输协议)的行数限制为1000个字符。通过强制您的行数不超过76个字符(第76个字符=),您就不会因为该特定原因而导致电子邮件失败的风险。
你怎么做呢!?
事实证明,PHP具有本机功能,仅用于此目的...
quoted_printable_encode(string);
Run Code Online (Sandbox Code Playgroud)
^使用问题中的确切电子邮件代码对其进行了测试,并且效果与广告一样!
做同样的事情(我知道,这很明显):
<?php
$var = '... some huge, long string...';
$var2 = quoted_printable_encode($var);
?>
Run Code Online (Sandbox Code Playgroud)
然后进行漂亮的视觉比较...
<html>
<body>
<?php
echo '<pre>' . htmlspecialchars($var) . '</pre>';
echo '<br><hr><br>';
echo '<pre>' . htmlspecialchars($var2) . '</pre>';
?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
注意:对于您的电子邮件(如问题中的内容),您只想在Content-Transfer-Encoding: quoted-printable声明之后对部分进行编码...
$message .= "Content-Type: text/html; charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$tmp = '<html>... long, formatted, beautiful...</html>';
$message .= quoted_printable_encode($tmp);
$message .= "\r\n\r\n--" . $boundary . "--";
Run Code Online (Sandbox Code Playgroud)
再加上一些毫无意义的东西,因为StackOverflow不喜欢少于6个字符的编辑!愚蠢且毫无意义的限制。意味着无法纠正简单的拼写错误。
| 归档时间: |
|
| 查看次数: |
1706 次 |
| 最近记录: |