当我收到电子邮件时,如何将回车转换为php中的换行符?

时间:2013-08-26 05:50:09

标签: php textarea

我有一个表格的网站。表单将提交到电子邮件地址,以便我可以从那里阅读。我一直在通过发送电子邮件来测试它,但是当我收到消息时我希望它就像这个例子:

  

姓名:有些人

     

编号:123456789

     

电子邮件:someguy@someweb.com

     

消息:

     

羊角面包甘草蛋白杏仁饼干。枣杯形蛋糕杯形蛋糕杯形蛋糕棉花糖丹麦语。松饼羊角面包苹果派。

     Chupa chups jelly-o oat蛋糕糖梅子羊角面包tootsie卷。顶部柠檬下落的棒棒糖。 Gummies果冻gummies布朗尼halvah芝麻扣糖果手杖申请。

     棉花糖焦糖糖梅果冻蛋白杏仁芝麻按扣丹麦粉芝麻按扣。 Bonbon甘草松饼甘草精棉花糖甜点燕麦蛋糕。棉花糖焦糖点心蛋糕饼果冻豆提拉米苏。蛋糕适用提拉米苏松饼蛋白杏仁饼干甜甜圈。

我这样收到他们:

  

羊角面包甘草蛋白杏仁饼干。枣杯形蛋糕杯形蛋糕杯形蛋糕棉花糖丹麦语。松饼羊角面包苹果派。 Chupa chups jelly-o oat蛋糕糖梅花羊角面包tootsie卷。顶部柠檬下落的棒棒糖。 Gummies果冻gummies布朗尼halvah芝麻按扣棒棒糖申请。棉花糖焦糖糖李子果冻蛋白杏仁饼干芝麻按扣丹麦粉芝麻按扣。 Bonbon甘草松饼甘草精棉花糖甜点燕麦蛋糕。棉花糖焦糖点心蛋糕饼果冻豆提拉米苏。蛋糕适用提拉米苏松饼蛋白杏仁饼干甜甜圈。

ALl换行符消失,消息是一条直线。

我还不熟悉PHP,所以如果你能以最简单的方式帮助我,我们将不胜感激。

这是我的php评论部分代码:

<?php
//If the form is submitted
if(isset($_POST['submit'])) {
$emailText = $_POST['message'];
str_replace("\r", "\n", $emailText);

//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
        $hasError = true;
    } else {
    $email = trim($_POST['email']);
}

//Check to make sure sure that a phone number is submitted
if(trim($_POST['phone']) == '')  {
    $hasError = true;
} else if (!preg_match("/^[1-9][0-9]{0,10}$/", trim($_POST['phone']))) {
        $hasError = true;
    } else {
    $phone = trim($_POST['phone']);
}

//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
    $hasError = true;
} else {
    $subject = trim($_POST['subject']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
}

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = 'myemail@gmail.com'; //Put your own email address here
    $body = "<strong>Name:</strong> $name <br><br><strong>Email:</strong> $email <br><br><strong>Contact:</strong> $phone <br><br><strong>Subject:</strong> $subject <br><br><strong>Message:</strong><br><br> $comments";
    $headers = 'From: Ucorp Online Form <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email . "\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;
}
 ?>

2 个答案:

答案 0 :(得分:3)

您正在寻找nl2br()。将值设置为$comments后将其应用:

if(trim($_POST['message']) == '') {
    $hasError = true;
} else {
    if(function_exists('stripslashes')) {
        $comments = stripslashes(trim($_POST['message']));
    } else {
        $comments = trim($_POST['message']);
    }
    $comments = nl2br($comments);
}

答案 1 :(得分:0)

尝试使用php的`str_replace()方法。

$emailText = $_POST['message'];
str_replace("\r", "\n", $emailText);

http://www.php.net/manual/en/function.preg-replace.php