I’m working on building an email system for my website using PHP and Mailgun’s SMTP service. However, I keep getting an SMTP host connection error when trying to send emails.
Here’s the code I’m using:
<?php
require("../config/settings.php");
require("../vendor/mailgun/autoload.php");
require("../mailer/phpmailer.class.php");
use Mailgun\Mailgun;
if($_SERVER["REQUEST_METHOD"] === "GET")
{
display("contact.php", ["page_title" => "Get in touch"]);
}
if($_SERVER["REQUEST_METHOD"] === "POST")
{
$emailer = new PHPMailer;
$emailer->isSMTP();
$emailer->Host = 'smtp.mailgun.org';
$emailer->SMTPAuth = true;
$emailer->Username = '[email protected]';
$emailer->Password = '*my_smtp_password';
$emailer->SMTPSecure = 'ssl';
$emailer->Port = '465';
$emailer->From = '[email protected]';
$emailer->FromName = 'Contact Form';
$emailer->addAddress('[email protected]');
$emailer->isHTML(true);
$emailer->Subject = 'New contact message';
$emailer->Body = $_POST["user_message"];
if(!$emailer->send())
{
echo "Email delivery failed.";
echo 'Error details: ' . $emailer->ErrorInfo . "\n";
}
else
{
header("Location: success.html");
}
}
?>
This is for a school project so I’m using the free sandbox domain. Has anyone run into similar connection problems? Any suggestions would be helpful.