Search code examples
phpemailphpmailer

PHPMailer SMTP connection error


I've been trying to figure out how to get phpMailer to work for the last few hours. I keep getting errors. Here is my code:

<?php
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

require("class.phpmailer.php");
$mail = new PHPMailer(); 
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "******@gmail.com"; // SMTP username
$mail->Password = "******"; // SMTP password
$host = "smtp.mandrillapp.com";
$port = 587;
$mail->SMTPSecure = 'ssl';  
$mail->SMTPDebug = 1;
$webmaster_email = "****@gmail.com"; //Reply to this email ID
$email= $email; // Recipients email ID
$name= $name; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "University of Life Experiences";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "ULE";
$mail->Body = $message; //HTML Body
$mail->AltBody = $message; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

Here is the error I have been receiving:

SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed. Mailer Error: SMTP connect() failed.

I have tried many different SMTP servers including Gmail and I always get a similar error about failing to connect.

Please help, I have tried many other phpMailer example codes all with the same issue. If anyone can recommend any other PHP mailing programs that would be useful. The reason I am not using the inbuilt mail() function.


Solution

  • You didn't set the SMTP Host ( though you declare the variable $host ). Set it via:

    $mail->host = $host;
    

    Thanks @Rikesh for reminding, $port is also the same case.

    Side Note: I noticed you use gmail.com as reply email but your SMTP server is not gmail. This may cause some email servers to put your email to spam / junk folder.