As many of you are aware, Microsoft is deprecating basic authentication (login and password) for Exchange online on 01/10/2022 onwards. You can read the full article over here:
https://www.microsoft.com/en-us/microsoft-365/blog/2022/09/01/microsoft-retires-basic-authentication-in-exchange-online/#:~:text=As%20previously%20announced%2C%20we%20are,users%20move%20to%20Modern%20Authentication
I am currently using PHPMailer to send e-mails via SMTP from an application.
Working example of the current code:
<?php
include "vendor/autoload.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "smtp.office365.com";
$mail->Port = "587";
$mail->SMTPAuth = true;
$mail->Username = "my_email#my-company.com";
$mail->Password = "my_password";
$mail->SMTPSecure = "tls";
$mail->From = "my_email#my-company.com";
$mail->FromName = "my_name";
$mail->AddAddress("my_email#my-company.com");
$mail->IsHTML(true);
$mail->Subject = "This is a test subject";
$mail->Body = "Hello, how are you?";
$mail->Send();
?>
I want to transfer from basic authentication to OAuth 2.0. After reading a lot of documentation, searching the web for 3 days, trying and trying, I am not able to get it working.
Example of my new code:
<?php
include "vendor/autoload.php";
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use Stevenmaguire\OAuth2\Client\Provider\Microsoft;
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "smtp.office365.com";
$mail->SMTPAuth = true;
$mail->AuthType = "XOAUTH2";
$mail->SMTPDebug = SMTP::DEBUG_LOWLEVEL;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$username = "my_email#my-company.com";
$clientId = "client_id_from_azure_app_registration";
$clientSecret = "client_secret_from_azure_app_registration";
$redirectURI = "my_redirect_uri";
$Token = "my_token";
$mail->refresh_token = $Token;
$provider = new Stevenmaguire\OAuth2\Client\Provider\Microsoft(
[
"clientId" => $clientId,
"clientSecret" => $clientSecret,
"redirectUri" => $redirectURI
]
);
$provider->urlAPI = "https://outlook.office365.com/.default";
$provider->scope = "Mail.Send";
$mail->setOAuth(
new OAuth(
[
"provider" => $provider,
"clientId" => $clientId,
"clientSecret" => $clientSecret,
"refreshToken" => $Token,
"userName" =>$username
]
)
);
$mail->From = $username;
$mail->AddAddress("my_email#my-company.com");
$mail->IsHTML(true);
$mail->Subject = "This is a test subject";
$mail->Body = "Hello, how are you?";
$mail->Send();
?>
I generate an access token via Postman and use this token in the code above:
The application is configured in Microsoft Azure with the required permissions:
This is the output I get:
2022-09-27 13:51:38 Connection: opening to smtp.office365.com:587, timeout=300, options=array()
2022-09-27 13:51:38 Connection: opened
2022-09-27 13:51:38 SMTP INBOUND: "220 AS4P191CA0011.outlook.office365.com Microsoft ESMTP MAIL Service ready at Tue, 27 Sep 2022 13:51:37 +0000"
2022-09-27 13:51:38 SERVER -> CLIENT: 220 AS4P191CA0011.outlook.office365.com Microsoft ESMTP MAIL Service ready at Tue, 27 Sep 2022 13:51:37 +0000
2022-09-27 13:51:38 CLIENT -> SERVER: EHLO
2022-09-27 13:51:38 SMTP INBOUND: "250-AS4P191CA0011.outlook.office365.com Hello [2a02:4780:8:2::25]"
2022-09-27 13:51:38 SMTP INBOUND: "250-SIZE 157286400"
2022-09-27 13:51:38 SMTP INBOUND: "250-PIPELINING"
2022-09-27 13:51:38 SMTP INBOUND: "250-DSN"
2022-09-27 13:51:38 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2022-09-27 13:51:38 SMTP INBOUND: "250-STARTTLS"
2022-09-27 13:51:38 SMTP INBOUND: "250-8BITMIME"
2022-09-27 13:51:38 SMTP INBOUND: "250-BINARYMIME"
2022-09-27 13:51:38 SMTP INBOUND: "250-CHUNKING"
2022-09-27 13:51:38 SMTP INBOUND: "250 SMTPUTF8"
2022-09-27 13:51:38 SERVER -> CLIENT: 250-AS4P191CA0011.outlook.office365.com Hello [2a02:4780:8:2::25]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-STARTTLS250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8
2022-09-27 13:51:38 CLIENT -> SERVER: STARTTLS
2022-09-27 13:51:38 SMTP INBOUND: "220 2.0.0 SMTP server ready"
2022-09-27 13:51:38 SERVER -> CLIENT: 220 2.0.0 SMTP server ready
2022-09-27 13:51:38 CLIENT -> SERVER: EHLO
2022-09-27 13:51:38 SMTP INBOUND: "250-AS4P191CA0011.outlook.office365.com Hello [2a02:4780:8:2::25]"
2022-09-27 13:51:38 SMTP INBOUND: "250-SIZE 157286400"
2022-09-27 13:51:38 SMTP INBOUND: "250-PIPELINING"
2022-09-27 13:51:38 SMTP INBOUND: "250-DSN"
2022-09-27 13:51:38 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2022-09-27 13:51:38 SMTP INBOUND: "250-AUTH LOGIN XOAUTH2"
2022-09-27 13:51:38 SMTP INBOUND: "250-8BITMIME"
2022-09-27 13:51:38 SMTP INBOUND: "250-BINARYMIME"
2022-09-27 13:51:38 SMTP INBOUND: "250-CHUNKING"
2022-09-27 13:51:38 SMTP INBOUND: "250 SMTPUTF8"
2022-09-27 13:51:38 SERVER -> CLIENT: 250-AS4P191CA0011.outlook.office365.com Hello [2a02:4780:8:2::25]250-SIZE 157286400250-PIPELINING250-DSN250-ENHANCEDSTATUSCODES250-AUTH LOGIN XOAUTH2250-8BITMIME250-BINARYMIME250-CHUNKING250 SMTPUTF8
2022-09-27 13:51:38 Auth method requested: XOAUTH2
2022-09-27 13:51:38 Auth methods available on the server: LOGIN,XOAUTH2
Fatal error: Uncaught League\OAuth2\Client\Provider\Exception\IdentityProviderException: Bad Request in /home/u760208683/vendor/stevenmaguire/oauth2-microsoft/src/Provider/Microsoft.php:79 Stack trace: #0 /home/u760208683/vendor/league/oauth2-client/src/Provider/AbstractProvider.php(628): Stevenmaguire\OAuth2\Client\Provider\Microsoft->checkResponse(Object(GuzzleHttp\Psr7\Response), Array) #1 /home/u760208683/vendor/league/oauth2-client/src/Provider/AbstractProvider.php(537): League\OAuth2\Client\Provider\AbstractProvider->getParsedResponse(Object(GuzzleHttp\Psr7\Request)) #2 /home/u760208683/vendor/phpmailer/phpmailer/src/OAuth.php(115): League\OAuth2\Client\Provider\AbstractProvider->getAccessToken(Object(League\OAuth2\Client\Grant\RefreshToken), Array) #3 /home/u760208683/vendor/phpmailer/phpmailer/src/OAuth.php(128): PHPMailer\PHPMailer\OAuth->getToken() #4 /home/u760208683/vendor/phpmailer/phpmailer/src/SMTP.php(598): PHPMailer\PHPMailer\OAuth->getOauth64() #5 /home/u760208683/vendor/phpmailer/phpmailer/src/PHPMailer in /home/u760208683/vendor/stevenmaguire/oauth2-microsoft/src/Provider/Microsoft.php on line 79
Can anyone point me in the right direction?
Many thanks in advance!
Kind regards,
Laurents
Fatal error: Uncaught League\OAuth2\Client\Provider\Exception\IdentityProviderException:
This error is due to the fact you are providing an access_token to send the mail instead of a refresh_token.
First of all, add the scope offline_access to get a refresh_token in your authorise url and save it somewhere in your app ( expiration is 24 hour or 90 days depends on type of app)
once you have the refresh token you can send as many mails as you want with the same token.
you can get new one by providing another non expired refresh token in the url and change grant_type to refresh_token and add the key refresh_token.
Related
I have tried to connect using SMTP, but failed. Here is my code.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 20;
$mail->isSMTP();
$mail->Host = 'smtp.mail.yahoo.com';
$mail->SMTPAuth = true;
$mail->AuthType = 'LOGIN';
$mail->Username = 'david_thedragonzzz#yahoo.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('david_thedragonzzz#yahoo.com', 'Mailer');
$mail->addAddress('david_thedragonzzz#yahoo.com', 'User');
$mail->addReplyTo('test#yahoo.com', 'Information');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
The result
2018-02-07 10:45:43 Connection: opening to ssl://smtp.mail.yahoo.com:465, timeout=300, options=array()
2018-02-07 10:45:43 Connection: opened
2018-02-07 10:45:43 SMTP INBOUND: "220 smtp.mail.yahoo.com ESMTP ready"
2018-02-07 10:45:43 SERVER -> CLIENT: 220 smtp.mail.yahoo.com ESMTP ready
2018-02-07 10:45:43 CLIENT -> SERVER: EHLO localhost
2018-02-07 10:45:43 SMTP INBOUND: "250-smtp.mail.yahoo.com"
2018-02-07 10:45:43 SMTP INBOUND: "250-PIPELINING"
2018-02-07 10:45:43 SMTP INBOUND: "250-SIZE 41697280"
2018-02-07 10:45:43 SMTP INBOUND: "250-8 BITMIME"
2018-02-07 10:45:43 SMTP INBOUND: "250 AUTH PLAIN LOGIN XOAUTH2 XYMCOOKIE OAUTHBEARER"
2018-02-07 10:45:43 SERVER -> CLIENT: 250-smtp.mail.yahoo.com250-PIPELINING250-SIZE 41697280250-8 BITMIME250 AUTH PLAIN LOGIN XOAUTH2 XYMCOOKIE OAUTHBEARER
2018-02-07 10:45:43 Auth method requested: LOGIN
2018-02-07 10:45:43 Auth methods available on the server: PLAIN,LOGIN,XOAUTH2,XYMCOOKIE,OAUTHBEARER
2018-02-07 10:45:43 CLIENT -> SERVER: AUTH LOGIN
2018-02-07 10:45:43 SMTP INBOUND: "334 VXNlcm5hbWU6"
2018-02-07 10:45:43 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2018-02-07 10:45:43 CLIENT -> SERVER: xxx==
2018-02-07 10:45:43 SMTP INBOUND: "334 UGFzc3dvcmQ6"
2018-02-07 10:45:43 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2018-02-07 10:45:43 CLIENT -> SERVER: xxx==
2018-02-07 10:45:46 SMTP INBOUND: "535 5.7.0 (#MBR1212) Incorrect username or password."
2018-02-07 10:45:46 SERVER -> CLIENT: 535 5.7.0 (#MBR1212) Incorrect username or password.
2018-02-07 10:45:46 SMTP ERROR: Password command failed: 535 5.7.0 (#MBR1212) Incorrect username or password.
SMTP Error: Could not authenticate.
2018-02-07 10:45:46 CLIENT -> SERVER: QUIT
2018-02-07 10:45:46 SMTP INBOUND: "221 2.0.0 Bye"
2018-02-07 10:45:46 SERVER -> CLIENT: 221 2.0.0 Bye
2018-02-07 10:45:46 Connection: closed
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
I can't understand how come this code gives me "incorrect username or password" error. I have configured smtp using Mozilla Thunderbird. Username and Password is right, but still gives me this kind of error.
I am having troubles with PHPMailer. on index.php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/PHPMailer/src/Exception.php';
require __DIR__ . '/PHPMailer/src/PHPMailer.php';
require __DIR__ . '/PHPMailer/src/SMTP.php'
$mail = new PHPMailer;
require __DIR__ . '/../controllers/login.php';
And login. php
$app->post('/proyectos/login', function ($request) use($servername, $dbname, $dbuser, $dbpassword, $mail)
{
$objTmp = $request->getBody();
$obj = array();
parse_str($objTmp, $obj);
$response = array();
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'user#gmail.com';
$mail->Password = 'pass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('oneemail#gmail.com', 'sender');
$mail->addAddress('someemail#gmail.com', 'receiver');
$mail->addReplyTo('anotheremail#gmail.com', 'Information');
$mail->isHTML(true);
$mail->Subject = 'Just a subject';
$mail->Body = "Just a body";
if(!$mail->send()) {
$response['success'] = false;
$response['message'] = "Could not send email.";
echo json_encode($response);
return;
}
$response['success'] = true;
echo json_encode($response);
return;
2017-12-01 19:54:33 Connection: opening to smtp.gmail.com:587, timeout=300, options=array()
2017-12-01 19:54:34 Connection: opened
2017-12-01 19:54:34 SMTP INBOUND: "220 smtp.gmail.com ESMTP g37sm1362021uah.16 - gsmtp"
2017-12-01 19:54:34 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP g37sm1362021uah.16 - gsmtp
2017-12-01 19:54:34 CLIENT -> SERVER: EHLO localhost
2017-12-01 19:54:34 SMTP INBOUND: "250-smtp.gmail.com at your service, [190.94.198.150]"
2017-12-01 19:54:34 SMTP INBOUND: "250-SIZE 35882577"
2017-12-01 19:54:34 SMTP INBOUND: "250-8BITMIME"
2017-12-01 19:54:34 SMTP INBOUND: "250-STARTTLS"
2017-12-01 19:54:34 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2017-12-01 19:54:34 SMTP INBOUND: "250-PIPELINING"
2017-12-01 19:54:34 SMTP INBOUND: "250-CHUNKING"
2017-12-01 19:54:34 SMTP INBOUND: "250 SMTPUTF8"
2017-12-01 19:54:34 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [190.94.198.150]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2017-12-01 19:54:34 CLIENT -> SERVER: STARTTLS
2017-12-01 19:54:34 SMTP INBOUND: "220 2.0.0 Ready to start TLS"
2017-12-01 19:54:34 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2017-12-01 19:54:34 Connection failed. Error #2: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed [C:\xampp\htdocs\proyectos\public\PHPMailer\src\SMTP.php line 404]
SMTP Error: Could not connect to SMTP host.
2017-12-01 19:54:34 CLIENT -> SERVER: QUIT
2017-12-01 19:54:34
2017-12-01 19:54:34
2017-12-01 19:54:34
2017-12-01 19:54:34 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
{"success":false,"message":"Could not send email."}
I am using phpmailer latest version . I am using the following code :
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // 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 = 'sultan.ahmed.sagor#gmail.com'; // SMTP username
$mail->Password = 'simplePass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SMTPDebug=2;
$mail->setFrom('sultan.ahmed.sagor#gmail.com', 'Mailer');
$mail->addAddress('zakir#era.com.bd', 'Joe User'); // Add a recipient
//$mail->addAddress('ellen#example.com'); // Name is optional
//$mail->addReplyTo('info#example.com', 'Information');
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
But when I run this code I am giving the following output :
2017-02-09 11:20:34 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP w70sm17914821wrc.47 - gsmtp
2017-02-09 11:20:34 CLIENT -> SERVER: EHLO localhost
2017-02-09 11:20:34 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [50.118.143.87] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHU*KING 250 SMTPUTF8
2017-02-09 11:20:34 CLIENT -> SERVER: STARTTLS
2017-02-09 11:20:37 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2017-02-09 11:20:38 CLIENT -> SERVER: EHLO localhost
2017-02-09 11:20:39 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [50.118.143.87] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
2017-02-09 11:20:39 CLIENT -> SERVER: AUTH LOGIN
2017-02-09 11:20:39 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2017-02-09 11:20:39 CLIENT -> SERVER: xxx==
2017-02-09 11:20:40 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2017-02-09 11:20:40 CLIENT -> SERVER: xxx==
2017-02-09 11:20:40 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials w70sm17914821wrc.47 - gsmtp
2017-02-09 11:20:40 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials w70sm17914821wrc.47 - gsmtp
2017-02-09 11:20:40 SMTP Error: Could not authenticate.
2017-02-09 11:20:40 CLIENT -> SERVER: QUIT
2017-02-09 11:20:41 SERVER -> CLIENT: 221 2.0.0 closing connection w70sm17914821wrc.47 - gsmtp
2017-02-09 11:20:41 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
And the email is not sent . How can I use phpmailer to send email ?
This should fix your issue:
$mail->AuthType = 'LOGIN';
https://www.emailarchitect.net/easendmail/sdk/html/authtype.htm
I'm using digital ocean.
I've been digging to fix this error.
2016-10-26 04:35:35 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP ht5sm340860pad.16 - gsmtp
2016-10-26 04:35:35 CLIENT -> SERVER: EHLO example.com 2016-10-26 04:35:35 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [128.199.201.169] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
2016-10-26 04:35:35 CLIENT -> SERVER: STARTTLS
2016-10-26 04:35:35 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2016-10-26 04:35:35 SMTP Error: Could not connect to SMTP host.
2016-10-26 04:35:35 CLIENT -> SERVER: QUIT
2016-10-26 04:35:36 SERVER -> CLIENT:
2016-10-26 04:35:36 SMTP ERROR: QUIT command failed:
2016-10-26 04:35:36 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
There was an error! Try again
Here is my setting:
require 'mailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2;
$mail->isSMTP();
// Specify main and backup SMTP servers
$mail->Host = gethostbyname('smtp.gmail.com');
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mail#gmail.com'; // SMTP username
$mail->Password = 'psw'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('d#gmail.com', 'd');
$mail->addAddress('test#gmail.com', ''); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = " Account Confirmation";
$mail->Body = "Hello. test Thank you for you using our service! To activate your account, please access this link:\n <a href='http://w2heaven.com/index.php?confirmcode=$confirmcode'>Click here</a> \n";
if(!$mail->send()) {
echo "There was an error! Try again";
} else {
echo "Please confirm your email at: blablah";
}
More info: It works without gethostbyname(); but extremely slow because DigitalOcean blocks IPv6
I'm trying to send email using PHPMailer. When I tried sending the email from gmail account, it works perfectly fine. However, I would like to send the email from my own SMTP, but there seems to be an error. What could be the problem? Please help me. Thank you.
Using smtp.gmail.com :
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
Using mail.distech.com.my :
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'mail.distech.com.my';
$mail->SMTPAuth = true;
$mail->Username = 'myemail#distech.com.my';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = 'myemail#distech.com.my';
$mail->addAddress('amalina#distech.com.my');
$mail->Subject = 'Test';
$mail->Body = 'Test message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Error using mail.distech.com.my
2015-06-23 06:58:45 Connection: opening to mail.distech.com.my:587, timeout=300, options=array ( )
2015-06-23 06:58:45 Connection: opened
2015-06-23 06:58:45 SERVER -> CLIENT: 220-server.firstonline-server16.com ESMTP Exim 4.85 #2 Tue, 23 Jun 2015 14:58:45 +0800 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2015-06-23 06:58:45 CLIENT -> SERVER: EHLO 192.168.1.20
2015-06-23 06:58:46 SERVER -> CLIENT: 250-server.firstonline-server16.com Hello 192.168.1.20 [115.135.120.220] 250-SIZE 52428800 250-8BITMIME 250-PIPELINING 250-AUTH PLAIN LOGIN 250-STARTTLS 250 HELP
2015-06-23 06:58:46 CLIENT -> SERVER: STARTTLS
2015-06-23 06:58:46 SERVER -> CLIENT: 220 TLS go ahead
Warning: stream_socket_enable_crypto() [function.stream-socket-enable-crypto]: SSL: The operation completed successfully. in C:\xampp\htdocs\ehars\phpmailer\class.smtp.php on line 344
2015-06-23 06:58:46 SMTP Error: Could not connect to SMTP host.
2015-06-23 06:58:46 CLIENT -> SERVER: QUIT
Notice: fwrite() [function.fwrite]: send of 6 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host. in C:\xampp\htdocs\ehars\phpmailer\class.smtp.php on line 937
2015-06-23 06:58:46 SERVER -> CLIENT:
2015-06-23 06:58:46 SMTP ERROR: QUIT command failed:
2015-06-23 06:58:46 Connection: closed 2015-06-23 06:58:46 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
UPDATE :
Checked my phpinfo () and the ssl has been enabled.