1

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.

4
  • Have you connected to smtp on your server and verified the service is running and you can connect with that password?
    – David Paul
    Dec 25 '15 at 23:45
  • I'm sorry, I have a very limited experience and knowledge with SMTP. How could I do that? To add further information, I am running a WAMP virtual-host server.
    – Gabriel
    Dec 26 '15 at 0:32
  • You need to search for that message - there are many duplicates of this question. You should also read the docs that tell you how to deal with it.
    – Synchro
    Dec 26 '15 at 7:45
  • Thank you for the info ! I'll look into it asap.
    – Gabriel
    Dec 26 '15 at 15:58
2

I've contacted the Mailgun staff which provided me with excellent support. The problem lies with how I specify the port. It should be 465 for SSL and 587 for TLS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.