0

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>';
}
?>

Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Tangentially Perpendicular Aug 26 '21 at 22:38
  • 1
    Try setting `$mail->SMTPDebug` to 2, to enable verbose debugging output. What does it show? – mti2935 Aug 27 '21 at 02:14

1 Answers1

0

Update your SMTP SECURE AND PORT TO:

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

That solved my issues with using Mandrill with SMTP.