3

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

姓名:某男

电话:123456789

电子邮件:someguy@someweb.com

信息:

羊角面包甘草杏仁饼。大枣纸杯蛋糕纸杯蛋糕纸杯蛋糕丹麦棉花糖。松饼羊角面包苹果派。

Chupa chups jelly-o 燕麦蛋糕糖李子羊角面包 tootsie 卷。糖果手杖浇头柠檬滴。Gummies jelly gummies brownie halvah sesame snap candy canes applicake。

棉花糖焦糖糖梅果冻马卡龙芝麻脆丹麦粉芝麻脆。Bonbon甘草松饼甘草棉花糖甜点燕麦蛋糕。棉花糖焦糖甜点蛋糕派果冻豆提拉米苏。蛋糕应用提拉米苏松饼马卡龙馅饼甜甜圈。

我像这样收到它们:

羊角面包甘草杏仁饼。大枣纸杯蛋糕纸杯蛋糕纸杯蛋糕丹麦棉花糖。松饼羊角面包苹果派。Chupa chups jelly-o 燕麦蛋糕糖李子羊角面包 tootsie 卷。糖果手杖浇头柠檬滴。Gummies jelly gummies brownie halvah sesame snap candy canes applicake。棉花糖焦糖糖梅果冻马卡龙芝麻脆丹麦粉芝麻脆。Bonbon甘草松饼甘草棉花糖甜点燕麦蛋糕。棉花糖焦糖甜点蛋糕派果冻豆提拉米苏。蛋糕应用提拉米苏松饼马卡龙馅饼甜甜圈。

所有的换行符都消失了,消息是一条直线。

我对 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;
}
 ?>
4

2 回答 2

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);
}
于 2013-08-26T16:28:54.680 回答
0

试试 php 的 `str_replace() 方法。

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

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

于 2013-08-26T06:19:15.803 回答