PHPMailer发送重复的电子邮件

时间:2018-11-13 23:39:31

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

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

  

可能的原因:我有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。任何建议或帮助将不胜感激。谢谢!

8 个答案:

答案 0 :(得分:2)

尝试为每个按钮创建一个单独的commit()函数(带有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>

答案 1 :(得分:1)

首先,引导不是这里的罪魁祸首,JS也不是。而且,如果PHP(或php mailer)也很干净,那么您可能想看看邮件服务器的设置(请与您的主机联系)。

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

  1. 打开Goog​​le chrome调试器(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;
    }

好运

答案 2 :(得分:0)

我不知道您使用的是哪个版本的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->send()),它是如何工作的,但是您应该使用if($mail->send()),这是文档中推荐的方式

此代码段来自6.0.3版。

答案 3 :(得分:0)

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

您是否尝试将所有按钮都更改为<input type='submit'/>

答案 4 :(得分:0)

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

Rest

答案 5 :(得分:0)

  

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

// 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);
}

答案 6 :(得分:0)

我理解正确吗? 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())
    ...
}

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

答案 7 :(得分:0)

这是我问题的完整答案,这要感谢@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();
}