新行转义需要如何格式化才能使消息正确显示
How does new line escaping need to formated to get message to display correctly
我将以下内容保存到我的 $msg
变量中。在展示时,我的目标是让它以新行的正确格式打印。我不确定我能做些什么不同的事情。在旁注中,我可能会错误地创建消息。
$msg ="Hey ".$row['emailName']."\rYou recently signed up to receive email updates from us, and we wanted to verify your email address.\nIf you signed up to receive updates please press the confirmation link below:\nwww.domain.com/validateemail?email=".$row['emailAddress']."&emailID=".$row['emailID']."&emailed=1\nThe request came from".$row['signupIP']."\nIf you did not make this request, you can ignore this email\n \r Thanks,\rKathleen Williams\rHead Trainer";
问题是它没有显示换行符。
不同的OS使用不同的行尾符号(EOL):
\r\n
- CRLF (Windows)
\n
- LF(Unix 和 OS X)
\r
- CR (Mac)
如果您在 HTML 中打印出此消息,则必须将 EOL 符号更改为 <br>
标记,因为 EOL 符号将被忽略。
您可以使用 nl2br 进行转换。
您的代码中的问题:
\r
不是换行; \n
是换行符。
www.domain.com/validateemail?...
不是 URL。 URL 以协议开头(http://
、https://
等)。没有它,电子邮件客户端不会将其检测为 URL,也不会从中创建 link。
为了便于阅读和修改,代码有多种编写方式。例如,您可以使用 heredoc syntax for strings. It allows you to write the text on multiple lines without worrying how to write newline characters. Also, PHP parses it for variables and escape sequences the same way it does with double quoted strings.
// The text starting after the line `<<< END` and ending before
// the marker provided after `<<<` is a string.
// It is stored into the $msg variable.
$textBody = <<< END_TEXT
Hey {$row['emailName']}
You recently signed up to receive email updates from us, and we wanted to verify your email address.
If you signed up to receive updates please press the confirmation link below:
http://www.example.com/validateemail?email={$row['emailAddress']}&emailID={$row['emailID']}&emailed=1
The request came from {$row['signupIP']}.
If you did not make this request, you can ignore this email.
Thanks,
Kathleen Williams
Head Trainer
END_TEXT;
如果您想发送 HTML 电子邮件,您可以使用相同的技术生成电子邮件正文,但不要忘记 HTML 不关心源代码中的换行符。将文本包裹在 <p>
HTML 元素中以生成段落,并使用 <br>
HTML 元素强制在段落内换行。
代码可能是这样的:
$htmlBody = <<< END_HTML
<p>Hey {$row['emailName']}</p>
<p>You recently signed up to receive email updates from us,
and we wanted to verify your email address.<br>
If you signed up to receive updates please press this
<a href="http://www.example.com/validateemail?email={$row['emailAddress']}&emailID={$row['emailID']}&emailed=1">confirmation link</a>.</p>
<p>The request came from {$row['signupIP']}.<br>
If you did not make this request, you can ignore this email.</p>
<p>Thanks,<br>
Kathleen Williams<br>
Head Trainer</p>
END_HTML;
我将以下内容保存到我的 $msg
变量中。在展示时,我的目标是让它以新行的正确格式打印。我不确定我能做些什么不同的事情。在旁注中,我可能会错误地创建消息。
$msg ="Hey ".$row['emailName']."\rYou recently signed up to receive email updates from us, and we wanted to verify your email address.\nIf you signed up to receive updates please press the confirmation link below:\nwww.domain.com/validateemail?email=".$row['emailAddress']."&emailID=".$row['emailID']."&emailed=1\nThe request came from".$row['signupIP']."\nIf you did not make this request, you can ignore this email\n \r Thanks,\rKathleen Williams\rHead Trainer";
问题是它没有显示换行符。
不同的OS使用不同的行尾符号(EOL):
\r\n
- CRLF (Windows)\n
- LF(Unix 和 OS X)\r
- CR (Mac)
如果您在 HTML 中打印出此消息,则必须将 EOL 符号更改为 <br>
标记,因为 EOL 符号将被忽略。
您可以使用 nl2br 进行转换。
您的代码中的问题:
\r
不是换行;\n
是换行符。www.domain.com/validateemail?...
不是 URL。 URL 以协议开头(http://
、https://
等)。没有它,电子邮件客户端不会将其检测为 URL,也不会从中创建 link。
为了便于阅读和修改,代码有多种编写方式。例如,您可以使用 heredoc syntax for strings. It allows you to write the text on multiple lines without worrying how to write newline characters. Also, PHP parses it for variables and escape sequences the same way it does with double quoted strings.
// The text starting after the line `<<< END` and ending before
// the marker provided after `<<<` is a string.
// It is stored into the $msg variable.
$textBody = <<< END_TEXT
Hey {$row['emailName']}
You recently signed up to receive email updates from us, and we wanted to verify your email address.
If you signed up to receive updates please press the confirmation link below:
http://www.example.com/validateemail?email={$row['emailAddress']}&emailID={$row['emailID']}&emailed=1
The request came from {$row['signupIP']}.
If you did not make this request, you can ignore this email.
Thanks,
Kathleen Williams
Head Trainer
END_TEXT;
如果您想发送 HTML 电子邮件,您可以使用相同的技术生成电子邮件正文,但不要忘记 HTML 不关心源代码中的换行符。将文本包裹在 <p>
HTML 元素中以生成段落,并使用 <br>
HTML 元素强制在段落内换行。
代码可能是这样的:
$htmlBody = <<< END_HTML
<p>Hey {$row['emailName']}</p>
<p>You recently signed up to receive email updates from us,
and we wanted to verify your email address.<br>
If you signed up to receive updates please press this
<a href="http://www.example.com/validateemail?email={$row['emailAddress']}&emailID={$row['emailID']}&emailed=1">confirmation link</a>.</p>
<p>The request came from {$row['signupIP']}.<br>
If you did not make this request, you can ignore this email.</p>
<p>Thanks,<br>
Kathleen Williams<br>
Head Trainer</p>
END_HTML;