将电子邮件发送到 Gmail 时 HTML 链接出错
您好,我开发了一个新闻通讯脚本来发送 HTML 电子邮件。
接收到桌面电子邮件客户端时一切都很好,但我注意到当我发送到 gmail 帐户时,html 链接不起作用,而嵌入图像、CSS、表格布局却工作得很好。 Gmail 更改
<a href="myurl" title="My url">link</a>
为
<a title="My url">link</a>
我发现了一些主题,但没有解决方案。
这就是我构建电子邮件的方式,在 $body var 中是标准的 html 链接:
function send_mail ($from,$to,$subject,$body,$site_skin,$site_name,$site_url) { //通用头 $header = "来自: $site_name \n"; $header .="密件抄送: $to \n"; $header .= "X-Mailer: PHP5.3\n";
//separator string
$boundary = "==String_Boundary_x" .md5(time()). "x";
// specific message header
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/related;\n";
$header .= " boundary=\"$boundary\";\n\n";
//this part is viewed only if the program cannot manage the MIME
$message = "your software dosen't support MIME\n\n";
$message .= "--$boundary\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$title_newsletter ="$subject";
$mail_body = $body;
$subject = "$titoloNSL";
$mail_site_url="$site_url";
include ("../../../../themes/skins/$site_skin/email/email.php");
$message .= "$htmlBody";
$message .= "--$boundary\n";
$message .= "Content-ID: <header>\n";
$message .= "Content-Type: image/jpeg\n";
$message .= "Content-Transfer-Encoding: base64\n\n";
$embedded_file = "../../../../themes/skins/$site_skin/images/mail_header.jpg";
$file = fopen($embedded_file,'rb');
$data = fread($file,filesize($embedded_file));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "$data\n\n";
$message .= "--$boundary\n";
$message .= "Content-ID: <go>\n";
$message .= "Content-Type: image/gif\n";
$message .= "Content-Transfer-Encoding: base64\n\n";
$embedded_file = "../../../../themes/skins/$site_skin/images/go.gif";
$file = fopen($embedded_file,'rb');
$data = fread($file,filesize($embedded_file));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "$data\n\n";
$message .= "--$boundary--\n";
@mail($to, $subject, $message, $header);
}
Hello i have developed a newsletter script to send HTML emails.
Everything works great receiving to desktop email clients, but i notice when i send to a gmail account, the html links are not working while embedded images, css, table layout work great.
Gmail change
<a href="myurl" title="My url">link</a>
to
<a title="My url">link</a>
i found a few topics around, without solutions.
this is how i build the email, in the $body var are the standard html links:
function send_mail ($from,$to,$subject,$body,$site_skin,$site_name,$site_url)
{
//general header
$header = "From: $site_name \n";
$header .="BCC: $to \n";
$header .= "X-Mailer: PHP5.3\n";
//separator string
$boundary = "==String_Boundary_x" .md5(time()). "x";
// specific message header
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/related;\n";
$header .= " boundary=\"$boundary\";\n\n";
//this part is viewed only if the program cannot manage the MIME
$message = "your software dosen't support MIME\n\n";
$message .= "--$boundary\n";
$message .= "Content-Type: text/html; charset=\"utf-8\"\n";
$message .= "Content-Transfer-Encoding: 7bit\n\n";
$title_newsletter ="$subject";
$mail_body = $body;
$subject = "$titoloNSL";
$mail_site_url="$site_url";
include ("../../../../themes/skins/$site_skin/email/email.php");
$message .= "$htmlBody";
$message .= "--$boundary\n";
$message .= "Content-ID: <header>\n";
$message .= "Content-Type: image/jpeg\n";
$message .= "Content-Transfer-Encoding: base64\n\n";
$embedded_file = "../../../../themes/skins/$site_skin/images/mail_header.jpg";
$file = fopen($embedded_file,'rb');
$data = fread($file,filesize($embedded_file));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "$data\n\n";
$message .= "--$boundary\n";
$message .= "Content-ID: <go>\n";
$message .= "Content-Type: image/gif\n";
$message .= "Content-Transfer-Encoding: base64\n\n";
$embedded_file = "../../../../themes/skins/$site_skin/images/go.gif";
$file = fopen($embedded_file,'rb');
$data = fread($file,filesize($embedded_file));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "$data\n\n";
$message .= "--$boundary--\n";
@mail($to, $subject, $message, $header);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是你的代码没有任何问题;看起来 Gmail 正在从 HTML 末尾剥离链接。
可能您的网址与垃圾邮件过滤器发生冲突,或者类似的情况?
如果是这种情况,我不知道您可以在 PHP 代码中做些什么来改进(除了以纯文本而不是 html 发送电子邮件)。
My guess is that there's nothing wrong with your code; it looks like it's Gmail that's stripping the links out of your HTML at their end.
Possibly your URLs are falling foul of their spam filters, or something like that?
If that's case, I don't know that there's anything you can do in your PHP code to improve things (short of sending the email as plain text instead of html).