【发布时间】:2021-10-26 22:54:40
【问题描述】:
我的代码有问题,其意图是使用 smtp.mandrill.com 发送电子邮件并使用警报知道电子邮件何时发送,否则在发送电子邮件失败时发送另一个警报。我收到了确认电子邮件的警报,但没有发送。如果有人知道这个问题,我非常感谢你的帮助。 这是代码。
<?php
$name = $_POST["first_name"];
$telefono = $_POST["telephone"];
$texto = $_POST["text"];
$body =
"Name: ".$name."<br>
Telephone: ".$telephone."<br>
Message: ".$text;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPmailer/Exception.php';
require 'PHPmailer/PHPMailer.php';
require 'PHPmailer/SMTP.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 0; //"2" Enable verbose debug output. Change to "0" to hide all the letters n.n
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.mandrill.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'example@example.com'; //SMTP username
$mail->Password = 'Any API key'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465;
//Recipients
$mail->setFrom('example@example', 'Example');
$mail->addAddress('example@example'); //Add a recipient
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Contact';
$mail->Body = 'New message from <b>Contact</b><br><br>'.$body;
$mail->CharSet = 'UTF-8';
$mail->send();
echo '<script>
alert("The data was sent successfully.");
window.history.go(-1);
</script>';
} catch (Exception $e) {
echo '<script>
alert("An error occurred while sending the data.");
window.history.go(-1);
</script>';
}
?>
【问题讨论】:
-
尝试将
$mail->SMTPDebug设置为2,以启用详细调试输出。它显示了什么?
标签: php email mailchimp mandrill