PHP表单中$txt变量的字符间距设置及换行实现问题咨询
调整PHP邮件内容格式:添加空格与换行
嘿,我帮你搞定这个邮件内容的格式问题!你的核心需求是给$name、$news变量前加空格,同时让两个语句换行显示,另外我还发现你mail()函数的参数顺序写错了,这个会导致邮件正文和头部混乱,一起给你修正了:
修正后的完整代码:
<?php ini_set( display_errors, 1 ); error_reporting( E_ALL ); $email = $_POST['myEmail']; $name = $_POST['myName']; $news = $_POST['news']; if($news != 'Yes') $news = 'No'; $to = "web@myemail.com"; $subject = "Add me to the Launch Day Notification List!"; $headers = "From:".$email; // 修正内容拼接:添加空格、换行,调整语法 $txt = "This person wishes to be added to the launch day notification list: ".$name."\n" ."Does this person wish to subscribe to the newsletter? ".$news; // 修正mail函数参数顺序:正文是第三个参数,头部是第四个 mail($to, $subject, $txt, $headers); echo "Thank you for your inquiry, you will be added to our launch day notification list!"; ?>
关键改动说明:
- 变量与字符串的空格衔接:在字符串末尾和变量之间添加了空格(比如
list: ".$name),确保$name的值和前面的句子自然隔开 - 换行处理:用PHP的换行符
\n替代了<br>,因为默认发送的是纯文本邮件,<br>会被当作普通字符串显示,而\n会在邮件里生成换行 - 修复
mail()参数顺序:原代码把正文$txt放在了头部参数的位置,正确顺序是mail(收件人, 主题, 正文, 额外头部),这个错误会导致邮件内容显示异常 - 规范字符串拼接:用
.来正确连接多个字符串和变量,保证语法正确
这样处理后,邮件内容就会变成你想要的格式:
This person wishes to be added to the launch day notification list: name here Does this person wish to subscribe to the newsletter? yes or no here
内容的提问来源于stack exchange,提问作者ainteractive
相关产品推荐
相关产品推荐

