简体   繁体   中英

PHP: mail function — how do I include an apostrophe in a parameter

I am having trouble with a valid email address with an apostrophe as the $to parameter. I don't know how to pass a parameter with an apostrophe to the PHP mail() function.

Wrap the email address in double quotes. But are you sure about the apostrophe being valid?

Edit, yup, it's legal. Just wrap it in double quotes, but also add backslashes in front of double quotes, spaces, and backslash.

I would really doubt an apostrophe is valid in an E-Mail address (even through the spec allows it) but to pass a parameter you could do one of the following:

$to = "this is a string with an ' in it";

or

$to = 'this is a string with an \' in it';

Now you can use it for mail();

mail($to, $subject, $message, $headers);

Either way you'll be sending the apostrophe to mail().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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