PHPMailer 发送重复的电子邮件
PHPMailer sends duplicate emails
问题: 我收到了三封重复的电子邮件,每个用户都使用他们的电子邮件地址注册。我的客人名单被重复的电子邮件淹没了,我不知道我的代码有什么问题。
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代码:
$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 文件的表单,但它们不应该是这里的问题。我只是把它加在这里,以防万一有人能想出办法。
表格 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 邮件专家请告诉我如何解决这个问题,我认为它可能是 Bootstrap JS 4.13。任何建议或帮助将不胜感激。谢谢!
我不知道你使用的是哪个版本的 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。
我认为深入研究您的 jQuery / JS 代码可能会找到答案,但我的猜测是您表单中的 button
元素会触发一些影响其他表单的事件。
您是否尝试将所有按钮更改为 <input type='submit'/>
?
在您的服务器中启动会话并存储邮件发送信息
// 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;
}
}
?>
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);
}
尝试为每个按钮创建一个单独的 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>
首先:Bootstrap 不是这里的罪魁祸首,JS 也不是。如果 PHP(或 php 邮件程序)也很干净,那么您可能需要查看邮件服务器设置(与您的主机核实)。
但是,也许表单被提交了两次?让我们检查一下:
- 打开googlechrome调试器(F12)
- 点击 "Network",勾选 "Preserve log"
- 单击 "Clear"(左侧第二个图标)
- 运行您的楷书(即填写表格并提交)
- "Network" 选项卡将显示您的 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;
}
祝你好运
我的理解正确吗? o_O
当然,使用 php 代码,您将发送多封电子邮件。
只需像这样更改您的 php 代码,以便您解析 mail1
和 mail2
有条件地分隔:
if (!empty($_POST['topsubmit']))
{
$mail2 = new PHPMailer();
...
if(!$mail2->Send())
...
}
if (!empty($_POST['footersubmit']))
{
$mail2 = new PHPMailer();
...
if(!$mail2->Send())
...
}
因此,不会在同一页面加载时发送所有电子邮件。
这是我问题的完整答案,感谢@SearchingSolutions 向我展示了解决此问题的正确方法。
我为解决这个问题所做的是创建 2 个不同的按钮,每个按钮都有单独的 onclick-handlers。每个按钮都有不同的 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();
}
问题: 我收到了三封重复的电子邮件,每个用户都使用他们的电子邮件地址注册。我的客人名单被重复的电子邮件淹没了,我不知道我的代码有什么问题。
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代码:
$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 文件的表单,但它们不应该是这里的问题。我只是把它加在这里,以防万一有人能想出办法。
表格 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 邮件专家请告诉我如何解决这个问题,我认为它可能是 Bootstrap JS 4.13。任何建议或帮助将不胜感激。谢谢!
我不知道你使用的是哪个版本的 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。
我认为深入研究您的 jQuery / JS 代码可能会找到答案,但我的猜测是您表单中的 button
元素会触发一些影响其他表单的事件。
您是否尝试将所有按钮更改为 <input type='submit'/>
?
在您的服务器中启动会话并存储邮件发送信息
// 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;
}
}
?>
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);
}
尝试为每个按钮创建一个单独的 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>
首先:Bootstrap 不是这里的罪魁祸首,JS 也不是。如果 PHP(或 php 邮件程序)也很干净,那么您可能需要查看邮件服务器设置(与您的主机核实)。
但是,也许表单被提交了两次?让我们检查一下:
- 打开googlechrome调试器(F12)
- 点击 "Network",勾选 "Preserve log"
- 单击 "Clear"(左侧第二个图标)
- 运行您的楷书(即填写表格并提交)
- "Network" 选项卡将显示您的 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;
}
祝你好运
我的理解正确吗? o_O
当然,使用 php 代码,您将发送多封电子邮件。
只需像这样更改您的 php 代码,以便您解析 mail1
和 mail2
有条件地分隔:
if (!empty($_POST['topsubmit']))
{
$mail2 = new PHPMailer();
...
if(!$mail2->Send())
...
}
if (!empty($_POST['footersubmit']))
{
$mail2 = new PHPMailer();
...
if(!$mail2->Send())
...
}
因此,不会在同一页面加载时发送所有电子邮件。
这是我问题的完整答案,感谢@SearchingSolutions 向我展示了解决此问题的正确方法。
我为解决这个问题所做的是创建 2 个不同的按钮,每个按钮都有单独的 onclick-handlers。每个按钮都有不同的 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();
}