php - Problems with sending an email using smtp mandrill
问题描述
I have a problem with my code, the intension is to send an email using smtp.mandrill.com and use the alert to know when the email is sended, otherwise send another alert when fails to send the email. And I recived the alert confirming the email, but is'nt sended. If anyone knows the problem I thanks a lot your help. This is the code.
<?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>';
}
?>
解决方案
将您的 SMTP 安全和端口更新为:
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
这解决了我将 Mandrill 与 SMTP 一起使用的问题。
推荐阅读
- ios - Bundle.main.path(forResource:) 返回 nil
- java - MediaPlayer 在 seek() 调用之前不会播放
- android - Gradle api 与多模块项目中的实现的最佳实践
- bash - 我如何将 / 更改为 \/
- java - 无法使用spring和hibernate集成自动创建表和插入记录
- python - 尝试加载谷歌播放页面时,Selenium 无法找到“app-id-title”元素
- thingsboard - 超过 5000 个数据点
- javascript - 无法自动填充复选框并选择字段
- ffmpeg - 如何将图像和音频合并为具有相同颜色的视频?(FFMPEG)
- sql - 如果我取消执行,我的查询是否需要“回滚”其更改