Mena
Mena

Reputation: 59

PHP mailer not working in Cpanel

my php mailer is working fine for last week but now not working and get this error message : SMTP -> ERROR: Failed to connect to server: Connection refused (111) i don't know what is wrong ,i didn't change anything . i'm test send email in outlook with my email account the result is fine. I am using PHPmailer Version: 2.0.4 Here my code :

<?php
$btnsubmit      =   $_REQUEST["btnSubmit"];



require_once('class.phpmailer.php');
$mail = new PHPMailer();

$mail->AddEmbeddedImage("images/img1.jpg", "img1", "img1.jpg");


$body             = file_get_contents("promotion.html");
$mail->IsSMTP(); 
$mail->Host       = "smtp.gmail.com"; 
$mail->SMTPDebug  = 1;                
$mail->SMTPAuth   = true; 
$mail->SMTPSecure = "tls";
$mail->Host       = "smtp.gmail.com"; 
$mail->Port       = 465;              
$mail->FromName   = "Administrator"; 
$mail->Username   = "[email protected]"; 
$mail->Password   = "*******";            
$mail->Subject    = "Promotions Tours to Beijing_4D3N_DEPART: 01-OCT-13";
$mail->IsHTML(true);
$mail->MsgHTML($body);



 if( isset($_POST['btnSubmit']))
 {
$mail->AddAddress("[email protected]", "msymarina99");
$mail->Send();  
echo("SENT COMPLETTED");

}


?>

Upvotes: 1

Views: 24798

Answers (4)

Ranush
Ranush

Reputation: 11

This is Working for me

$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2; 
$mail->Host = 'Outgoing Server (please add your cpanel of email server) ex: sothing.somthing.com does not required mail word before';
//$mail->Debugoutput = 'html';
$mail->Port       = 587;
$mail->SMTPSecure = 'tls';

$mail->SMTPAuth = true;
$mail->Username = 'your email ex: something.something.com'; 
$mail->Password = 'your password of before mentioned email';
$mail->setFrom('before mentioned email', "something");
$mail->addAddress('receive address (same server email address (cpanel))', "something");
$mail->Subject = 'something';
$mail->msgHTML('something text'); 
//$mail->msgHTML(file_get_contents('contents.html'), __DIR__); 
//Read an HTML message body from an external file, convert referenced images to embedded,
$mail->AltBody = 'HTML messaging not supported';
// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file
// echo "err";

if(!$mail->send()){
    //echo "Mailer Error: " . $mail->ErrorInfo;
   echo "Ok email send";
}else{
    echo "err";
}

Upvotes: 1

CPanel blocks access to external SMTP servers by default.

Disable this restriction in whm > security center > SMTP Restrictions disable

This works

<?php
require_once('./class.phpmailer.php');
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages         only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.mail.yahoo.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "xxxxxx";
$mail->SetFrom("[email protected]");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("[email protected]");

if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message has been sent";
}?>

Upvotes: 1

adiazm
adiazm

Reputation: 1

Some Cpanel block port 587 or 465 of gmail. you should try one of those 2 when using phpmailer.

Upvotes: 0

David Ingram
David Ingram

Reputation: 46

I just got it working for me I had:

$mail->Host       = "mail.drakecomfort.com"; 
$mail->SMTPSecure = "tls";
$mail->Port       = 587;  

changed it to

$mail->Host       = "smtp.gmail.com"; 
$mail->SMTPSecure = "ssl"; 
$mail->Port       = 465;  

final working code for me:

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                       // 1 = errors and messages
                                       // 2 = messages only
$mail->SMTPAuth   = "true";                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "******@drakecomfort.com";  // GMAIL username
$mail->Password   = "******";            // GMAIL password

$mail->SetFrom('*******@mycomputerstore.com', 'Debrief');

$mail->AddReplyTo("******@mycomputerstore.com","David Ingram");

$mail->Subject    = "$subject";

Upvotes: 3

Related Questions