您好,我正在用smtp发送电子邮件。电子邮件是完全发送的,但脚本后的电子邮件发送脚本不工作。有人能帮我一下吗?
邮件发送成功,但是脚本没有执行。
邮件也需要2秒的时间来发送,但是邮件问题并不重要,因为脚本不工作
这是代码
<?php
require 'include/dbconfig.php';
require_once "vendor/autoload.php";
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
//PHPMailer Object
$mail = new PHPMailer(); //Argument true in constructor enables exceptions
// SMTP configuration
$mail->SMTPDebug = 1;
$mail->isSMTP();
$mail->Mailer = "smtp";
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "myemail@gmail.com";
$mail->Password = "emailpassword";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->isHTML(true);
$mail->setFrom('myemail@gmail.com', 'Sabir');
$mail->addAddress("test@gmail.com");
$mail->Subject = "Test";
$mail->Body = "Test Email";
$email_sent=$mail->send();
if(!$email_sent) { ?>
<script language="javascript" type="text/javascript">
alert('Something went wrong');
</script><?php
} else { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message');
</script>
<?php
}
?>
早上好
你可以把语句精简一点:
$email_sent=$mail->send();
if(!$email_sent) { ?>
<script language="javascript" type="text/javascript">
alert('Something went wrong');
</script><?php
} else { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message');
</script>
<?php
}
它看起来应该是这样的:
$email_sent = $mail->send();
if(!$email_sent) {}else {}
你也不需要让所有这些中断,把你的js放到echo中,然后删除:
language="javascript"
用 代替
echo "<script>alert('Something went wrong');</script>";
所以它看起来像这样:
$email_sent = $mail->send();
if(!$email_sent) {
echo "<script>alert('Something went wrong');</script>";
} else {
echo "<script>alert('Thank you for the message');</script>";
}
编辑
差点忘了!
现在我们已经清除了一些代码,但它不会像那样工作。
你还需要调整你的表单:
onclick or onsubmit
或者使用ajax调用:
$('#mailform').on('submit', function(e)
{
e.preventDefault();
$.ajax({
data: {data_field: 'value'},
type: 'post',
url: '/script.php',
success: function(r) {$('#thepoup').removeClass('hidden')},
error: function(r) {alert('error'); console.log(r)}
});
});