PHPMailer dev-xoauth failed authentication - oauth-2.0

So I've gone through all of the docs on github - phpmailer xoauth tree to set up my scripts accordingly to use Google's oauth2. I am trying to send an email from the form at halcyonco.io to myself. The domain name is just to live test this site. I encounter the same error when sending the form locally.
My question is when I submit the form I receive this error:
SERVER -> CLIENT: 220 mx.google.com ESMTP pj4sm27148656pbb.29 - gsmtp
CLIENT -> SERVER: EHLO halcyonco.io
SERVER -> CLIENT: 250-mx.google.com at your service, [68.65.121.206]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 halcyonco.io
SERVER -> CLIENT: 250-mx.google.com at your service, [68.65.121.206]
250-SIZE 35882577250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN XOAUTH
250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING
250 SMTPUTF8
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection pj4sm27148656pbb.29 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I have been in touch with Namecheap as I have my hosting and domains through them but after an hour and half they could not help me.
Below is my contact.php script
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["user_name"]);
$email = trim($_POST["user_email"]);
$comment = trim($_POST["comment"]);
if ($name == "" OR $email == "" OR $comment == "") {
echo '<h3>Please fill out all forms.</h3>';
exit;
}
// SPAM protection
foreach ($_POST as $value) {
if ( stripos($value, 'Content-Type') !== FALSE ) {
echo "There was a problem with the information you entered.";
exit;
}
}
//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('America/New_York');
require '../vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
//Load dependnecies from composer
//If this causes an error, run 'composer install'
require '../vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailerOAuth;
//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';
//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;
//Set AuthType
$mail->AuthType = 'XOAUTH2';
//User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
$mail->oauthUserEmail = "myemail#gmail.com";
//Obtained From Google Developer Console
$mail->oauthClientId = "1234567890.apps.googleusercontent.com";
//Obtained From Google Developer Console
$mail->oauthClientSecret = "123456789";
//Obtained By running get_oauth_token.php after setting up APP in Google Developer Console.
//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
// eg: http://localhost/phpmail/get_oauth_token.php
$mail->oauthRefreshToken = "1/ABCD12345EFGH6789";
//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, $name);
//Set who the message is to be sent to
$mail->addAddress('myemail#gmail.com', 'Brandon Smith');
//Set the subject line
$mail->Subject = 'Promethean Fitness Enquiry | ' . $name;
//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 = $comment;
//Replace the plain text body with one created manually
$mail->Body = $comment;
$mail->AltBody = $comment;
//Attach an image file
//send the message, check for errors
if (!$mail->send()) {
echo '<h3>Please ensure you have entered a correct email address.</h3><br>';
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header("Location: ../html/thanks.html");
exit;
}
//This is a spam protection
if ($_POST["user_address"] != "") {
echo "Your form submission has an error";
exit;
}
}
?>
Any help on what I need to do in order to fix this would be great.

Related

What is proper way to setup auth layer for CouchbaseLite (CBLListener and CBLReplication) for p2p database replication?

I am trying to configure basic login / password authentication for p2p replication.
Replication works fine with no authentication. So I pretty sure my issue relates to authentication configuration.
CBListener configuration:
login = "my_login"
password = "my_password"
listener = CBLListener(manager: CBLManager.sharedInstance(), port: 55555)
listener.setBonjourName(netServiceName, type: netServiceType)
listener.readOnly = true
listener.setPasswords([login : password])
try! listener.start()
CBLReplication configuration:
let netService: NetService = ... // Got from NetServiceBrowserDelegate and NetServiceDelegate
let components = NSURLComponents()
components.scheme = "http"
components.host = netService.hostName
components.port = NSNumber(value: 55555)
components.path = "/" + database.name
components.user = login
components.password = password
let newReplication = database.createPullReplication(components.url!)
newReplication.continuous = true
newReplication.start()
Here is what I am getting in Xcode console:
CBLRestPuller[http://my_login:*****#Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: Going online
CBLRestPuller[http://my_login:*****#Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: No local checkpoint; not getting remote one
CBLRestPuller[http://my_login:*****#Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net] Progress: set active = 1
CBLReplication[from http://my_login:*****#Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: active, progress = 0 / 0, err: (null)
nil
CBLRestPuller[http://my_login:*****#Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: Reachability state = :reachable (20002), suspended=0
: HTTP auth failed; sent Authorization: Basic bXlfbG9naW46bXlfcGFzc3dvcmQ= ; got WWW-Authenticate: Digest realm="CouchbaseLite", qop="auth", nonce="69773230-683C-4DCC-AB40-A21527A1F911"
CBLSocketChangeTracker[0x127d63810 sg_to_firstfoundry_net]: Can't connect, giving up: CBLHTTP[401, http://my_login:*****#Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_changes?feed=normal&heartbeat=300000&style=all_docs]<--NSURLError[-1013, http://my_login:*****#Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_changes?feed=normal&heartbeat=300000&style=all_docs]
CBLRestPuller[http://my_login:*****#Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: ChangeTracker stopped; error=CBLHTTP[401, http://my_login:*****#Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_changes?feed=normal&heartbeat=300000&style=all_docs]<--NSURLError[-1013, http://my_login:*****#Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_changes?feed=normal&heartbeat=300000&style=all_docs]
I think the most important log here is this line:
: HTTP auth failed; sent Authorization:
Basic bXlfbG9naW46bXlfcGFzc3dvcmQ= ; got WWW-Authenticate: Digest
realm="CouchbaseLite", qop="auth",
nonce="69773230-683C-4DCC-AB40-A21527A1F911"
Alternatively I tried to use CBLAuthenticator instead of injecting login/password into url. Still doesn't work, however I am getting different error messages in log:
What is changed:
// Added:
newReplication.authenticator = CBLAuthenticator.basicAuthenticator(withName: login, password: password)
// Removed:
components.user = login
components.password = password
Here is what I am getting in Xcode console after updating the code:
CBLRestPuller[http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: Going online
CBLRestPuller[http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net] Progress: set active = 1
CBLRestPuller[http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: Successfully logged in!
RemoteRequest: Added Authorization header for CBLPasswordAuthorizer[my_login/****]
CBLRemoteJSONRequest[GET http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_local/b3f625c9bddabfb59ee49d78f3bb484a5876b486]: Starting...
DEALLOC CBLRemoteLogin[http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]
2017-02-20 16:51:28.335 Union Dev[1922:1702832] RemoteRequest: CBLRemoteSession starting CBLRemoteJSONRequest[GET http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_local/b3f625c9bddabfb59ee49d78f3bb484a5876b486]
Sync: CBLReplication[from http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: active, progress = 0 / 0, err: (null)
nil
CBLRestPuller[http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: Reachability state = :reachable (20002), suspended=0
RemoteRequest: Got challenge for CBLRemoteJSONRequest[GET http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_local/b3f625c9bddabfb59ee49d78f3bb484a5876b486]: method=NSURLAuthenticationMethodHTTPDigest, err=(null)
RemoteRequest: challenge: (phase 3) continueWithoutCredential
Sync: CBLRestPuller[http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: Server is (null)
RemoteRequest: CBLRemoteJSONRequest[GET http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_local/b3f625c9bddabfb59ee49d78f3bb484a5876b486]: Got response, status 401
CBLRemoteJSONRequest[GET http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_local/b3f625c9bddabfb59ee49d78f3bb484a5876b486]: Got error CBLHTTP[401, "unauthorized", http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_local/b3f625c9bddabfb59ee49d78f3bb484a5876b486]
Sync: CBLRestPuller[http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net]: Error fetching last sequence: CBLHTTP[401, "unauthorized", http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_local/b3f625c9bddabfb59ee49d78f3bb484a5876b486]
Sync: CBLRestPuller[http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net] Progress: set error = CBLHTTP[401, "unauthorized", http://Uladzimir-Papkos-iPad.local.:55555/sg_to_firstfoundry_net/_local/b3f625c9bddabfb59ee49d78f3bb484a5876b486]
Could anybody point me to place where I am doing something wrong?
After having conversation with Couchbase engineers it was decided that the best solution here is to enable secure http connection, since it doesn't make sense to send login / password through insecure http request.
The easiest (not most secure) way to enable https is to do next:
// Change url to https
components.scheme = "https"
// Inject self-signed generated on the fly certificate into CBLListener
try listener.setAnonymousSSLIdentityWithLabel("use_any_string_here")
More information about configuring https connection for p2p replication is available here.

Form data to an email address in asp.net core

What is the easiest way to have form data sent to an email address , in an asp.net core app? I have tried to send an email from my outlook account to gmail one wit Mailkit, but i keep getting an error AuthenticationException: AuthenticationInvalidCredentials , even if my credentials are corect.
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Joey Tribbiani", "joey#outlook.com"));
message.To.Add(new MailboxAddress("Mrs. Chanandler Bong", "Chandler#gmail.com"));
message.Subject = "How you doin'?";
message.Body = new TextPart("plain")
{
Text = #"Hey Chandler"
};
using (var client = new SmtpClient())
{
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("smtp.outlook.com", 587, false);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
// Note: only needed if the SMTP server requires authentication
client.Authenticate("username", "*************");
client.Send(message);
client.Disconnect(true);
}

"Unrecognized SSL message, plaintext connection" during send mail using grails async mail api

I am trying to send mail using Grails Async Mail API
When I tried for Gmail using below configuration :
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "xxxx#gmail.com"
password = "xxxx"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
in config.groovy, I was able to send mail. When I changed above configuration for a specific mail server as below :
grails {
mail {
host = "xxx"
port = 25
username = "xxx"
props = ["mail.smtp.auth":"false",
"mail.smtp.socketFactory.port":"25",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"true",
"mail.smtp.starttls.enable": "false",
"mail.smtp.starttls.required": "false"]
}
}
I am getting below error :
javax.mail.MessagingException: Could not connect to SMTP host: xxx, port: 25;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
Please provide me some hint, what I am doing wrong in above case.
You can try to do simply following and it should work for you
grails {
mail {
host = "xxx"
port = 25
username = "xxx"
props = ["mail.smtp.auth":"false",
"mail.smtp.socketFactory.port":"25",
"mail.smtp.starttls.enable": "true"]
}
}

Email verification in grails

I am new with grails and am developing a web application in grails.
In my registration page I am getting the user's email id and I need to send a mail with authentication link.
http://grails.org/plugin/mail
http://grails.org/plugin/email-confirmation
I have referred these pages and many other pages to do this task.
But the problem is, my email is not sending.
I have used
Gmail SMTP server address : smtp.gmail.com
Gmail SMTP username : myid#gmail.com
Gmail SMTP password : -my password-
Gmail SMTP port : 465
Gmail SMTP TLS/SSL required : yes
Mail settings are:
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "myId#gmail.com"
password = "mypassword"
props = [
"mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class": "javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
}
}
grails.mail.default.from="noreply#gmail.com"
but at least
sendMail {
to "friend#gmail.com"
subject "Hello "
body 'How are you?'
}
is not working.
The exception occured is
Error 500: Internal Server Error
URI
/MailVerificationDemo/user/signup/form
Class
java.net.ConnectException
Message
Connection refused
Around line 104 of MailMessageBuilder.groovy
101: log.trace("Sending mail ${getDescription(message)}} ...")102: }103:104: mailSender.send(message instanceof MimeMailMessage ? message.mimeMessage : message)105:106: if (log.traceEnabled) {107: log.trace("Sent mail ${getDescription(message)}} ...")
Around line 41 of grails-app/services/grails/plugin/mail/MailService.groovy
38: callable.resolveStrategy = Closure.DELEGATE_FIRST39: callable.call()40:41: messageBuilder.sendMessage()42: }43:44: def getMailConfig() {
Around line 18 of grails-app/controllers/user/UserController.groovy
15: return16: }17:18: mailService.sendMail {19: to userInstance.email20: subject "New User Confirmation"21: html g.render(template:"mailtemplate",model:[code:userInstance.confirmCode])
Around line 195 of PageFragmentCachingFilter.java
192: if (CollectionUtils.isEmpty(cacheOperations)) {193: log.debug("No cacheable annotation found for {}:{} {}",194: new Object[] { request.getMethod(), request.getRequestURI(), getContext() });195: chain.doFilter(request, response);196: return;197: }198:
Around line 63 of AbstractFilter.java
60: try {61: // NO_FILTER set for RequestDispatcher forwards to avoid double gzipping62: if (filterNotDisabled(request)) {63: doFilter(request, response, chain);64: }65: else {66: chain.doFilter(req, res);
Try this it's worked for me.
Notice that: Gmail SMTP TLS/SSL required : yes.
But you don't put "mail.smtp.starttls.enable": "true"
grails.mail.host="smtp.gmail.com"
grails.mail.port=587
grails.mail.username="yourUsernameHere"
grails.mail.password="yourPwdHere"
grails.mail.from="defaultMailFromHere"
grails.mail.props = ['mail.smtp.auth': "true",
"mail.smtp.starttls.enable": "true",
"mail.from":"defaultMailFromHere"]
grails.mail.javaMailProperties = ['mail.smtp.auth': "true",
"mail.smtp.starttls.enable": "true",
"mail.from":"defaultMailFromHere"]

Confused with the SmtpClient.UseDefaultCredentials Property

In my MVC4 application, I'm using the SmtpClient to send out email via Gmail's smtp.gmail.com SMTP server.
I've configured my Web.Config file with the following settings:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network enableSsl="true"
defaultCredentials="false"
host="smtp.gmail.com"
port="587"
userName="xxMyUserNamexx#gmail.com"
password="xxMyPasswordxx" />
</smtp>
</mailSettings>
</system.net>
The method that uses the SmtpClient and sends an email message looks like:
public void SendMail(string fromDisplayName, string fromEmailAddress, string toEmailAddress, string subject, string body)
{
MailAddress from = new MailAddress(fromEmailAddress, fromDisplayName);
MailAddress to = new MailAddress(toEmailAddress);
MailMessage mailMessage = new MailMessage(from, to);
mailMessage.Body = body;
mailMessage.Subject = subject;
SmtpClient client = new SmtpClient();
//client.UseDefaultCredentials = false;
client.Send(mailMessage);
}
The code above works as expected and is fine. What confuses me is the commented line client.UseDefaultCredentials = false; - If I were to uncomment that line, I'll receive an exception message that states:
The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication Required.
What's more is, it doesn't matter if I set the UseDefaultCredentials property to true or false, I'll still receive the exception message. The only way for me to avoid the exception message is to remove the line altogether.
Is this behavior normal? Can you explain why I'm receiving the exception message?
So why would me explicitly setting the property to false throw an exception?
The reason for this is because the setter for UseDefaultCredentials sets the Credentials property to null if you set it to false, or it sets it to the CredentialCache.DefaultNetworkCredentials property if set to true. The DefaultNetworkCredentials property is defined by MSDN as:
The credentials returned by DefaultNetworkCredentials represents the authentication credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application. For ASP.NET applications, the default network credentials are the user credentials of the logged-in user, or the user being impersonated.
When you set UseDefaultCredentials to true, it's using your IIS user, and I'm assuming that your IIS user does not have the same authentication credentials as your account for whatever SMTP server you're using. Setting UseDefaultCredentials to false null's out the credentials that are set. So either way you're getting that error.
Here's a look at the setter for UseDefaultCredentials using dotPeek:
set
{
if (this.InCall)
{
throw new InvalidOperationException(
SR.GetString("SmtpInvalidOperationDuringSend"));
}
this.transport.Credentials = value
? (ICredentialsByHost) CredentialCache.DefaultNetworkCredentials
: (ICredentialsByHost) null;
}
I was getting the same message and it was driving me crazy. After reading this thread I realized that the order mattered on setting my credentials. This worked:
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(smtpSettings.Username, smtpSettings.Password);
While this generated the error you describe:
client.Credentials = new NetworkCredential(smtpSettings.Username, smtpSettings.Password);
client.UseDefaultCredentials = false;
This is just an FYI to anybody else having the same problem.
This option will set the client to use the default credentials of the currently logged in user
If you set it to true, then it will try to use the user's credentials. If you set it to false, then it will use the values explicitly set for the Credentials property of the client, and if they aren't explicitly set, then it will try to connect anonymously as you are seeing.
This is how we can create SMTP Client with or without NetworkCredentials. I am using this code to send emails. We should use client.UseDefaultCredentials only when we are not passing credentials and going by default.
private SmtpClient InitializeSMTPClient()
{
var client = new SmtpClient(_smtpServer, _smtpPort);
client.UseDefaultCredentials = _useSMTPDefaultCredentials;
if (_useSMTPDefaultCredentials)
return client;
var credentials = new NetworkCredential(_smtpUsername, _smtpPassword);
client.Credentials = credentials;
return client;
}
SMTPEmailResult SendSMTPEmail(List<string> to_email, List<string> ccEmails, string subject, string message)
{
try
{
using (var client = InitializeSMTPClient())
{
var mail_message = GetMailMessage(to_email, ccEmails, subject, message);
log.Debug("Sending SMTP email.");
client.Send(mail_message);
log.Debug("SMTP email sent successfully.");
return SMTPEmailResult.SendSuccess;
}
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
return SMTPEmailResult.SendFailed;
}
}

Resources