PHP mail()减少了一半的消息

时间:2015-01-10 20:03:54

标签: php html email

因此,当我尝试通过此代码发送电子邮件时 这是php文件 mail.php

$cookie_name_person = "personName";
$cookie_name_email = "email";
$cookie_name_message = "emailMessage";
$cookie_name_subject = "subject";

$cookie_value_email = $_COOKIE[$cookie_name_email];
$cookie_value_message = $_COOKIE[$cookie_name_message];
$cookie_value_person = $_COOKIE[$cookie_name_person];
$cookie_value_subject = $_COOKIE[$cookie_name_subject];

$to = 'mail@gmail.com';
$subject = "${cookie_value_subject}";
$body = "От ${cookie_value_person}, \n ${cookie_value_message}";

$headers = "From: ${cookie_value_email}" . "\n";

$body = wordwrap($body, 70, "\n");

mail($to, $subject, $body, $headers);

这是html文件mail.html

`

<head>
    <title>NPMG Java Home Mail Form</title>
    <script src="src/jquery.min.js"></script>
    <script src="src/script.js"></script>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="mail.css">
</head>

<body>
    <font color="white">
    <form id="emailsender" method="post" name="emailForm">
        <label>Име:</label>
        </br>
        <input type="text" id="personname"/>
        </br>
        <label>Email:</label>
        </br>
        <input type="text" id="email"/>
        </br>
        <label>Тема:</label>
        </br>
        <input type="text" id="subject"/>
        </br>
        <label>Съобщение:</label>
        </br>
        <textarea id="message" rows="10" cols="50"></textarea>
        </br>
        <input type="button" value="Изпрати" id="submit" onclick="sendMail()"/>
    </form>
    <div id="status"></div>
    </font>
</body>

`

这是JS部分的sendMail()函数

function sendMail()
{
var email = document.getElementById("email").value;
var message = document.getElementById("message").value;
    var personname = document.getElementById("personname").value;
    var subject = document.getElementById("subject").value;

if(name == "" || email == "" || message == "")
    {
        document.getElementById("status").innerHTML = "Please fill all the gaps";
        return 1;
    }

var cookieNameEmail = "email";
var cookieNameMessage = "emailMessage";
    var cookieNamePerson = "personName";
    var cookieNameSubject = "subject";

var date = new Date();
date.setTime(date.getTime() + (60 * 1000));
var expires = "; expires=" + date.toGMTString();

document.cookie = cookieNameEmail + "=" + email + expires + "; path=/";
document.cookie = cookieNameMessage + "=" + message + expires + "; path=/";
    document.cookie = cookieNamePerson + "=" + personname + expires + "; path=/";
    document.cookie = cookieNameSubject + "=" + subject + expires + "; path=/";

$(document).ready(function() {
            $.get('src/mail.php', function(){
window.close();
});

        });

}

在$ cookie_value_message的第一个换行符后,正在剪切文本。即使用\ r \ n更改\ n后问题仍然存在

1 个答案:

答案 0 :(得分:0)

1)您无法将换行符存储到Cookie中。在放入cookie之前,您必须encodeURI()您的数据,然后在PHP脚本中urldecode(),如下所示:

的script.js:

document.cookie = cookieNameMessage + "=" + encodeURI(message) + expires + "; path=/";

mail.php

$cookie_value_message = urldecode($_COOKIE[$cookie_name_message]);

2)您的JS中存在错误,这会阻止数据始终发布:

if(name == "" || email == "" || message == "") // 'name' instead of 'personname'