问题
PROBLEM: I'm receiving three duplicate emails for each user who signs up with their email address. My guest list is being overrun by duplicate emails, and I don't know what's wrong with my code.
Probable Cause: I have 3 signup forms, so I think somehow when I submit one signup form they all submit at the same time. The problem lies in either the Bootstrap 4.1.3 JS or the HTML.
PHP Code:
$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;
}
I also have other Forms that open different PHP files, but they shouldn't be the issue here. I just added it here just in case anyone can figure something out.
FORM 1 (Same Landing Page)
<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 (Same Landing Page)
<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>
I'm not a JavaScript or PHP mailer expert someone please show me how to fix this problem, I think it might be a Bootstrap JS 4.13. Any suggestion or help will be greatly appreciated. Thanks!
回答1:
Try creating a seperate submit()-function (with the onclick-handler) for each button and removing the value='submit'
from them, like so:
<!--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>
And then in your js:
<script>
function submit1() {
$(".signupForm1").submit();
}
function submit2() {
$(".signupForm2").submit();
}
</script>
回答2:
First off: Bootstrap ain't the culprit here, JS neither. And if if PHP (or php mailer) are clean too, then you may want to take a look at the mail server settings (check with your host).
However, maybe the form is submitted twice? Let's check:
- open google chrome debugger (F12)
- Click on "Network", tick "Preserve log"
- Click on "Clear" (2nd icon left)
- Run your regular script (i.e. fill the form and submit)
- "Network" tab will show you any duplicate call of your PHP page
If it still fails, try this:
download https://github.com/PHPMailer/PHPMailer
create a completely blank file, directly put your PHP emailing lines in it. Remove the form, input, javascript, isset and the rest. Run this file by simply calling it, so it would look like:
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;
}
godd luck
回答3:
I don't know which version of PHPMailer you're using but you're also using PHP's mail() method, which is quite ironic because you're also using PHPMailer. But nevertheless, this is how I did it in one of my projects :
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;
}
}
If you're looking for a simpler example, remove the wrapped function and use only code which you need. More details on Docs
In your code you're using if(!$mail->send())
which I'm not sure, how it is working, but you should use if($mail->send())
which is recommended way from the docs
This code snippet is from verion 6.0.3.
回答4:
I think that a deep dive into your jQuery / JS code could bring the answer, but my guess is that the button
elements in your forms trigger some event that affects the other forms.
Did you try changing all the buttons to <input type='submit'/>
?
回答5:
start a session in your server and store the mail sent information
// 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;
}
}
?>
回答6:
Where you are handling form submit event? Create a separate function for sending mail. call that function when user click on specific form submit button. like below
// 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);
}
回答7:
Did I understand it correctly? o_O
Of course, with that php code, you are sending multiple emails.
just change your php code like this, so you parse the mail1
and mail2
conditionally separated:
if (!empty($_POST['topsubmit']))
{
$mail2 = new PHPMailer();
...
if(!$mail2->Send())
...
}
if (!empty($_POST['footersubmit']))
{
$mail2 = new PHPMailer();
...
if(!$mail2->Send())
...
}
thus, all of the emails wont be sent on the same pageload.
回答8:
This is the complete answer to my question, thanks to @SearchingSolutions for showing me the correct way to solve this issue.
What I did to fix this problem was to create 2 different buttons with separate onclick-handlers for each button. Each button has different ids and I added the TYPE="Submit" on both. I gave every form a different name, and added a small JS script to submit the specific form when the specific onclick handler is called.
So far, every email is coming in as intended, and everything is running smoothly again.
Thank you all for the great answers!
The 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>
The JS Script:
<script>
function submit1() {
$(".signupForm1").submit();
}
function submit2() {
$(".signupForm2").submit();
}
来源:https://stackoverflow.com/questions/53291081/phpmailer-sends-duplicate-emails