最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Allow '&' Character in mailto href links - Stack Overflow

programmeradmin3浏览0评论

I have an email mailto href link, and when I use a & character in the subject, this prevents any code rendering after this ampersand in the email subject line. i.e. Oil & Gas, just shows as Oil.

Under normal conditions I would just change the & to the word and, but the subject line is dynamically generated via post titles in Wordpress.

Does anyone know how I can prevent the subject line breaking, or in other words how I can get the & to show as a text character?

A stripped out version of the code is below:

<a href="mailto:[email protected]?subject=Oil&Gas">Apply</a>

Although in the HTML of the site this is pulled in using:

<a href="mailto:<?php echo $author_email;?>?subject=<?php the_title(); ?>">Apply</a>

Any help or ideas would be fabulous and I'm not sure if this will be html, php or Javascript solution?

I have an email mailto href link, and when I use a & character in the subject, this prevents any code rendering after this ampersand in the email subject line. i.e. Oil & Gas, just shows as Oil.

Under normal conditions I would just change the & to the word and, but the subject line is dynamically generated via post titles in Wordpress.

Does anyone know how I can prevent the subject line breaking, or in other words how I can get the & to show as a text character?

A stripped out version of the code is below:

<a href="mailto:[email protected]?subject=Oil&Gas">Apply</a>

Although in the HTML of the site this is pulled in using:

<a href="mailto:<?php echo $author_email;?>?subject=<?php the_title(); ?>">Apply</a>

Any help or ideas would be fabulous and I'm not sure if this will be html, php or Javascript solution?

Share Improve this question asked Mar 9, 2018 at 1:47 pjk_okpjk_ok 97710 gold badges53 silver badges110 bronze badges 5
  • URLEncode on the PHP side. – Randy Casburn Commented Mar 9, 2018 at 1:50
  • Related: Issue with mailto link in email address containing ampersand? – showdev Commented Mar 9, 2018 at 1:52
  • I retracted my duplicate vote as the other post has no reference to PHP. – showdev Commented Mar 9, 2018 at 1:52
  • no one uses mailto links any more, spam bots harvest addresses that way. create a form and mail that – user6763587 Commented Mar 9, 2018 at 1:57
  • 1 @nogad Valid concern, but not to imply that mailto links are obsolete. This could be for a non-public-facing site where harvesting is not an issue. – showdev Commented Mar 9, 2018 at 2:01
Add a ment  | 

5 Answers 5

Reset to default 3

You can escape the string so it's safe to use, using the urlencode function, like so:

<a href="mailto:<?php echo $author_email;?>?subject=<?php echo urlencode(the_title()); ?>">Apply</a>

urlencode PHP DOC

You need to urlencode() the title.

<?php
$title = "Gas&Oil";
?>
<a href="mailto:[email protected]?subject=<?= urlencode($title); ?>">Apply</a>

Demo

Also, since the_title() echos the title by default you need to use get_the_title(), otherwise urlencode() will have no effect. You can see this simulated here:

<?php
function the_title() {
    echo "Gas & Oil";
}
function get_the_title() {
    return "Gas & Oil";
}
?>
<a href="mailto:[email protected]?subject=<?=urlencode(the_title()); ?>">Apply</a><br> <!-- doesn't work -->
<a href="mailto:[email protected]?subject=<?=urlencode(get_the_title()); ?>">Apply</a> <!-- works -->

Demo

However, this will encode the whole title, changing other characters that you don't necessarily need encoded. So, to avoid this, only replace & for %26:

<a href="mailto:[email protected]?subject=<?=str_replace("&", "%26", the_title()); ?>">Apply</a><br> <!-- doesn't work -->
<a href="mailto:[email protected]?subject=<?=str_replace("&", "%26", get_the_title()); ?>">Apply</a> <!-- works -->

Try replacing the ampersand with either "%26" or "&" :

<a href="mailto:<?php echo $author_email;?>?subject=<?php str_replace('&', '%26', the_title()); ?>">Apply</a>
<a href="mailto:<?php echo $author_email;?>?subject=<?php echo str_replace('&amp;','%26',rawurlencode(htmlspecialchars_decode(the_title()))); ?>">Apply</a>

Use PHP bination: str_replace('&amp;','%26',rawurlencode(htmlspecialchars_decode(the_title())));

The issue is that the & character needs to be escaped in a URL as & is treated as a control character.

You can escape it by using HTML encoding. For example, &amp; is a & character, and &nbsp is a non-breaking space.

In JavaScript you can use "encodeURIComponent" for subject and body. Then it will show all special characters in email.

Example:

    const emailRequest = {
            to: "[email protected]",
            cc: "[email protected]",
            subject: "Email Request - for <CompanyName>",
            body: `Hi All, \r\n \r\n This is my pany <CompanyName> 
            \r\n Thanks
        }

    const subject = encodeURIComponent(emailRequest.subject.replace("<CompanyName>",'ABC & ** Company'));
    const body = encodeURIComponent(emailRequest.body .replace("<CompanyName>", 'ABC & ** Company'));

    window.location.href = (`mailto:${emailRequest.to}?cc=${emailRequest}&subject=${subject}&body=${body}`);
发布评论

评论列表(0)

  1. 暂无评论