PHPMailer: SMTP Error: Could not connect to SMTP Host #
I’ve used PHPMailer on several projects but now I’m stuck. It gives me the error:
SMTP Error: Could not connect to SMTP host.
I’ve tried sending email from Thunderbird and it works ! But not through PHPMailer … Here are the settings from Thunderbird:
Server name: mail.exampleserver.com
Port: 587
Username: user@exampleserver.com
Secure Authentication: No
Connection Security: STARTTLS
I’ve compared these with the server at my last project where I used PHPMailer and they were:
Server name: mail.exampleserver2.com
Port: 465
Username: user@exampleserver2.com
Secure Authentication: No
Connection Security: SSL/TLS
My php code is:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = SMTP_HOST; // SMTP servers
$mail->Port = SMTP_PORT; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = SMTP_USER; // SMTP username
$mail->Password = SMTP_PASSWORD; // SMTP password
$mail->From = MAIL_SYSTEM;
$mail->FromName = MAIL_SYSTEM_NAME;
$mail->AddAddress($aSecuredGetRequest[’email’]);
$mail->IsHTML(true); // send as HTML
Where I am wrong?
Here is Some Solutions :
Solution 1: #
$mail = new PHPMailer(true);
$mail->isSMTP();// Set mailer to use SMTP
$mail->CharSet = “utf-8”;// set charset to utf8
$mail->SMTPAuth = true;// Enable SMTP authentication
$mail->SMTPSecure = ‘tls’;// Enable TLS encryption, `ssl` also accepted
$mail->Host = ‘smtp.gmail.com’;// Specify main and backup SMTP servers
$mail->Port = 587;// TCP port to connect to
$mail->SMTPOptions = array(
‘ssl’ => array(
‘verify_peer’ => false,
‘verify_peer_name’ => false,
‘allow_self_signed’ => true
)
);
$mail->isHTML(true);// Set email format to HTML
$mail->Username = ‘Sender Email’;// SMTP username
$mail->Password = ‘Sender Email Password’;// SMTP password
$mail->setFrom(‘example@mail.com’, ‘John Smith’);//Your application NAME and EMAIL
$mail->Subject = ‘Test’;//Message subject
$mail->MsgHTML(‘HTML code’);// Message body
$mail->addAddress(‘User Email’, ‘User Name’);// Target email
$mail->send();
Solution 2: #
$mail->SMTPSecure = “ssl”;
$mail->Host=’smtp.gmail.com’;
$mail->Port=’465′;
$mail->Username = ‘you@gmail.com’; // SMTP account username
$mail->Password = ‘your gmail password’;
$mail->SMTPKeepAlive = true;
$mail->Mailer = “smtp”;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->CharSet = ‘utf-8’;
$mail->SMTPDebug = 0;
Solution 3: #
$mail->SMTPOptions = array(
‘ssl’ => array(
‘verify_peer’ => false,
‘verify_peer_name’ => false,
‘allow_self_signed’ => true
)
);
Solution 4: #
// if you’re using SSL
$mail->SMTPSecure = ‘ssl’;
// OR use TLS
$mail->SMTPSecure = ‘tls’;