I've been trying to implement a contact service using Mailgun coded in PHP. I receive the following error:
Could not connect to SMTP host
Below is my code:
<?php
require("../includes/config.php");
require("../mailgun-php/vendor/autoload.php");
require("../phpmailer/_lib/class.phpmailer.php");
use Mailgun\Mailgun;
if($_SERVER["REQUEST_METHOD"] === "GET")
{
render("contact-form.php", ["title" => "Contact us"]);
}
if($_SERVER["REQUEST_METHOD"] === "POST")
{
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mailgun.org'; // Specify mailgun SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'postmaster@sandboxb9cc446d8b7240efa59917c68fae6e50.mailgun.org'; // SMTP username from https://mailgun.com/cp/domains
$mail->Password = '*SMTP password from sandbox domain'; // SMTP password from https://mailgun.com/cp/domains
$mail->SMTPSecure = 'sll'; // Enable encryption, 'ssl'
$mail->Port= '465';
$mail->From = 'sandboxb9cc446d8b7240efa59917c68fae6e50.mailgun.org'; // The FROM field, the address sending the email
$mail->FromName = 'Enquiry bot'; // The NAME field which will be displayed on arrival by the email client
$mail->addAddress('****@gmail.com'); // Recipient's email address and optionally a name to identify him
$mail->isHTML(true); // Set email to be sent as HTML, if you are planning on sending plain text email just set it to false
// The following is self explanatory
$mail->Subject = 'Client enquiry';
$mail->Body = $_POST["message"];
if(!$mail->send())
{
echo "Message hasn't been sent.";
echo 'Mailer Error: ' . $mail->ErrorInfo . "\n";
}
else
{
redirect("confirmation.html");
}
}
?>
Note that this is the controller (I'm using MCV).
This is part of a website I've been implementing as a project for a CS class. Therefore I would avoid buying a private domain.
UPDATE: I've modified the original post with how this piece of code should be. May anyone else encounter this issue, this snippet is working without any issue.