首页 > 解决方案 > 如何在 PHP 中使用带有 GET 变量的 URL 构建 mailto HTML 元素

问题描述

我正在我的 PHP 程序中构建一个“mailto”HTML 元素,构建的代码如下:

    <a href="mailto:some.one@some.where.com
            ?Subject=CA-7%20graphing%20tool%20error report
            &body=Problem with graph http://mcz:51019/noSecurePhp/graphBuildTest.php?startJobname=PE2300DY&amp;endJobnames=&amp;jobMask=&amp;collapseJobnames=&amp;searchDepth=1&amp;schedId=001&amp;custSchedule=&amp;cpColour=%23ffff00&amp;cpStartEndColour=%23ff0000&amp;grpBySuite=on&amp;maxNodes=500&amp;ranksep=0.5&amp;nodesep=0.25&amp;layout=dot&amp;splines=spline&amp;rankDir=TB&amp;graphStyle=Full&amp;bgColour=%23a8a8a8&amp;nodeColour=%23ffc68c&amp;fontColour=%23000000&amp;nodeStyle=filled&amp;penWidth=3%20Issue description :" target="_blank" class="btn btn-danger">Send Bug report</a>

该按钮看起来不错,当我将鼠标悬停在它上面时,状态栏中显示的 URL 看起来还不错,尽管“&”只是显示为“&”,即 URL 看起来是正确的。

当我单击该按钮时,我会收到一封包含正确地址和主题的新电子邮件,但正文只是:

Problem with graph http://mcz:51019/noSecurePhp/graphBuildTest.php?startJobname=PE2300DY+

即在第一个'&'处切断。

这是构建 mailto 元素的代码(NL 只是“
\n”):

function createBugbutton() {
    $invokingUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $invokingUrl = str_replace(["&", "#", " "], ["&amp;", "%23", "%20"], $invokingUrl);

    $emailBody = "Problem with graph ".$invokingUrl.NL.NL."Issue description :";

    $emailLink = '<a href="mailto:some.one@some.where.com
            ?Subject=CA-7%20graphing%20tool%20error report
            &body='.$emailBody.'"
            target="_blank" class="btn btn-danger"
            >Send Bug report</a>';

    echo $emailLink;
}

我已将生成的 HTML 复制到一个空白的 HTML 文件中,并且行为完全相同,所以我认为这不是 PHP 的事情。

我究竟做错了什么?

编辑:以下评论和答案,我生成的 HTML 现在是:

<a href="mailto:some.one@some.where.com
                ?Subject=error report
                &body=Problem with graph http://mcz/graphBuildTest.php?startJobname=PE2300DY&26;endJobnames=fred<br>
Issue description :" target="_blank" class="btn btn-danger"
                >Send Bug report</a>

但即使这个 cpied 到一个 .html 文件文件来构建电子邮件,正文在“PE2300DY”之后再次停止

新编辑:

按照公认的答案工作 - 谢谢大家。

标签: phphtml

解决方案


替换&amp;%26

我对此进行了测试,并且可以正常工作:

<a href="mailto:some.one@some.where.com
        ?Subject=CA-7%20graphing%20tool%20error report
        &body=Problem with graph http://mcz:51019/noSecurePhp/graphBuildTest.php?startJobname=PE2300DY%26endJobnames=%26jobMask=%26collapseJobnames=%26searchDepth=1%26schedId=001%26custSchedule=%26cpColour=%23ffff00%26cpStartEndColour=%23ff0000%26grpBySuite=on%26maxNodes=500%26ranksep=0.5%26nodesep=0.25%26layout=dot%26splines=spline%26rankDir=TB%26graphStyle=Full%26bgColour=%23a8a8a8%26nodeColour=%23ffc68c%26fontColour=%23000000%26nodeStyle=filled%26penWidth=3%20Issue description :" target="_blank" class="btn btn-danger">Send Bug report</a>

你的功能应该是:

function createBugbutton() {
    $invokingUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $invokingUrl = str_replace(["&", "#", " "], ["%26", "%23", "%20"], $invokingUrl);

    $emailBody = "Problem with graph ".$invokingUrl.NL.NL."Issue description :";

    $emailLink = '<a href="mailto:some.one@some.where.com
            ?Subject=CA-7%20graphing%20tool%20error report
            &body='.$emailBody.'"
            target="_blank" class="btn btn-danger"
            >Send Bug report</a>';

    echo $emailLink;
}

推荐阅读