【问题标题】:PHPmailer message when send发送时的PHPmailer消息
【发布时间】:2017-04-09 18:38:30
【问题描述】:

我做了一个 PHPMailer,它工作得非常好,但是如果我点击发送,它给了我我见过的最大的错误代码。我知道错误与代码中的header('Location: bedankt.php'); 有关。

我想要完成的是,用户收到一条消息,表明表单已在同一页面上发送(没有警报框),只是弹出的纯文本说明表单已提交,所以没有重定向到“Bedankt.php”。这是我谈到的错误代码的截图:你们能帮帮我吗?这是我的代码:

Index.PHP:

<?php

session_start();

require_once 'helpers/security.php';

$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];
?>



<!DOCTYPE html>
<html lang="en">
<head>
<title>Servicepunt Detailhandel Groningen | Home</title>
<link rel="shortcut icon" href="../../images/favicon/favicon.png" type="image/png">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css?<?php echo date('l jS \of F Y h:i:s A'); ?>"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"> </script>
<script src="js/script.js"></script>
</head>
<body>
<body>
<div class="container-fluid">

<div class="header">
contact
</div>

<div id="footer">
footer
<div  class="col-md-3 col-md-offset-9 col-xs-4 col-xs-offset-8" id="contact">

<?php if(!empty($errors)): ?>
    <div class="panel">
        <ul>
            <li>
                <?php echo implode('</li><li>', $errors); ?>
            </li>
        </ul>
    </div>
<?php  endif; ?>
<form action="libs/contact.php" method="post">
    <label>
        Uw naam*
        <input type="text" name="name" id="name" autocomplete="off" <?php echo isset($fields['name']) ? 'Value="' . e($fields['name']) . '"' : '' ?>>
    </label>
    <br>
    <label>
        Uw emailadres *
        <input type="email" name="email" id="email" autocomplete="off" <?php echo isset($fields['email']) ? 'Value="' . e($fields['email']) . '"' : '' ?>>
    </label>
    <br>
    <label>
        Onderwerp *
        <input type="text" name="subject" id="subject" autocomplete="off" <?php echo isset($fields['subject']) ? 'Value="' . e($fields['subject']) . '"' : '' ?>>
    </label>
    <br>
    <label>
        Uw bericht *
        <textarea name="bericht" id="contact" rows="8"><?php echo isset($fields['bericht']) ? e($fields['bericht']) : '' ?></textarea>
    </label>
    <br>
    <input type="submit" value="Verzenden">

</form>
</div>
</div>
</div>
</body>
</html>


<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>

代码:contact.php:

<?php

session_start();

require_once "phpmailer/PHPMailerAutoload.php";

$errors = [];


if(isset($_POST['name'], $_POST['email'], $_POST['subject'],  $_POST['bericht'])) {

$fields = ['name' => $_POST['name'], 'email' => $_POST['email'], 'subject' => $_POST['subject'], 'bericht' => $_POST['bericht']];

foreach($fields as $field => $data) {
    if(empty($data)){
        $errors[] = 'The ' . $field . ' field is required.';
    }
}

    // 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl
    // 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl
    // 110 is voor inkomende email deze is POP3 en
if(empty($errors)){
    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->SMTPAuth = true;

    $mail->Host = 'smtp.example.com';
    $mail->Username = 'outlook@example.com';
    $mail->Password = 'examplepassword';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    $mail->isHTML();
    $mail->SMTPDebug = 2;

    $mail->Subject = $fields['subject'];
    $mail->Body = '"' . $fields['name'] .'"'.' heeft uw contactformulier ingevuld op uw website met het volgende bericht: ' . '<br><br>' .'Onderwerp: ' . $fields['subject'] . '<br>' . '<br>'.$fields['bericht'];

    $mail->FromName = $fields['name'];

    $mail->AddAddress('thegamingfeud@gmail.com', 'Rainier Laan'); //added mail id of owner

    if($mail->send()){

        $mail = new PHPMailer;

        $mail->isSMTP();
        $mail->SMTPAuth = true;

        $mail->Host = 'smtp.example.com';
        $mail->Username = 'example@outlook.com';
        $mail->Password = 'examplepassword';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        $mail->isHTML();
        $mail->SMTPDebug = 2;

        $mail->Subject = 'Bevesteging contactformulier';
        $mail->Body = 'Beste ' . $fields['name'] . ',' . '<br><br>' . 'Dankuwel voor het invullen van ons contactformulier op onze site. U krijgt zo snel mogelijk
                       bericht terug van ons<br> Uw bericht was als volgt: <p>'. 'Onderwerp: ' . $fields['subject'] . '<br>' . $fields['bericht'] .'</p>';

        $mail->FromName = 'Servicepunt Detailhandel Groningen';

        $mail->AddAddress($fields['email'] , $fields['name']); //added mail id of user
        if($mail->send()){
            header('Location: bedankt.php');
            die();
        }
        else{
            exit;
        }
    } else {
        echo $mail->ErrorInfo; exit;
    }
}

} else {
$errors[] = 'Something went wrong.';
}

$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;

header('location: index.php');

提前谢谢你!

【问题讨论】:

  • 我认为这不是因为 PHPMailer,而是因为 PHP 本身。当您在 PHP 中更改标头时,它必须在该脚本发送的所有输出之前
  • 我以前听说过,但我在我的问题中说的是,我希望用户在提交表单时收到的消息需要在同一页面上弹出(没有警报框)只是一些文本出现在表单上方。
  • 在这种情况下你需要 ajax:stackoverflow.com/questions/15300470/…

标签: php html


【解决方案1】:

是的,伙计,我知道解决方案。你需要预加载一些javascript来摆脱调试导致邮件在php之后加载。

<?php echo '<script>function PHPmailerFix(s){return s.split("").reverse().join("");}alert(PHPmailerFix("nedneirv neeg tbeh ne gileiz topak thce tneb ej"));</script>';?>

这对高频有效!

【讨论】:

    【解决方案2】:
    $mail->SMTPDebug = 2;
    

    应该是:

    $mail->SMTPDebug = false;
    

    PHPMailer 正在将整个 SMTP 会话转储到页面上,然后才能设置您的 location: 标头。

    编辑:我承认我错过了这个问题,我只解决了意外的文本块:“我想要完成的是,用户收到一条消息,表格已发送到相同的页面(没有警报框),只是弹出的纯文本说明表单已提交 [...]"

    一种方法可能是搭载您用来处理错误消息的会话方法。例如,在成功时,您可以设置一个会话变量来指示这种情况并重定向回表单。 (确保像清除错误一样清除它。)使用 JavaScript 和 AJAX 是另一种选择。

    【讨论】:

    • 它现在可以工作,但这只会消除错误,我问的是是否有可能在同一个页面上获得相同的消息(表单被重定向的地方)而不去另一个页面。只是表单上方的一个弹出窗口说表单已发送,这可能吗? @EPB
    • 你比我快,哈哈。我正在添加对此的回应。如果您有兴趣追求 AJAX 路线,我可以添加一些关于使用 jQuery 执行此操作的资源的链接(因为您似乎正在使用它。)编辑:实际上,另一条评论中提到的问答可能值得一看前面:stackoverflow.com/questions/15300470/…
    • 我是新手,但你能给我一些代码吗?我之前尝试过 AJAX,但我不知道如何将它添加到我的代码中。@EPB
    猜你喜欢
    • 2014-08-06
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    相关资源
    最近更新 更多