如何在电子邮件表单中添加PHP中的换行符?

时间:2018-02-20 10:47:36

标签: php email

我知道此问题已被多次询问,但我无法在下面的代码中将
或\ n标记添加到正确的位置: 我想在电子邮件的不同行中列出发件人姓名,电话号码等。你能指出我需要添加代码的“确切”位置吗?

// Read the form values
$success = false;
$senderName = isset( $_POST['username'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['username'] ) : "";

$recommenderEmail = isset($_POST['recommender-email']) ? preg_replace("/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['recommender-email']) : "";

$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";

$phone = isset( $_POST['phone'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['phone'] ) : "";

$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

$date = isset( $_POST['date'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['date'] ) : "";

$timeToCall = isset( $_POST['time-to-call'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['time-to-call'] ) : "";

$referralAddress = isset( $_POST['referral-address'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['referral-address'] ) : "";

// If all "required" values exist, send the email
if ( $senderName && $senderEmail && $phone && $recommenderEmail && $referralAddress) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: <" . $recommenderEmail . ">";

  $msgBody = " Referral Name: " . $senderName .  " Phone: " . $phone .  " E-Mail: " . $senderEmail . " Referral Address: " . $referralAddress .  " Move Date: " . $date .  " Best Time to Contact: " .  $timeToCall . " Message: " . $message . "";
  $success = mail( $recipient, $headers, $msgBody );

谢谢。

2 个答案:

答案 0 :(得分:1)

在字符串中尝试\ n进行换行。

如果不能正常工作,请为电子邮件创建html模板,并在电子邮件标题中添加内容类型'Content-Type:text / html;字符集= UTF-8' 。

<?php
   //Read the form values 
    $success = false;
    $senderName = isset($_POST['username']) ? preg_replace("/[^.-\' a-zA-Z0-9]/", "", $_POST['username']) : "";
    $recommenderEmail = isset($_POST['recommender-email']) ? preg_replace("/[^.-_\@a-zA-Z0-9]/", "", $_POST['recommender-email']) : "";
    $senderEmail = isset($_POST['email']) ? preg_replace("/[^.-_\@a-zA-Z0-9]/", "", $_POST['email']) : "";
    $phone = isset($_POST['phone']) ? preg_replace("/[^.-\' a-zA-Z0-9]/", "", $_POST['phone']) : "";
    $message = isset($_POST['message']) ? preg_replace("/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message']) : "";
    $date = isset($_POST['date']) ? preg_replace("/[^.-\' a-zA-Z0-9]/", "", $_POST['date']) : "";
    $timeToCall = isset($_POST['time-to-call']) ? preg_replace("/[^.-\' a-zA-Z0-9]/", "", $_POST['time-to-call']) : "";
    $referralAddress = isset($_POST['referral-address']) ? preg_replace("/[^.-\' a-zA-Z0-9]/", "", $_POST['referral-address']) : "";

    // If all "required" values exist, send the email if ( $senderName && $senderEmail && $phone && $recommenderEmail && $referralAddress) { $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">"; $headers = "From: <" . $recommenderEmail . ">";

    $msgBody = " Referral Name: " . $senderName . "\n Phone: " . $phone . "\n E-Mail: " . $senderEmail . "\n Referral Address: " . $referralAddress . "\n Move Date: " . $date . "\n Best Time to Contact: " . $timeToCall . "\n Message: " . $message . "";
    $success = mail($recipient, $headers, $msgBody);

答案 1 :(得分:-1)

您可以使用nl2br功能 这是文档http://php.net/manual/en/function.nl2br.php