PHPMailer closes connection - php

I'm trying to send an e-mail with PHPMailer to my SMTP server hosted with Mail-In-A-Box (which uses Postfix) on another Ubuntu 16.04 VPS, but I have an error
The mail vps is hosted on Digital Ocean, same for the website/php one
This is what I get when I run the code:
2019-02-26 17:54:01 CLIENT -> SERVER: EHLO mail
2019-02-26 17:54:01 SERVER -> CLIENT: 250-mail.ultracore.it
250-PIPELINING
250-SIZE 134217728
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-DSN
250 SMTPUTF8
2019-02-26 17:54:01 CLIENT -> SERVER: STARTTLS
2019-02-26 17:54:01 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2019-02-26 17:54:01 SMTP Error: Could not connect to SMTP host.
2019-02-26 17:54:01 CLIENT -> SERVER: QUIT
2019-02-26 17:54:01 SERVER -> CLIENT:
2019-02-26 17:54:01 SMTP ERROR: QUIT command failed:
2019-02-26 17:54:01 SMTP Error: Could not connect to SMTP host.
This is the code I use to send the email:
function sendEmail($from, $password, $email, $subject, $messageHtml, $message)
{
$mail = new PHPMailer(true);
//$pop = POP3::popBeforeSmtp('pop3.ultracore.it', 110, 1, $from, $password, 2);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.ultracore.it'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $from; // SMTP username
$mail->Password = $password; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom($from, $from);
$mail->addAddress($email, 'Joe User'); // Add a recipient
$mail->addReplyTo('support#ultracore.it', 'Support');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $messageHtml;
$mail->AltBody = $message;
$mail->send();
echo 'The message has been sent';
} catch (Exception $e) {
echo $mail->ErrorInfo;
}
}
It's the PHPMailer's example modified.
As you can see I tried using the popBeforeSMTP function, but I had the same error, and another one before it:
<pre>Connecting to the POP3 server raised a PHP warning:errno: 2 errstr: fsockopen(): unable to connect to pop3.ultracore.it:110 (Connection refused); errfile: /var/www/html/vendor/phpmailer/phpmailer/src/POP3.php; errline: 238</pre><pre>Connecting to the POP3 server raised a PHP warning:errno: 2 errstr: fsockopen(): unable to connect to pop3.ultracore.it:110 (Connection refused); errfile: /var/www/html/vendor/phpmailer/phpmailer/src/POP3.php; errline: 238Failed to connect to server pop3.ultracore.it on port 110. errno: 111; errstr: Connection refused</pre>
I tried with ssl instead of tls, but I'm receiving this:
2019-02-26 18:03:52 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshootingr`
I know some other user have asked a question like this, but none of the solutions I've tried worked for me.
I've imported PHPMailer through the composer with this line
"require": {
"phpmailer/phpmailer": "~6.0"
}
Update
I changed the code to this (still coming from the PHPMailer examples)
function sendEmail($from, $password, $to, $subject, $messageHtml)
{
$smtp_settings = array(
"debug" => 2,
"host" => "smtp.ultracore.it",
"port" => 587,
"auth" => true,
"encryption" => "tls",
"reply_address" => "support#ultracore.it",
"peer_name" => "smtp.ultracore.it",
"verify_peer" => true,
"verify_depth" => 3,
"allow_self_signed" => false,
"cafile" => "/var/www/html/certificates/ssl_private_key.pem"
);
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = $smtp_settings['debug'];
//Set the hostname of the mail server
$mail->Host = $smtp_settings['host'];
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = $smtp_settings['port'];
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = $smtp_settings['encryption'];
//Custom connection options
//Note that these settings are INSECURE
$mail->SMTPOptions = array(
'ssl' => [
'verify_peer' => $smtp_settings['verify_peer'],
'verify_depth' => $smtp_settings['verify_depth'],
'allow_self_signed' => $smtp_settings['allow_self_signed'],
'peer_name' => $smtp_settings['peer_name'],
'cafile' => $smtp_settings['cafile'],
],
);
//Whether to use SMTP authentication
$mail->SMTPAuth = $smtp_settings['auth'];
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $from;
//Password to use for SMTP authentication
$mail->Password = $password;
//Set who the message is to be sent from
$mail->setFrom($from, 'First Last');
//Set who the message is to be sent to
$mail->addAddress($to, 'John Doe');
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($messageHtml, __DIR__);
//Send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
}
But I have the same error
2019-02-26 21:48:23 SERVER -> CLIENT: 220 mail.ultracore.it ESMTP Hi, I'm a Mail-in-a-Box (Ubuntu/Postfix; see https://mailinabox.email/)
2019-02-26 21:48:23 CLIENT -> SERVER: EHLO ultracore.it
2019-02-26 21:48:23 SERVER -> CLIENT: 250-mail.ultracore.it250-PIPELINING250-SIZE 134217728250-VRFY250-ETRN250-STARTTLS250-ENHANCEDSTATUSCODES250-8BITMIME250-DSN250 SMTPUTF8
2019-02-26 21:48:23 CLIENT -> SERVER: STARTTLS
2019-02-26 21:48:23 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2019-02-26 21:48:23 CLIENT -> SERVER: QUIT
2019-02-26 21:48:23 SERVER -> CLIENT:
2019-02-26 21:48:23 SMTP ERROR: QUIT command failed:
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I tried switching the cafile to all of this files:
files I tried
Am I doing this wrong? The files are on the website's VPS, I've applied the certificates to the server through Mail-In-A-Box's admin panel.
I have CloudFlare, but the address smtp.ultracore.it is not under cloudflare's protection, and all the certificates are created on the Mail VPS' address, so ultracore.it is created with the address of mail.ultracore.it, not the website's one...
The code
echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."<br>";
returns this
SSL loaded

You're using SMTPSecure = 'ssl' (implicit TLS) on port 587, which expects explicit TLS (STARTTLS). This is covered in all the examples and the docs.
When STARTTLS fails, it's most likely due to an invalid certificate, or an invalid CA certificate, and again, that is covered in the troubleshooting guide.
Don't use POP-before-SMTP; that's a really ancient auth mechanism and should not be used.

I fixed setting "verify_peer" to false, so my code looks like this now:
function sendEmail($from, $password, $to, $subject, $messageHtml)
{
$smtp_settings = array(
"debug" => 2,
"host" => "mail.ultracore.it",
"port" => 587,
"auth" => true,
"encryption" => "tls",
"reply_address" => "support#ultracore.it",
"peer_name" => "mail.ultracore.it",
"verify_peer" => false,
"verify_depth" => 3,
"allow_self_signed" => true,
"cafile" => "/var/www/html/certificates/ssl_certificate.pem"
);
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = $smtp_settings['debug'];
//Set the hostname of the mail server
$mail->Host = $smtp_settings['host'];
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = $smtp_settings['port'];
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = $smtp_settings['encryption'];
//Custom connection options
//Note that these settings are INSECURE
$mail->SMTPOptions = array(
'ssl' => [
'verify_peer' => $smtp_settings['verify_peer'],
'verify_depth' => $smtp_settings['verify_depth'],
'allow_self_signed' => $smtp_settings['allow_self_signed'],
'peer_name' => $smtp_settings['peer_name'],
'cafile' => $smtp_settings['cafile'],
],
);
//Whether to use SMTP authentication
$mail->SMTPAuth = $smtp_settings['auth'];
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $from;
//Password to use for SMTP authentication
$mail->Password = $password;
//Set who the message is to be sent from
$mail->setFrom($from, 'First Last');
//Set who the message is to be sent to
$mail->addAddress($to, 'John Doe');
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($messageHtml, __DIR__);
//Send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
}

Related

SMTP Error could not authenticate and SMTP Error failed to connect to server(0)

I'm getting this SMTP error when using the newest PHPMailer release:
SMTP Error: Could not authenticate.
I've tried using SSL instead of TLS with port number 465. OpenSSL is already loaded in the config file, and I've looked through the troubleshooting guide none of this has made a difference, what exactly is wrong with my code? One thing I'm not sure of is including the OAuth2 provider class with this :
use League\OAuth2\Client\Provider\Google;
There's no file path for that, should I change this? Also, when I change encryption to $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; and now I get this : SMTP ERROR: Failed to connect to server: (0).
Here's the code:
class gmail_xoauth
{
public static function sendMail($subject,$body,$address)
{
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_CLIENT;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';
//Fill in authentication details here
//Either the gmail account owner, or the user that gave consent
$email = 'myemail#gmail.com';
$clientId = 'clientid';
$clientSecret = 'clientSecret';
//Obtained by configuring and running get_oauth_token.php
//after setting up an app in Google Developer Console.
$refreshToken = 'refreshToken';
//Create a new OAuth2 provider instance
$provider = new Google(
[
'clientId' => $clientId,
'clientSecret' => $clientSecret,
]
);
//Pass the OAuth provider instance to PHPMailer
$mail->setOAuth(
new OAuth(
[
'provider' => $provider,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'refreshToken' => $refreshToken,
'username' => $email,
]
)
);
//Set who the message is to be sent from
//For gmail, this generally needs to be the same as the user you logged in as
$mail->setFrom($email, 'me');
//Set who the message is to be sent to
$mail->addAddress($address, 'John Doe');
//Set the subject line
$mail->Subject = $subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->msgHTML(file_get_contents('PHPMailer/PHPMailer-master/examples/contentsutf8.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('PHPMailer/PHPMailer-master/examples/images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: '. $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
}
}
This is the full error
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP w67sm7347917yww.16 - gsmtp
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [108.203.6.59]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [108.203.6.59]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection w67sm7347917yww.16 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/TroubleshootingEmail sent!

using phpmailer with plesk smtp

I'm trying to setup PHP mailer on my VPS with Plesk SMTP.
The PHP code
require("path_to_phpmailer");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
//Server settings
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->CharSet = 'UTF-8';
$mail->Host = 'localhost';
$mail->Port = 25;
$mail->SMTPSecure = 'tls';
$mail->SMTPOptions = array (
'ssl' => array(
'verify_peer' => true,
'verify_depth' => 3,
'allow_self_signed' => true,
'peer_name' => 'Plesk',
)
);
$mail->SMTPAuth = true;
$mail->Username = 'support#mywebsite.com';
$mail->Password = 'somepassword';
$mail->setFrom('support#mywebsite.com', 'Name Surname');
$mail->addAddress('someadress#web.de', 'Name Surname');
$mail->Subject = 'Email subject';
$mail->msgHTML('Email content with <strong>html</strong>');
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
support#mywebsite.com is the email that i created in my ples dashboard and somepassword is its password. However running the script gives the following output:
2018-04-14 09:27:10 SERVER -> CLIENT: 220 myserver.net ESMTP
Postfix (Ubuntu)
2018-04-14 09:27:10 CLIENT -> SERVER: EHLO mywebsite.com
2018-04-14 09:27:10 SERVER -> CLIENT: 250-myserver.net250-
PIPELINING250-SIZE 10240000250-ETRN250-STARTTLS250-AUTH DIGEST-MD5 CRAM-MD5
PLAIN LOGIN250-XFORWARD NAME ADDR PROTO HELO SOURCE PORT IDENT250-
ENHANCEDSTATUSCODES250-8BITMIME250 DSN
2018-04-14 09:27:10 CLIENT -> SERVER: STARTTLS
2018-04-14 09:27:10 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2018-04-14 09:27:10 CLIENT -> SERVER: QUIT
2018-04-14 09:27:10 SERVER -> CLIENT:
2018-04-14 09:27:10 SMTP ERROR: QUIT command failed:
SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/TroubleshootingMessage has been
sent
Did I understand something wrong?
Thank you in advance!
Check if is a problem with ssl, and try again
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
if it still has an error
$mail->SMTPDebug = 3;
https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging

SMTP Error: Could not connect to SMTP host. PHPMailer. PHP [duplicate]

This question already has answers here:
PHPMailer - SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
(4 answers)
Closed 3 years ago.
PHPMailer is not working and throwing following error.
2017-12-04 13:34:14 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP y19sm20173980pgv.19 - gsmtp
2017-12-04 13:34:14 CLIENT -> SERVER: EHLO solutions.proprompt.com
2017-12-04 13:34:14 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [103.58.144.11]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-12-04 13:34:14 CLIENT -> SERVER: STARTTLS
2017-12-04 13:34:14 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2017-12-04 13:34:14 CLIENT -> SERVER: QUIT
2017-12-04 13:34:15
2017-12-04 13:34:15
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.
It was working properly before. I have used following lines of code:
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'some#email.com'; // SMTP username
$mail->Password = 'somepassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
Can anyone tell me where it went wrong?
After hours of struggling , I found your answer to be correct,
Those who are facing issues with PHPMailer and godaddy linux hosting server have to some simple changes ,
host = 'localhost'
and add the below lines
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
should work
Following lines of code solved this issue.
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
Followed code worked for me:
$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('MAIL_USERNAME', 'Test');//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();

xampp + PHPMailer + Gmail = SMTP Error: Could not connect to SMTP host

I have installed PHPMailer using Composer and added all the Composer dependency to use Google XOAuth2 authentication.
Then I have followed some tutorials to get the refresh token with the Gmail API.
Everything should be working just fine. I did all the paper work right. Right?!
Despite my best intention and all the docs, I'm not able to establish a SMTP connection to smtp.gmail.com
I get this error:
2017-10-20 18:01:45 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP c17sm2715728wrg.26 - gsmtp
2017-10-20 18:01:45 CLIENT -> SERVER: EHLO localhost
2017-10-20 18:01:45 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [151.61.40.58]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-10-20 18:01:45 CLIENT -> SERVER: STARTTLS
2017-10-20 18:01:45 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2017-10-20 18:01:45 CLIENT -> SERVER: QUIT
2017-10-20 18:01:45
2017-10-20 18:01:45
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
My code is straight from the PHPMailer Gmail XOAUTH example.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use League\OAuth2\Client\Provider\Google;
date_default_timezone_set('Europe/Rome');
require 'lib/php/vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';
$email = 'seckrit-stuff';
$clientId = 'seckrit-stuff';
$clientSecret = 'seckrit-stuff';
$refreshToken = 'seckrit-stuff';
$provider = new Google(
[
'clientId' => $clientId,
'clientSecret' => $clientSecret,
]
);
$mail->setOAuth(
new OAuth(
[
'provider' => $provider,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'refreshToken' => $refreshToken,
'userName' => $email,
]
)
);
$mail->setFrom($email, 'Matteo Bini');
$mail->addAddress('seckrit-stuff', 'Matteo Bini');
$mail->Subject = 'PHPMailer GMail XOAUTH2 SMTP test';
$mail->CharSet = 'utf-8';
$mail->msgHTML('<strong>HTML</strong> message!');
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Could you please help me finding a way to use PHPMailer with Gmail in localhost (xampp)?
This is nothing to do with OAuth. You’ve got a much earlier issue at the TLS level. Either you’re missing the openssl extension, it’s misconfigured, or your CA certificates are out of date.
This issue is covered in the troubleshooting guide the error links to - check using openssl.

PHPMailer SMTP SSL Error

Below is my code
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "test#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('test#company.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('test#yahoo.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('test#yahoo.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML('test');
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('phpmailer.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
but it displays this error:
SERVER -> CLIENT: 220 mx.google.com ESMTP oo3sm681775pdb.26 - gsmtp
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-mx.google.com at your service, [124.13.146.153]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
( ! ) Warning: stream_socket_enable_crypto(): this stream does not support
SSL/crypto in C:\wamp\www\phpmailer\class.smtp.php on line 344
Call Stack
# Time Memory Function Location
1 0.0007 253800 {main}( ) ..\ibdex1.php:0
2 0.0061 944248 PHPMailer->send( ) ..\ibdex1.php:52
3 0.0069 962000 PHPMailer->postSend( ) ..\class.phpmailer.php:970
4 0.0069 962368 PHPMailer->smtpSend( ) ..\class.phpmailer.php:1062
5 0.0069 963320 PHPMailer->smtpConnect( ) ..\class.phpmailer.php:1218
6 0.8391 1164280 SMTP->startTLS( ) ..\class.phpmailer.php:1343
7 1.0490 1164576 stream_socket_enable_crypto ( ) ..\class.smtp.php:344
CLIENT -> SERVER: QUIT
SERVER -> CLIENT:
SMTP ERROR: QUIT command failed:
SMTP connect() failed.
Mailer Error: SMTP connect() failed.
You are missing the openssl extension. This is covered in the troubleshooting docs.
Enable access to that account Unlock account
Google disables it by default.

Categories