PHP Mailer将字符添加到文本字段

时间:2018-06-22 02:15:28

标签: php phpmailer

PHP Mailer通过电子邮件发送输入的数据时,会添加一串奇数字符-
-以换行。

例如,当我进入文本区域时:

This is a test
2 pizzas
12 wings

它发送电子邮件:

This is a test
2 pizzas
12 wings

有什么想法吗?以下是相关的代码片段。谢谢!

require("phpmailer/class.phpmailer.php");

$mail = new PHPMailer;

if (!$mail->ValidateAddress($email)) {
    echo "Invalid Email Address";
    exit;
}

$email_body = "";
$email_body .= "Name " . $name . "\n";
$email_body .= "Email " . $email . "\n";
$email_body .= "Phone " . $email . "\n";
$email_body .= "Details " . $details . "\n";

$mail->setFrom($email, $name);
$mail->addAddress('amirm400@hotmail.com', 'Amir');     // Add a recipient

$mail->isHTML(false);                                  // Set email format to HTML

$mail->Subject = 'Offer request ' . $name;
$mail->Body    = $email_body;

还有这个

    <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
        echo "<p>Thanks for your request! We&rsquo;ll be in touch with an offershortly!</p>";
    } else { ?>

    <form method="post" action="offer.php">
        <input type="text" id="name" name="name" placeholder="Name">  <br />
        <input type="text" id="email" name="email" placeholder="Email"> <br />
        <input type="text" id="phone" name="phone" placeholder="Phone Number"> <br />
        <textarea name="details" id="details" cols="22" rows="7"  placeholder="Description of Products: Include Model # and Condition"></textarea> <br />
        <input style="display:none" type="text" id="address" name="address" />
        <input type="submit" value="Send" />
    </form>
    <?php } ?>

再次感谢!

1 个答案:

答案 0 :(得分:0)

您使用的是非常老的,有漏洞的,易受攻击的PHPMailer版本,因此无论如何都应该upgrade。 6.0具有更一致的换行处理,这就是您在此处看到的。除此之外,使用本机PHP换行符代替文字换行符可能会带来更多的运气:

$email_body = "";
$email_body .= "Name " . $name . PHP_EOL;
$email_body .= "Email " . $email . PHP_EOL;
$email_body .= "Phone " . $email . PHP_EOL;
$email_body .= "Details " . $details . PHP_EOL;

也不要这样做:

$mail->setFrom($email, $name);

这是伪造的,会导致您的邮件被阻止或垃圾邮件过滤。改为这样做:

$mail->addReplyTo($email, $name);
$mail->setFrom('amirm400@hotmail.com', 'Amir');