首页 > 解决方案 > 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>';
}
?>

标签: phpemailmailchimpmandrill

解决方案


将您的 SMTP 安全和端口更新为:

$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;                           

这解决了我将 Mandrill 与 SMTP 一起使用的问题。


推荐阅读