首页 > 解决方案 > PHP Mailer 将字符添加到文本字段

问题描述

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

例如,当我进入 textarea 时:

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 } ?>

再次感谢!

标签: phpphpmailer

解决方案


你正在使用一个非常旧的、有缺陷的、易受攻击的 PHPMailer 版本,所以无论如何你都应该升级。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');

推荐阅读