简体   繁体   English

在php中转义mailto链接

[英]escape mailto link in php

I have a mailto link in my php code ie我的 php 代码中有一个 mailto 链接,即

<a href="mailto:email@domain.tld?subject=<?php rawurlencode('Travels & Tours'); ?>&body=this is body">link text</a>

The issue is when it appears in email client eg outlook the subject line text is changed to Travels &amp; Tours问题是当它出现在电子邮件客户端时,例如 Outlook,主题行文本更改为Travels &amp; Tours Travels &amp; Tours

Strictly speaking, you should be using urlencode() to URL encode the URL parameter value, instead of rawurlencode() , which is used to encode the URL-path part of the URL.严格来说,您应该使用urlencode()对 URL 参数值进行 URL 编码,而不是使用rawurlencode()来编码 URL 的 URL-path 部分。 Whilst these two functions produce similar results, there are differences.虽然这两个函数产生相似的结果,但还是有区别的。 Different parts of the URL have different restrictions. URL 的不同部分有不同的限制。 Notably (from your example), spaces are encoded as + in the URL parameter value, whereas they are encoded as %20 in the URL-path (although this doesn't really matter in most scenarios).值得注意的是(从您的示例中),空格在 URL 参数值中被编码为+ ,而它们在 URL 路径中被编码为%20 (尽管这在大多数情况下并不重要)。

As well as URL encoding the URL in order to make a valid request, you may also need to HTML entity encode the entire HTML attribute in order to make it valid HTML (however, HTML5 is less strict).除了对 URL 进行 URL 编码以发出有效请求外,您可能还需要对整个 HTML 属性进行 HTML 实体编码以使其有效(不过,HTML5 不太严格)。 (Browsers are, however, quite forgiving and are likely to automatically HTML entity encode the URL as needed. However, this might not always function correctly and for security reasons, you should ensure that whatever HTML you generate is already correctly encoded.) (然而,浏览器非常宽容,并且可能会根据需要自动对 URL 进行 HTML 实体编码。但是,这可能并不总是正确运行,出于安​​全原因,您应该确保生成的任何 HTML 都已经正确编码。)

So, ideally, you should be doing something like the following:因此,理想情况下,您应该执行以下操作:

<?php
$subject = urlencode('Travels & Tours');
$body = urlencode('body is body');
$url = htmlspecialchars("mailto:email@domain.tld?subject=$subject&body=$body");
?>
<a href="<?=$url?>">link text</a>

This results in the following HTML:这将产生以下 HTML:

<a href="mailto:email@domain.tld?subject=Travels+%26+Tours&amp;body=body+is+body">link text</a>

However, this may still not resolve your problem and could depend on the browser / email client - since the URL parameters must be correctly HTML entity and URL decoded before being processed by the email client.但是,这可能仍然无法解决您的问题,并且可能取决于浏览器/电子邮件客户端 - 因为 URL 参数在被电子邮件客户端处理之前必须是正确的 HTML 实体和 URL 解码。 The above works OK for me using Gmail, but so did your original example (once the PHP syntax was corrected).以上对我使用 Gmail 可以正常工作,但您的原始示例也是如此(一旦 PHP 语法得到纠正)。

Email clients are relatively basic in what they can process and so it is generally the email client that you need to wrestle with.电子邮件客户端在它们可以处理的内容方面相对基本,因此通常是您需要处理的电子邮件客户端。 Personally, I would avoid using & in the email subject to begin with.就个人而言,我会避免在电子邮件主题中使用&开始。

use html_entity_decode使用html_entity_decode

ie

<a href="mailto:email@domain.tld?subject=<?php html_entity_decode(Travels & Tours) ?>&body=this is body">link text</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM