Bootstrap PHPMailer发送重复的电子邮件

rvpgvaaj  于 2023-09-28  发布在  Bootstrap
关注(0)|答案(8)|浏览(118)

**问题:**我收到三个重复的电子邮件,每个用户谁签署了他们的电子邮件地址。我的客人名单被重复的邮件淹没了,我不知道我的代码出了什么问题。
可能原因:我有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 = "[email protected]"; 
$mail->Password = "password"; 
$mail->SMTPSecure = 'TLS';                           
$mail->Port = 587;
$mail->From = $email;
$mail->setFrom('$email', 'Guest');
$mail->addAddress('[email protected]', '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 = "[email protected]"; 
$mail2->Password = "password"; 
$mail2->SMTPSecure = 'TLS';                      
$mail2->Port = 587;                             
$mail2->setFrom('[email protected]', 'Support');
$mail2->AddAddress("$email");    
$mail2->AddReplyTo('[email protected]');

    $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文件的其他表单,但它们不应该是这里的问题。我只是把它加在这里,以防有人能弄清楚。

表格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>

表格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邮件Maven,请告诉我如何解决这个问题,我认为这可能是一个Bootstrap JS 4.13。任何建议或帮助将不胜感激。谢谢你,谢谢

w7t8yxp5

w7t8yxp51#

尝试为每个按钮创建一个单独的submit()函数(使用onclick-handler),并从它们中删除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>
5anewei6

5anewei62#

首先:Bootstrap不是罪魁祸首,JS也不是。如果PHP(或php mailer)也很干净,那么你可能想看看邮件服务器设置(与你的主机检查)。
但是,可能表单提交了两次?让我们检查一下:
1.打开google Chrome debugger(F12)
1.点击“网络”,勾选“保存日志”
1.点击“清除”(左侧第二个图标)
1.运行您的常规脚本(即填写表格并提交)
1.“网络”选项卡将显示您的PHP页面的任何重复调用
如果仍然失败,请尝试以下操作:
下载https://github.com/PHPMailer/PHPMailer
创建一个完全空白的文件,直接把你的PHP邮件行放在里面。删除表单、input、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 = "[email protected]"; 
    $mail2->Password = "password"; 
    $mail2->SMTPSecure = 'TLS';                      
    $mail2->Port = 587;                             
    $mail2->setFrom('[email protected]', 'Support');
    $mail2->AddAddress("$email");    
    $mail2->AddReplyTo('[email protected]');

    $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;
    }

祝你好运

mrwjdhj3

mrwjdhj33#

我不知道您使用的是哪个版本的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 = '[email protected]';
            $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('[email protected]', 'Do Not Reply');
            $mail->addAddress($email, 'Yash Karanke'); // Add a recipient
            $mail->addReplyTo('[email protected]');

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

如果你正在寻找一个更简单的例子,删除 Package 的函数,只使用你需要的代码。更多详情
在你的代码中,你使用了if(!$mail->send()),我不确定它是如何工作的,但是你应该使用if($mail->send()),这是文档中推荐的
此代码片段来自verion 6.0.3。

0pizxfdo

0pizxfdo4#

我认为深入研究jQuery / JS代码可以找到答案,但我的猜测是表单中的button元素触发了一些影响其他表单的事件。
你试过把所有的按钮都改成<input type='submit'/>吗?

0g0grzrc

0g0grzrc5#

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

// 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 = "[email protected]"; 
$mail->Password = "password"; 
$mail->SMTPSecure = 'TLS';                           
$mail->Port = 587;
$mail->From = $email;
$mail->setFrom('$email', 'Guest');
$mail->addAddress('[email protected]', '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 = "[email protected]"; 
$mail2->Password = "password"; 
$mail2->SMTPSecure = 'TLS';                      
$mail2->Port = 587;                             
$mail2->setFrom('[email protected]', 'Support');
$mail2->AddAddress("$email");    
$mail2->AddReplyTo('[email protected]');

    $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;
    }
}

?>
qfe3c7zg

qfe3c7zg6#

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

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

z4iuyo4d7#

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

因此,所有的电子邮件不会在同一个pagload上发送。

2exbekwf

2exbekwf8#

这是对我问题的完整回答,感谢@SearchingSolutions向我展示了解决这个问题的正确方法。
为了解决这个问题,我创建了两个不同的按钮,每个按钮都有单独的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();
}

相关问题