【问题标题】:PHPMailer sends duplicate emailsPHPMailer 发送重复的电子邮件
【发布时间】:2019-04-16 21:08:41
【问题描述】:

问题:对于使用其电子邮件地址注册的每个用户,我都会收到三封重复的电子邮件。我的来宾列表被重复的电子邮件所淹没,我不知道我的代码有什么问题。

可能的原因:我有 3 个注册表单,所以我想当我提交一个注册表单时,他们都同时提交。问题在于 Bootstrap 4.1.3 JS 或 HTML。

PHP 代码:

$email = $_REQUEST['email'] ;

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require './PHPMailer-master/src/Exception.php';
require './PHPMailer-master/src/PHPMailer.php';
require './PHPMailer-master/src/SMTP.php';

// Popup Form Signup
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.domain.com";
$mail->SMTPAuth = true;
$mail->Username = "user@domain.com"; 
$mail->Password = "password"; 
$mail->SMTPSecure = 'TLS';                           
$mail->Port = 587;
$mail->From = $email;
$mail->setFrom('$email', 'Guest');
$mail->addAddress('admin@domain.com', 'Admin');
$mail->IsHTML(true);
$mail->Subject = "New Member!";
$mail->Body = $email;
$mail->AltBody = $email;

$mail2 = new PHPMailer();
$mail2->IsSMTP();
$mail2->Host = "mail.domain.com"; 
$mail2->SMTPAuth = true; 
$mail2->Username = "user@domain.com"; 
$mail2->Password = "password"; 
$mail2->SMTPSecure = 'TLS';                      
$mail2->Port = 587;                             
$mail2->setFrom('support@domain.com', 'Support');
$mail2->AddAddress("$email");    
$mail2->AddReplyTo('support@domain.com');

    $mail2->Subject = "Thanks for signing up!";
    $message = '';
    $mail2->Body = $message;
    $mail2->AltBody = $message;


    if(!$mail2->Send())
    {
    echo "Message could not be sent. Please reload the page and try again. <p>";
    echo "Mailer Error: " . $mail2->ErrorInfo;
    exit;
    }

    if(!$mail->Send())
    {
    echo "Message was not received. Please reload the page and try again.<p>";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
    }

我还有其他打开不同 PHP 文件的表单,但它们不应该是这里的问题。我只是在这里添加它以防万一有人能弄清楚。

FORM 1(相同的着陆页)

<form class="signupForm1 input-group mt-1" method="post" action="topconfirm">
        <div class="input-group">
        <label for="email" class="sr-only">Enter your email address</label>
        <input style ="overflow:hidden;" id="email" type="email" name="email" required class="sentence form-control" aria-label="Large" placeholder="enter your email address">      
        <button style="font-size:17px;" name="topsubmit" type="submit" class="ctabutton semibolder submitButton sentence text-white btn btn-secondary px-lg-3 px-md-1 px-sm-1 btn-lg rounded-0">Get Started</button>
        </div>              
</form>

FORM 2(相同的着陆页)

<form class="signupForm2 input-group mt-1" method="post" action="bottomconfirm">
                <div class="input-group">
                <label style="font-weight:normal!important;" for="email" class="sr-only">Enter your email address</label>
                <input style ="overflow:hidden;"  id="email" type="email" name="email" required class="sentence form-control" aria-label="Large" placeholder="enter your email address">         
                <input style="font-size:17px;" name="footersubmit" type="submit" class="ctabutton semibolder submitButton sentence text-white btn btn-secondary px-lg-3 px-md-1 px-sm-1 btn-lg rounded-0" value="Get Started"/>
                </div>              
        </form>

我不是 JavaScript 或 PHP 邮件专家,请告诉我如何解决这个问题,我认为它可能是 Bootstrap JS 4.13。任何建议或帮助将不胜感激。谢谢!

【问题讨论】:

  • 你不需要初始化 PHPMailer 两次来发送 2 封电子邮件。您还使用 php 的 mail() 以及 phpmailer,说实话,这有点乱。
  • 以不同形式输入具有相同名称的输入应该无关紧要。您一次只能提交一个表单。
  • 你的 jquery 是什么样的?您在同一页面上有相同的#id。你的行动中的确认是什么?你应该把它留空
  • 确认触发confirm.php页面,在输入有效电子邮件并点击提交按钮后发送电子邮件
  • 如果您使用变量并根据 if(isset($_POST['variable'])) 替换它们,phpmailer 可以清理到一半的代码

标签: javascript php twitter-bootstrap bootstrap-4 phpmailer


【解决方案1】:

我不知道您使用的是哪个版本的 PHPMailer,但您也在使用 PHP 的 mail() 方法,这很讽刺,因为您也在使用 PHPMailer。但是,这就是我在我的一个项目中的做法:

function sendAuthEmail($email, $token, $role) {
        try {
            $mail = new PHPMailer(true);
            $mail->SMTPDebug = 0; //SMTP Debug
            $mail->isSMTP();
            $mail->Host = "smtp.gmail.com";
            $mail->SMTPAuth = true;
            $mail->Username = 'example@example.com';
            $mail->Password = 'password';
            $mail->SMTPSecure = 'tls';
            $mail->Port = '587';
            /**
             * SMTPOptions work-around by @author : Synchro
             * This setting should removed on server and
             * mailing should be working on the server
             */
            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );

            // Recipients

            $mail->setFrom('no-reply@confirmation.com', 'Do Not Reply');
            $mail->addAddress($email, 'Yash Karanke'); // Add a recipient
            $mail->addReplyTo('no-reply@confirmation.com');

            // Content

            $mail->isHTML(true); // Set email format to HTML
            $mail->Subject = 'Authentication Email';
            $mail->Body = 'text to send';
            if ($mail->send()) {
                return true;
            }
            else {
                return false;
                exit();
            }
        }

        catch(Exception $e) {
            return $mail->ErrorInfo;
        }
    }

如果您正在寻找更简单的示例,请删除包装函数并仅使用您需要的代码。更多详情Docs

在您的代码中,您使用的是if(!$mail-&gt;send()),我不确定它是如何工作的,但您应该使用文档中推荐的if($mail-&gt;send())

此代码 sn-p 来自版本 6.0.3。

【讨论】:

  • 当我注释掉邮件时($to, $subject, $message, $headers);它不起作用,但我确实删除了 $email = $_REQUEST['email'] ;没有它它就可以工作。但是,您的上述解决方案不起作用,我仍然收到重复的电子邮件。
  • 请检查新更新的 PHPMailer 代码,现在我认为问题与 jQuery 提交脚本有关。让我知道你的想法。
  • @ChosenJuan 是的,我查了,和jQuery Code没有冲突。
  • 是的,我实际上完全删除了 jQuery,甚至没有必要。我认为问题在于它同时提交了所有三个表单,但我不知道如何在保留所有三个表单的同时解决这个问题。有什么想法吗?
【解决方案2】:

我认为深入研究您的 jQuery / JS 代码可能会带来答案,但我的猜测是您的表单中的 button 元素会触发一些影响其他表单的事件。

您是否尝试将所有按钮更改为&lt;input type='submit'/&gt;

【讨论】:

  • 我尝试更改要提交的输入,但这也不起作用。唯一有问题的 JQuery 代码可能是 Jquery 验证代码,我将很快发布它
【解决方案3】:

在您的服务器中启动会话并存储发送的邮件信息

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require './PHPMailer-master/src/Exception.php';
require './PHPMailer-master/src/PHPMailer.php';
require './PHPMailer-master/src/SMTP.php';

// Popup Form Signup
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "mail.domain.com";
$mail->SMTPAuth = true;
$mail->Username = "user@domain.com"; 
$mail->Password = "password"; 
$mail->SMTPSecure = 'TLS';                           
$mail->Port = 587;
$mail->From = $email;
$mail->setFrom('$email', 'Guest');
$mail->addAddress('admin@domain.com', 'Admin');
$mail->IsHTML(true);
$mail->Subject = "New Member!";
$mail->Body = $email;
$mail->AltBody = $email;

$mail2 = new PHPMailer();
$mail2->IsSMTP();
$mail2->Host = "mail.domain.com"; 
$mail2->SMTPAuth = true; 
$mail2->Username = "user@domain.com"; 
$mail2->Password = "password"; 
$mail2->SMTPSecure = 'TLS';                      
$mail2->Port = 587;                             
$mail2->setFrom('support@domain.com', 'Support');
$mail2->AddAddress("$email");    
$mail2->AddReplyTo('support@domain.com');

    $mail2->Subject = "Thanks for signing up!";
    $message = '';
    $mail2->Body = $message;
    $mail2->AltBody = $message;


if(!$_SESSION['is_mail_1_send']){//check mail sended once
    if(!$mail2->Send())
    {
    echo "Message could not be sent. Please reload the page and try again. <p>";
    echo "Mailer Error: " . $mail2->ErrorInfo;
   $_SESSION['is_mail_1_send']=false;
 exit;
    }else{
            $_SESSION['is_mail_1_send']=true;   
    }
}
if(!$_SESSION['is_mail_2_send']){//check mail sended once
    if(!$mail->Send())
    {
    echo "Message was not received. Please reload the page and try again.<p>";
    echo "Mailer Error: " . $mail->ErrorInfo;
$_SESSION['is_mail_2_send']=false;
    exit;
    }else{
        $_SESSION['is_mail_2_send']=true;
    }
}

?>

【讨论】:

  • 这不起作用,它仍然发送重复的电子邮件。这应该将日志发送到我的电子邮件吗?
  • 您使用我的会话设置收到了多少封电子邮件我尝试了我的代码它只发送 2 封邮件
  • 它不断地从 support@domain.com 向用户发送三封重复的电子邮件,向我的 admin@domain.com 发送三封重复的电子邮件
【解决方案4】:

您在哪里处理表单提交事件?创建一个单独的函数 用于发送邮件。当用户单击特定表单时调用该函数 提交按钮。如下所示

// Your separate Mail sending function
sendMail(arg1, arg2, arg3){
   // PHP Mailer Configurations
}

// Calling your first form
if(isset($_POST['topsubmit']){
  sendMail(arg1, arg2, arg3);
}

// Calling your second form
if(isset($_POST['footersubmit']){
  sendMail(arg1, arg2, arg3);
}

【讨论】:

  • 能否详细说明如何合并上述代码?我应该只复制 sendmail 部分并将其放在上面还是应该替换某些东西?第二个代码应该只是替换 if if(!$mail->Send()) 代码?你能更新你的样本吗?
  • if(isset($_POST['topsubmit']){ if(!$mail->Send()) // 这样应该可以工作。 } // 但是如果你发送邮件相关的完整配置对于一个单独的函数来说,这是一个很大的打击。
  • 我可以添加这个发送函数来替换两个 if(!$mail->Send()) 函数 // 调用你的第一个表单 if(isset($_POST['topsubmit']){ sendMail ($mail, $mail2); echo "无法发送消息。请返回 voozlr.com 重试。如果问题仍然存在,请通过 voozlr.com/contact 联系我们。感谢您的耐心等待!

    "; echo "Mailer Error: " . $mail, $mail2->ErrorInfo; exit; }

  • if(isset($_POST['footersubmit']){ if(!$mail2->Send()) { echo "消息无法发送,请返回voozlr.com尝试再次。如果问题仍然存在,请通过 voozlr.com/contact 联系我们。感谢您的耐心等待!

    "; echo "Mailer Error: " . $mail2->ErrorInfo; exit; } 它不起作用。我做错了什么吗?还是我错过了什么?

【解决方案5】:

尝试为每个按钮创建一个单独的 submit() 函数(使用 onclick 处理程序)并从中删除 value='submit',如下所示:

    <!--signupform1-->
    <form class="signupForm1 input-group mt-1" method="post" action="topconfirm">
        <div class="input-group">
            <label for="email" class="sr-only">Enter your email address</label>
            <input style ="overflow:hidden;" id="email" type="email" name="email" required class="sentence form-control" aria-label="Large" placeholder="enter your email address">      
            <button style="font-size:17px;" name="topsubmit" type="button" class="ctabutton semibolder submitButton sentence text-white btn btn-secondary px-lg-3 px-md-1 px-sm-1 btn-lg rounded-0" onclick="submit1();">Get Started</button>
        </div>              
    </form>

    <!--signupform2-->
    <form class="signupForm2 input-group mt-1" method="post" action="bottomconfirm">
        <div class="input-group">
            <label style="font-weight:normal!important;" for="email" class="sr-only">Enter your email address</label>
            <input style ="overflow:hidden;"  id="email" type="email" name="email" required class="sentence form-control" aria-label="Large" placeholder="enter your email address">         
            <input style="font-size:17px;" name="footersubmit" type="button" class="ctabutton semibolder submitButton sentence text-white btn btn-secondary px-lg-3 px-md-1 px-sm-1 btn-lg rounded-0" onclick="submit2();" value="Get Started"/>
        </div>              
    </form>

然后在你的 js 中:

<script>
    function submit1() {
        $(".signupForm1").submit();
    }
    function submit2() {
        $(".signupForm2").submit();
    }
</script>

【讨论】:

  • 这个答案效果最好,它是导致问题的提交按钮。然而,为了让它工作,我不得不将 type="submit" 添加到两个表单上的按钮中。到目前为止,我一直从每个表格中收到单独的电子邮件。干杯!!!谢谢你的回答!
【解决方案6】:

首先:Bootstrap 不是罪魁祸首,JS 也不是。如果 PHP(或 php mailer)也很干净,那么您可能需要查看邮件服务器设置(请咨询您的主机)。

但是,表单可能被提交了两次?让我们检查一下:

  1. 打开谷歌浏览器调试器 (F12)
  2. 点击“网络”,勾选“保存日志”
  3. 点击“清除”(左侧第二个图标)
  4. 运行常规脚本(即填写表格并提交)
  5. “网络”选项卡将显示您的 PHP 页面的任何重复调用

如果还是失败,试试这个:

下载https://github.com/PHPMailer/PHPMailer

创建一个完全空白的文件,直接将您的 PHP 电子邮件行放入其中。删除表单、输入、javascript、isset 和其余部分。通过简单地调用它来运行这个文件,所以它看起来像:

    require_once("PHPMailerAutoload.php"); // <---- only this will be required (put your path properly)

    $mail2 = new PHPMailer();
    $mail2->IsSMTP();
    $mail2->Host = "mail.domain.com"; 
    $mail2->SMTPAuth = true; 
    $mail2->Username = "user@domain.com"; 
    $mail2->Password = "password"; 
    $mail2->SMTPSecure = 'TLS';                      
    $mail2->Port = 587;                             
    $mail2->setFrom('support@domain.com', 'Support');
    $mail2->AddAddress("$email");    
    $mail2->AddReplyTo('support@domain.com');

    $mail2->Subject = "Thanks for signing up!";
    $message = '';
    $mail2->Body = $message;
    $mail2->AltBody = $message;


    if(!$mail2->Send())
    {
    echo "Message could not be sent. Please reload the page and try again. 
    <p>";
    echo "Mailer Error: " . $mail2->ErrorInfo;
    exit;
    }

祝你好运

【讨论】:

  • 感谢调试器教程,非常有价值。但是,当我将电子邮件提交到表单时,我的日志并没有特别显示代码有任何问题。似乎它需要一个 JS 解决方案来修复它。还是谢谢!
  • @ChosenJuan 我想我们大多数人都错过了:从一开始就不应该将 JS 表单提交事件绑定到 html 提交按钮。 :D
【解决方案7】:

我理解正确吗? o_O

当然,使用该 php 代码,您将发送多封电子邮件。

只需像这样更改您的 php 代码,以便您解析 mail1mail2 有条件地分开:

if (!empty($_POST['topsubmit']))
{
    $mail2 = new PHPMailer();
    ...
    if(!$mail2->Send())
    ...
}

if (!empty($_POST['footersubmit']))
{
    $mail2 = new PHPMailer();
    ...
    if(!$mail2->Send())
    ...
}

因此,所有电子邮件都不会在同一个页面加载上发送。

【讨论】:

  • 我同时发送两封电子邮件。一封电子邮件:$mail 给我自己在 admin.domain.com 和另一封电子邮件:$mail2 给输入他们电子邮件的用户。但是你的回答是拆分顶部提交和页脚提交,但我将顶部提交和页脚提交拆分为单独的 PHP 文件,分别标记为 topconfirm.php 和 footerconfirm.php 这两个 php 文件分别在 mail 和 mail2 发送两封电子邮件,其中一封给新注册和一个给我。如何设置?
【解决方案8】:

这是我问题的完整答案,感谢@SearchingSolutions 向我展示了解决此问题的正确方法。

我解决这个问题的方法是创建 2 个不同的按钮,每个按钮都有单独的 onclick 处理程序。每个按钮都有不同的 id,我在两者上都添加了 TYPE="Submit"。我给每个表单取了一个不同的名称,并添加了一个小的 JS 脚本来在调用特定的 onclick 处理程序时提交特定的表单。

到目前为止,每封电子邮件都按预期送达,一切都再次顺利进行。

感谢大家的精彩回答!

HTML:

<form class="signupForm1 input-group mt-1" method="post" action="topconfirm">
        <div class="input-group">
        <label for="email2" class="sr-only">Enter your email address</label>
        <input style ="overflow:hidden;" id="email2" type="email" name="email2" required class="sentence form-control" aria-label="Large" placeholder="enter your email address">        
        <button style="font-size:17px;" name="topsubmit" type="submit" class="ctabutton semibolder sentence text-white btn btn-secondary px-lg-3 px-md-1 px-sm-1 btn-lg rounded-0" onclick="submit1();">Get Started"</button>
        </div>              
</form>


<form class="signupForm2 input-group mt-1" method="post" action="footerconfirm">
        <div class="input-group">
        <label style="font-weight:normal!important;" for="email3" class="sr-only">Enter your email address</label>
        <input style ="overflow:hidden;"  id="email3" type="email" name="email3" required class="sentence form-control" aria-label="Large" placeholder="enter your email address">       
        <button style="font-size:17px;" name="footersubmit" type="submit" class="ctabutton semibolder sentence text-white btn btn-secondary px-lg-3 px-md-1 px-sm-1 btn-lg rounded-0" onclick="submit2();">Get Started"</button>
        </div>              
</form>

JS 脚本:

<script>
function submit1() {
    $(".signupForm1").submit();
}
function submit2() {
    $(".signupForm2").submit();
}

【讨论】:

    猜你喜欢
    • 2014-07-04
    • 2017-01-16
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2013-10-05
    • 1970-01-01
    相关资源
    最近更新 更多