phpmailer with exchange and no SSL or TLS - php

I've been working on this problem for the past 2 days and everything that i tried is still not working. I'm trying to authenticate to an exchange server with php so i can send to outside domains.
If i disable authentication, i can send internally without any issues.
Here's the error
SMTP -> FROM SERVER:220 N-CAS1-13.company.domain Microsoft ESMTP MAIL Service ready at Sat, 20 Feb 2016 16:16:05 -0500
SMTP -> FROM SERVER: 250-N-CAS1-13.company.domain Hello [10.10.10.31] 250-SIZE 37748736 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-X- ANONYMOUSTLS 250-AUTH NTLM 250-X-EXPS GSSAPI NTLM 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 XRDST
SMTP -> ERROR: AUTH not accepted from server: 504 5.7.4 Unrecognized authentication type
SMTP -> FROM SERVER:250 2.0.0 Resetting
SMTP Error: Could not authenticate.
Here's the phpmailer code
require "phpmailer/class.phpmailer.php"; //include phpmailer class
//require "PHPMailer-5.2.14/PHPMailer-5.2.14/PHPMailerAutoload.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPDebug = 2;
$mail->SMTPSecure = false;
$mail->Host = "10.10.10.38";
$mail->Port = 25;
$mail->Username = "username";
$mail->Password = "cGhpbGltaWVzMjM2OTY5";
$mail->From = "name#companydomain.com";
$mail->FromName = "name";
$mail->SetFrom("name#companydomain.com", "name");
$mail->Subject = $_POST['company'].": (Time Sensitive)";
$mail->AddEmbeddedImage('trans2.png', 'logo', 'trans2.png ');
$mail->AddAttachment("docs/install.xlsx");
$mail->MsgHTML($message1.$message2.$message3);
// Send To
$mail->AddAddress($_POST['emailid'], ""); // Where to send it - Recipient
$mail->AddCC("person#companydomain.com");
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
I tried different version of phpmailer, i tried encoding the password with base64, i tried just the username, i tried the email as the username...Still nothing
The exchange server is on port 25 and it does not require SSL or TLS.

SMTP -> FROM SERVER: ... 250-AUTH NTLM
The server is only expecting NTLM authentication, not LOGIN, PLAIN or DIGEST-MD5 which could be used with only username and password. You would also need the NTLM realm and your workstation.
Unfortunately there is not much documentation about it but you might have a look at http://www.phpclasses.org/browse/file/31.html and look for NTLM to get the idea how to do this.

Related

Sending SMTP email via PHPMailer started failing suddenly

I have been using PHPMailer to send SMTP email on behalf of my office365 account, and it was working for about a week. Then it suddenly quit working and I don't know what changed.
When I enable high debug logging in PHPMailer I see this:
SMTP -> FROM SERVER:220 CY4PR15CA0011.outlook.office365.com Microsoft
ESMTP MAIL Service ready at Thu, 10 Jan 2019 13:30:20 +0000 SMTP ->
FROM SERVER: 250-CY4PR15CA0011.outlook.office365.com Hello
[198.154.243.158] 250-SIZE 157286400 250-PIPELINING 250-DSN
250-ENHANCEDSTATUSCODES 250-STARTTLS 250-8BITMIME 250-BINARYMIME
250-CHUNKING 250 SMTPUTF8 SMTP -> ERROR: AUTH not accepted from
server: 504 5.7.4 Unrecognized authentication type
[CY4PR15CA0011.namprd15.prod.outlook.com] SMTP -> FROM SERVER:250
2.0.0 Resetting
This piece seems to be the most relevant:
AUTH not accepted from server: 504 5.7.4 Unrecognized authentication
type
Here are my literal SMTP settings as being handed to PHPMailer:
smtpAuth: true
smtpSecure: STARTTLS
smtpHost: smtp.office365.com
smtpPort: 587
smtpUsername: [hidden]
smtpPassword: [hidden]
emailTo: [hidden]
And the actual PHP code:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = $smtpAuth;
$mail->SMTPSecure = $smtpSecure;
$mail->Host = $smtpHost;
$mail->Port = $smtpPort;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->SetFrom($smtpSenderDisplay);
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($emailTo);
As as sanity check, I set up an SMTP account with these same settings in Windows Live Mail - and everything works. No errors. Outgoing email lands in the inbox of a different email account I have.
So Windows Live Mail is doing something a little differently from what my PHP script is doing AND remember my PHP script was working fine up until a few days ago.
Any ideas what I need to change?
Microsoft changed their SMTP encryption requirement from STARTTLS to TLS. That solved it.

Can't send email with php using gmail - Authorising error

I'm new to PHP and trying to build a website with registration form. Users should receive an email with a confirmation link (since I have a free hosting server I am trying to use gmail server). The thing is I'm struggling with the email php code. I am trying to use PHPMailer functions as follows:
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$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 = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//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 = "MYEMAIL#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "MYPASSWORD";
//Set who the message is to be sent from
$mail->setFrom('example#example.com', 'Name S');
//Set an alternative reply-to address
$mail->addReplyTo('example#example.com', 'Name S');
//Set who the message is to be sent to
$mail->addAddress('email#gmail.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("HELLo");
//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
I tried both ports 587 and 465. Also tried both ssl and tls. Every time I try to run the code I get the following error:
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP gw4sm17090153wjc.45 - gsmtp
CLIENT -> SERVER: EHLO spec.dr-manny.co.uk SERVER -> CLIENT:
250-smtp.gmail.com at your service, [185.27.134.36]250-SIZE
35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN
OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250
SMTPUTF8 CLIENT -> SERVER: AUTH LOGIN SERVER -> CLIENT: 334
VXNlcm5hbWU6 CLIENT -> SERVER: bWFubnlzYWVkaUBnbWFpbC5jb20= SERVER ->
CLIENT: 334 UGFzc3dvcmQ6 CLIENT -> SERVER: a2luZ29uZW1vaDk5 SERVER ->
CLIENT: 534-5.7.14
Please log in via your web browser
and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14
https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 -
gsmtp SMTP ERROR: Password command failed: 534-5.7.14
Please log in via your web browser
and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14
https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 -
gsmtp SMTP Error: Could not authenticate. CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection gw4sm17090153wjc.45 -
gsmtp SMTP connect() failed.
Error: SMTP connect() failed.
Also, I got an email from Gmail with the subject "Someone has your password". I opened the email and found this "
Hi Manny,
Someone just used your password to try to sign in to your Google Account MYEMAIL#gmail.com, using an application such as an email client or mobile device."
I received this email about 15 mins after I ran the php page.
I turned off "2-step verification" and turned off "allow less secure apps". Nothing seems to help. Anyone please could help me?
I had the same issue last year: the code (VB.Net) was OK, so where the credentials. The problem was that gmail didn't like that some app in a web server far away (my hosting) where trying o use your same username.
I fixed it (in two different occasions) by:
Logging in gmail once from that server (using remote desktop).
Logging in here https://accounts.google.com/DisplayUnlockCaptcha from my pc to "unlock" remote connections.
After that test your code again.

Why PhpMailer failes on some machines?

I'm using PHPMailer to send emails. My problem is that from my PC everything works just fine, but on other devices, like someone else laptop or PC gives the following error:
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.The following From address failed: confidential#confidential.com Mailer Error: The following From address failed: confidential#confidential.com
SMTP server error: AUTH command used when not advertised
Can someone give some light on this one please?!
My code:
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = stripslashes($_POST['textarea']);
$body = str_replace(' ',' ',$body);
$body = str_replace('/media/','http://www.confidential.com/media/', $body);
$body = $body;
//$body = eregi_replace("[\]",'',$body);
$mail->From = "confidential#confidential.com";
$mail->FromName = "confidential confidential";
$mail->CharSet = 'UTF-8';
$mail->Subject = $_POST['subject'];
//$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "confidential.com"; // SMTP server
//$mail->SMTPAuth = false; // enable SMTP authentication
//$mail->Port = 25; // set the SMTP port for the GMAIL server
//$mail->Username = "confidential#confidential.com"; // SMTP account username
//$mail->Password = "confidential"; // SMTP account password
$mail->SetFrom('confidential#confidential.com', 'confidential confidential');
$mail->AddReplyTo("confidential#confidential.com","confidential confidential");
//$mail->SMTPDebug = 1;
Furthermore, when I use SMTP authentication I have these next errors, on every device:
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.SMTP Error: The following recipients failed: someemail#test.com Mailer Error: SMTP Error: The following recipients failed: someemail#test.com
SMTP server error: Authentication failed
UPDATE:
Full errors resulted:
SMTP -> FROM SERVER:220-sclabo15.ds.systemsunit.com ESMTP Exim 4.86_1 #1 Wed, 16 Mar 2016 13:10:25 +0200 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
SMTP -> FROM SERVER: 250-sclabo15.ds.systemsunit.com Hello confidential.com [176.223.213.66] 250-SIZE 52428800 250-8BITMIME 250-PIPELINING 250-AUTH PLAIN LOGIN 250-STARTTLS 250 HELP
SMTP -> ERROR: Password not accepted from server: 535 Incorrect authentication data
SMTP -> FROM SERVER:250 Reset OK
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.SMTP -> FROM SERVER:250 OK
SMTP -> FROM SERVER:550 Authentication failed
SMTP -> ERROR: RCPT not accepted from server: 550 Authentication failed
SMTP Error: The following recipients failed: confidential#confidential.com Mailer Error: SMTP Error: The following recipients failed: confidential#confidential.com
SMTP server error: Authentication failed
does the password use any non-ASCII characters? – Martin #Martin,
if '#' is non ASCII, then yes. - Anonymous
Your issue may be that your password contains the # character. This character is a bit of a pain as it doesn't quite fit as being "safe" but is often not actually removed or cleaned by many cleaning functions. It is a URL address special character and has repeatedly come up as being a cause for similar SMTP errors:
https://sourceforge.net/p/davmail/bugs/539/
https://meta.discourse.org/t/bug-smtp-password-field-does-not-escape-comment-sign-hash/23344/6
Also check your string encoding for the username and password strings. Your reference to $mail->CharSet = 'UTF-8'; might be causing the PHPMailer to encode the password for the SMTP (I'm not sure though).
http://www.easy-smtp.com/smtp-troubleshooting-guide
There are also various other knock on events such as you will be blocked from the server for to many failed authentication attempts. Frustrating, but needs to be noted.
http://www.fehcom.de/qmail/smtpauth.html
http://www.samlogic.net/articles/smtp-commands-reference-auth.htm
Solution:
Debug and change your password to not contain the # character and see if that improves your situation. But please note your potential for being blocked for too many false logins and you'd need to wait some time for a clear connection to te SMTP server again.
$mail->SMTPDebug = 2; enables SMTP debug information (for testing)

PHPMailer SMTP ERROR: command failed

My code works when i use gmail has host, but for this project i have to use office365.com and i receive errors i do not know what to do with :
$mail = new PHPMailer;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->IsHTML(true);
$mail->Host = "smtp.office365.com"; // SMTP server
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Port = 587; // set the SMTP port for the GMAIL
$mail->Username = "username"; // username
$mail->Password = "password"; // password
//works with this (gmail)
//$mail->Host = "smtp.gmail.com"; // SMTP server
//$mail->SMTPSecure = "tls"; // sets the prefix to the servier
//$mail->Port = 587; // set the SMTP port for the GMAIL
//$mail->Username = "username"; // GMAIL username
//$mail->Password = "password"; // GMAIL password
$mail->setFrom('someEmailAdress');
$mail->addReplyTo("someEmailAdress");
$mail->AddAddress('someEmailAdress');
$mail->Subject = $_POST['ActivityType']." ".$_POST['DealerName'];
$mail->Body = $message;
$mail->WordWrap = 400;
$mail->IsHTML(true);
$mail->SMTPDebug = 2;
$mail->Send();
this is the error i get
12014-12-22 18:15:39 SERVER -> CLIENT: 220 BY2PR05CA022.outlook.office365.com
Microsoft ESMTP MAIL Service ready at Mon, 22 Dec 2014 18:15:39 +0000
2014-12-22 18:15:39 CLIENT -> SERVER: EHLO 192.168.1.53
2014-12-22 18:15:39 SERVER -> CLIENT: 250-BY2PR05CA022.outlook.office365.com Hello [24.37.210.58]
250-SIZE 78643200
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250 CHUNKING
2014-12-22 18:15:39 CLIENT -> SERVER: STARTTLS
2014-12-22 18:15:39 SERVER -> CLIENT: 220 2.0.0 SMTP server ready
2014-12-22 18:15:39 CLIENT -> SERVER: QUIT
2014-12-22 18:15:40 SERVER -> CLIENT:
2014-12-22 18:15:40 SMTP ERROR: QUIT command failed:
SMTP connect() failed
been tryin to figure out whats going on, but theres really not much to search upon with the "general" error of quit command failed. Especially when gmail works fine, could it be on office365 side?
btw outlook is most restricted of them all specially when it comes to html emails which is the one u have set in ur options ,try first to send to something like gmail ,maybe yahoo but again it have some restriction but gmail works everytime without any probs ,u can also use something like fakesmtp to catch the sent email.
sorry can't comment yet but try these
https://github.com/PHPMailer/PHPMailer/issues/189
or may be a selinux thing
http://osticket.com/forum/discussion/77982/resolved-smtp-office365-issue

PHPMailer problem connecting to smtp.mail.emea.microsoftonline.com

I'm getting the following error when trying to send an email from a web server running W2K server 2003 to smtp.mail.emea.microsoftonline.com
SMTP -> FROM SERVER:220 smtp.mail.emea.microsoftonline.com Microsoft ESMTP MAIL Service ready at Thu, 8 Oct 2009 01:00:53 -0700
SMTP -> FROM SERVER: 250-smtp.mail.emea.microsoftonline.com Hello [78.109.167.122] 250-SIZE 31457280 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-STARTTLS 250-AUTH 250-8BITMIME 250-BINARYMIME 250 CHUNKING
SMTP -> ERROR: AUTH not accepted from server: 504 5.7.4 Unrecognized authentication type
SMTP -> FROM SERVER:250 2.0.0 Resetting
SMTP Error: Could not authenticate.
Can anyone spot what is wrong? I have the the following set in php.ini as well:
[mail function]
; For Win32 only.
SMTP = smtp.mail.emea.microsoftonline.com
smtp_port = 587
; For Win32 only.
sendmail_from = enquiries#domain.com
require_once('class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "smtp.mail.emea.microsoftonline.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
//$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Port = 587; // set the SMTP port for the server
$mail->Username = "enquiries#domain.com"; // username
$mail->Password = "Password"; // password
$mail->AddReplyTo('enquiries#domain.com', 'First Last');
$mail->AddAddress('myemail#gmail.com', 'John Doe');
$mail->SetFrom('enquiries#domain.com', 'First Last');
$mail->AddReplyTo('enquiries#domain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('examples/contents.html'));
$mail->Send();
echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Try these manual steps first to see if you can connect at all:
http://technet.microsoft.com/en-us/library/aa995718(EXCHG.65).aspx
Also, this page:
http://www.gilham.org/Blog/Lists/Posts/Post.aspx?List=aab85845%2D88d2%2D4091%2D8088%2Da6bbce0a4304&ID=665
says that: The FROM address must use a SMTP domain of type “Authoritative” – this can be confirmed in Microsoft Online Administration Center, under the Users - Domains tab.
I've same problem some month ago. And the error caused by our SMTPSecure is used ntlm.
Don't forget to relay your mail server which it's allow to sent and receive mail from the web.
And recheck your account email and the password that is it right?

Categories