简体   繁体   English

向PHP电子邮件添加不间断空格不会插入空格

[英]Adding a non breaking space to a php email does not insert spaces

I have this PHP code: 我有这个PHP代码:

mail($email, $subject, 
  "Welcome to **!\n\n The password for your account is:\n $password", 
  $headers);

Adding &nbsp to the php email content does not insert spaces. 将&nbsp添加到php电子邮件内容不会插入空格。

I want to put two spaces between the colon and $password . 我想在冒号和$password之间放置两个空格。 Is there a way to add spaces? 有没有添加空格的方法?

You can send the email as HTML which requires extra headers and actual HTML in your message body: 您可以将电子邮件作为HTML发送,这在邮件正文中需要额外的标题和实际的HTML:

// Add extra headers to $headers
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

// Send mail as HTML
mail($email, $subject, 
  "<html><body>Welcome to **!<br><br> The password for your account is:<br> &nbsp;$password</body></html>", 
  $headers);

Add <html> and </html> : to indicate the message is HTML formatted. 添加<html></html> :以指示消息是HTML格式的。 Then you can use &nbsp; 然后,您可以使用&nbsp;

Use the nl2br function to convert newlines to <br/> s. 使用nl2br函数将换行符转换为<br/>

Also set the Content-Type by appending a header " Content-Type: text/html; charset=UTF-8 ". 还可以通过添加标题“ Content-Type: text/html; charset=UTF-8 ”来设置Content-Type。

If you don't want to use HTML: you can use the tab-character \\t too, but not sure if Hotmail supports that eighter. 如果您不想使用HTML:您也可以使用制表符\\t ,但不确定Hotmail是否支持该文本。

This really depends on what you are reading it in. The problem with Hotmail is that it sees it as plan text, but converts it to HTML most likely to display to you. 这实际上取决于您正在阅读的内容。Hotmail的问题在于它将其视为计划文本,但是将其转换为最有可能显示给您的HTML。

Look at example 4 on how to send html email using php . 查看有关如何使用php发送html电子邮件的示例4

If the e-mail's Content-Type is text/plain , then all spaces should be honored. 如果电子邮件的Content-Typetext/plain ,则应保留所有空格。 Sounds like you're transmitting the e-mail message as text/html , which is why you would need the &nbsp to get the second space to show up. 听起来您正在以text/html发送电子邮件,这就是为什么您需要&nbsp来显示第二个空格的原因。

Use the http://swiftmailer.org/ Class to do emailing with PHP. 使用http://swiftmailer.org/类通过PHP发送电子邮件。 It's nice done and gives you a lot of nice features. 做得很好,并为您提供了许多不错的功能。

在发送消息时尝试使用“自动换行”,这应该可以拆分电子邮件并解决问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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