我需要向我正在創建的網站添加一個聯繫表單,並且發現有人爲我喜歡的沒有PHP經驗的人分享了一段有用的代碼。PHP聯繫表格中的換行
將代碼粘貼並編輯到頁面中並上傳到暫存站點後,我使用Gmail地址對其進行了測試,結果正常。唯一的是電子郵件文本沒有換行符。
下面的代碼:
<?php
if ($_POST["email"]<>'') {
$ToEmail = '[email protected]';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<form action="contactus.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"> </textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
<?php
};
?>
這是我收到的時候到達我的收件箱:
姓名:MrĴBloggsEmail:[email protected]:評論文本。
我想什麼是它,而不是像這樣:
姓名:MrĴ布羅格斯
電子郵件:[email protected]
評論:評論的文字。
在此先感謝您的幫助。
謝謝,但是這只是增加了一個空間,而不是休息,同去添加\ r \ n到代碼的末尾。事後看來,「MESSAGE_BODY」部件應該將我引向我需要尋找的地方。 – number8pie 2012-03-30 14:07:21
@ number8pie如果郵件是html,您應該添加'
',您的郵件像我的純文本looke – 2012-03-30 14:13:25
感謝您清除該郵件,如果我收到的電子郵件中有
標記,我將使用\ n方法。 – number8pie 2012-03-30 14:24:58