我有一个PHP mail();函数的奇怪问题.
像这样使用它
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html; charset=utf-8" . "\r\n";
$header .= "Content-Transfer-Encoding: quoted-printable" . "\r\n";
$header .= "Reply-To:" . $email . "\r\n";
$header .= "From: Kontaktformular <noreply@thomas-glaser-foto.de>" . "\r\n";
mail( "mail.me@mail.com", "Message from " . $name . " through your website!", $message, $header );
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作.邮件发送,一切都正确编码,主题也正常.
但当我用单引号更改双引号时,如下所示:
$header = 'MIME-Version: 1.0' . '\r\n';
$header .= 'Content-type: text/html; charset=utf-8' . '\r\n';
$header .= 'Content-Transfer-Encoding: quoted-printable' . '\r\n';
$header .= 'Reply-To:' . $email . '\r\n';
$header .= 'From: Kontaktformular <noreply@thomas-glaser-foto.de>' . '\r\n';
mail( 'mail.me@mail.com', 'Message from ' . $name . ' through your website!', $message, $header );
Run Code Online (Sandbox Code Playgroud)
邮件仍然可以发送,但没有设置主题.相反,它是
www-data@domain.com
并且特殊字符也被破坏了.那里发生了什么?
需要双引号才能输入特殊的换行符(\n "\r\n").当您使用单引号时,它们不会被视为换行符,而是将它们视为文字文本.
PHP解释器将使用双引号来评估材质,但它不会对单引号进行评估.因此,最佳做法是仅在需要评估某些内容时使用双引号(如变量,无法连接时,或者换行符等特殊字符).