SMTP Error: Could not authenticate. while sending email in php - php

Hi i am using PHPMailer for my website to send an email but getting error as SMTP Error: Could not authenticate.
This was error which i was getting.I have given the password correct and turned Allow less secure apps to "ON as well but still getting the same error.
020-01-25 09:11:24 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 oCoMr_x13f0BsQ-UMqe0zgPQOcVX9A7SY4fBk-WoLrRIJfT6uwspq7QpzlUjE8srm9esG534-5.7.14 73T38rlSCuGdkDRsjhkB3YQxKz3V8njuaclMAfqHskMAU3eX_1LYPH80oO9ZLkAl>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 i81sm528818ywe.82 - gsmtp
SMTP Error: Could not authenticate.
2020-01-25 09:11:24 CLIENT -> SERVER: QUIT
Here is the code which i have used for sending an email
<?php ob_start();
if(isset($_POST['submit_contact']))
{
require 'PHPMailer/PHPMailerAutoload.php';
$address = 'testing1#gmail.com';
$name=$_POST['name'];
$email =$_POST['email'];
$subject = $_POST['subject'];
$textMessage = $_POST['message'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug =1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->CharSet = "UTF-8";
$mail->IsHTML(true);
$mail->Username = "testing#gmail.com";
$mail->Password = "PASSword1#3";
$message = array();
$message[] = 'Name : '.trim($name).' ';
$message[] = 'Email : '.trim($email).' ';
$message[] = 'Comment : '.trim($textMessage).' ';
$message = implode(',', $message);
$mail->SetFrom($email);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress($address);
if (!$mail->Send()) {
$msg = "Error while sending email";
$msgclass = 'bg-danger';
} else {
$msg = 'Thank you for contacting us.Will get back to you soon.....';
$msgclass = 'bg-success';
header("Location: contact.php");
}
}
?>
Here is the HTML Code which i have written
<form class="form-horizontal" action="contactus" method="post" role="form" enctype="multipart/form-data">
<?php if(isset( $msg)) {?>
<div class="<?php echo $msgclass; ?>" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<div class="form-group">
<input type="text" name="name" id="name" class="form-control input-field" placeholder="Name" required="">
<input type="email" name="email" id="email" class="form-control input-field" placeholder="Email ID" required="">
<input type="text" name="subject" id="subject" class="form-control input-field" placeholder="Subject" required="">
</div>
<textarea name="message" id="message" class="textarea-field form-control" rows="4" placeholder="Enter your message" required=""></textarea>
<button type="submit" id="btnSend" value="Submit" class="btn center-block" name="submit_contact" >Send message</button>
<!-- Contact results -->
</form>

Always look at the information that’s right in front of you.
Read the troubleshooting guide linked from the error message - it tells you exactly how to deal with this precise problem — you most likely need to perform the “display captcha unlock” step.
It’s not just the “less secure apps” setting (which you can avoid by implementing XOAUTH2), it’s also that gmail imposes additional auth steps when logging in through a new mechanism (I.e. your script), which is briefly mentioned in the link they provide in the responses.

Related

PHP Mailer not working. No error logs, message says it gets sent, but never received

I receive no error logs in my php error log in my web server. I have tried multiple different SMTP servers (other email providers I know work). Here is my HTML form:
<form action="/mailfunction.php" method="post" id="contact-form">
<div class="nameInput">
<input id="boxes" type="text" id="fname" name="name" value="" class="formFormL" placeholder="Name" maxlength="50"></input>
</div>
<div class="emailInput">
<input id="boxes" type="text" id="lname" name="email" value="" class="formFormR" placeholder="Email" maxlength="50"></input>
</div>
<div class="messageInput">
<textarea id="boxes" id="fname" name="message" value="" class="formFormM" placeholder="Message" maxlength="1000"></textarea>
</div>
<div style="padding: 5px;">
<button type="submit" value="Send" class="up" name="submit">Send</button>
</div>
</form>
Here is my mailfunction.php which calls my mailer function outside of public html.
<?php
require '[REDACTED]/mailer.php';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
mails($name, $email, $message);
?>
Here is my mailing function.
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
function mails($name, $email, $message) {
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
try{
$mail = new \PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[REDACTED]';
$mail->Password = '[REDACTED]';
$mail->SMTPSecure = 'SSL';
$mail->Port = 465;
$mail->setFrom('[REDACTED]');
$mail->addAddress('[REDACTED]');
$mail->isHTML(true);
$mail->Subject = 'Message Received (Contact Page)';
$mail->Body = '<h3>Name : $name <br>Email: $email <br>Message : $message</h3>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
}
?>
No messages are sent through. What could be the issue here?
Thank you.
ERROR:
2022-07-07 01:16:57 SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I don't see a problem in your code. The error may be due to the incorrectly set password. Well, authorization procedures may be incorrect. When I got the same error, I determined that this was the problem and solved it. Can you try to fix the problem by watching this video. (Watching until 5:20 will be enough for your problem.)

CS student working on first website, having issues with phpmailer on hostinger

I've finally built my first website and gotten the hosting figured out. As stated in the title I'm using hostinger and phpmailer, I have never used PHP so I went through their short tutorial and got the PHP file set up how I feel is the correct way.
The issue I'm having is that when I submit the form, I get an error page. Nothing in the network tab on chrome suggests that it is trying to send any data, although it does show it opening the PHP file, I think, again this is my first website and first time using PHP.
PHP:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'hostinger-email#domain.com';
$mail->Password = 'password for hostinger email';
$mail->setFrom('same-as-username??', 'Admin');
$mail->addAddress('destination#gmail.com', 'Savannah');
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
$mail->Subject = 'PHPMailer contact form';
$mail->isHTML(false);
$mail->Body = <<<EOT
Email: {$_POST['email']}
Name: {$_POST['name']}
Message: {$_POST['message']}
EOT;
if (!$mail->send()) {
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
$msg = 'Message sent! Thanks for contacting us.';
}
} else {
$msg = 'Share it with us!';
}
?>
HTML FORM:
<header class="form_header">Contact Me</header>
<form id="form" class="topBefore" action="/domains/piecesofblonde.com/public_html/formscript.php" method="post">
<input id="name" type="text" placeholder="NAME">
<input id="email" type="text" placeholder="E-MAIL">
<textarea id="message" type="text" placeholder="MESSAGE"></textarea>
<input id="submit" type="submit" value="GO!">
</form>

Php form instead send mail download on computer

<div class="contact_form clearfix" id="Contact">
<h2>Hello... You can send me message to my universe here.</h2>
<img src="img/planeta1.png" alt="">
<form class="clearfix spaceForm" action="contactform.php" metod="post" >
<label for="name">Your name:</label>
<input type="text" name="name" id="name" placeholder="Jon Doe" required>
<label for="email">Your email:</label>
<input type="text" name="email" id="email" placeholder="something#mama.com" required>
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" required>
<label for="message">Your message:</label>
<textarea name="message" id="message" required></textarea>
<button type="submit" name="submit">Send mail</button>
</div>
and php code here...
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mailTo = "pisitenam#sammostalnisindikatstark.org.rs";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.html");
}
?>
My contact form instead to send message download on computer as php file. I uploaded my site to netfly but stil doesnt work.
Can anybody help and give me a hint where is problem?
On XAMPP im getting blank page and mail is not sent. When I uploaded site on netfly site works fine but contact from when click submit start download php file where is code writen for controling contact form.5 day im trying to find solution for this problem and im geting tired :D So if anybody can help...
you are having some spell mistake in your form tag, first of all correct the method spell in your code as it is not correct so it can't redirect and post your data to contact form.
mail library contains various function.
for example:
<?php require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'your_smtp_domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('john#example.net', 'John doe'); // Add a recipient
$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>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
this can works for you if you use library.

My website doesn't send the mail even though it works perfectly on wamp

Here is my website: www.accurateaccountsinc.tk
The problem is that the website just loads and doesn't do anything.
Everything works properly in localhost/wamp but it doesn't work on the actual site when running the task.
here's the code for the form
<form method="POST" action="index.php">
<input type="text" required name="name" id="name" placeholder="Enter Your Name" />
<input type="text" required name="email" id="email" placeholder="Enter Your Email" />
<input type="text" required name="phone" id="phone" placeholder="Phone Number" />
<textarea name="message" required id="message" placeholder="Enter Your Message"></textarea>
<input type="submit" name="mailed" value="Submit" />
</form>
Here's the php:
<?php
include 'dbconn.php';
if(isset($_POST['mailed'])){
$name = mysqli_real_escape_string($con,$_POST['name']);
$emailadd = mysqli_real_escape_string($con,$_POST['email']);
$contact = mysqli_real_escape_string($con,$_POST['phone']);
$entrymessage = mysqli_real_escape_string($con,$_POST['message']);
include "class.phpmailer.php";
include "class.smtp.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMPTDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "email here";
$mail->Password = "password here";
$mail->SetFrom("email here", 'Website Entry');
$subject = "Client Entry";
$message = "<br>Client Name: " . $name;
$message .= "<br>Email Address: " . $emailadd;
$message .= "<br>Contact Number: " . $contact;
$message .= "<br>Message: " . $entrymessage;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress("email here", $name);
if($mail->Send()){
unset($_POST['mailed']);
echo "<script>window.open('index.php','_self')</script>";
}
}
?>
if there's any more info you'd like to know please do ask..
also note that I only used the class.phpmailer.php and class.smtp.php to minimize the work needed if that's related to the problem..
Typo: $mail->SMPTDebug should be $mail->SMTPDebug, and you should set it to 2 for useful feedback.
You're also not displaying any errors that it may have caused, so echo $mail->ErrorInfo; if sending fails.
You can't send through gmail on port 25, only to gmail. Stick to tls on Port 587, like all the examples and docs say. I don't know where you got your code, but it looks like you used an old example - you should base it on the examples provided with PHPMailer, particularly the ones for gmail.

PHPMailer sending to blank page

I am trying to get PHPMailer to work to send mail from form data(obviously).
ISSUE:
When submitting form, I am getting sent to a blank page. No errors are being reported. I have followed as many stackoverflow threads surrounding this issue as I could find, and no solutions in those threads resolved the issue for myself.
Thanks
ERRORS:
Fixed all errors aside from: 2016-03-26 22:10:43 SMTP ERROR: Failed to connect to server: Connection refused (111) 2016-03-26 22:10:43 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
PHP:
<?php
$message=
'First Name: '.$_POST['first'].'<br />
Last Name: '.$_POST['last'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['message'].'
';
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 587;
// Authentication
$mail->Username = "xxxx#gmail.com"; // Your full Gmail address
$mail->Password = "xxxx"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->AddReplyTo($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("xxxx#gmail.com", "xxxx"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
HTML:
<form class="form-inline" name="contactform" action="mailer.php" enctype="multipart/form-data" method="POST">
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="first" required>First Name</label>
<input type="text" class="form-control-sty" name="first" placeholder="First Name">
</div>
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="last" required>Last Name</label>
<input type="text" class="form-control-sty" name="last" placeholder="Last Name">
</div>
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="phone" required>Phone</label>
<input type="text" class="form-control-sty" name="phone" placeholder="Phone">
</div>
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="email" required>Email</label>
<input type="email" class="form-control-sty" name="mailfrom" placeholder="Email">
</div>
<div class="col-sm-12 form-sty">
<label class="sr-only" for="comment">Comment</label>
<textarea class="form-control-sty" name="message" id="message" rows="7" placeholder="What's on your mind?" required></textarea>
</div>
<div class="col-sm-12 form-sty form-sty-btn">
<button type="submit" value="Submit" class="btn btn-default col-sm-12">Send</button>
</div>
<p><?php if(!empty($message)) echo $message; ?></p>
</form>
You need to redirect the header back to the form (I assume that is where you want to send them) Replace the location inside the header with your URL
<?php
$message=
'First Name: '.$_POST['first'].'<br />
Last Name: '.$_POST['last'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['message'].'
';
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 587;
// Authentication
$mail->Username = "xxxx#gmail.com"; // Your full Gmail address
$mail->Password = "xxxx"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->AddReplyTo($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("xxxx#gmail.com", "xxxx"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
if ($result)
{
header("Location: : http://www.example.com/contactform.php");
}
else {
echo "sending mail failed: " . $mail->ErrorInfo;
}
unset($mail);
}
?>
You should make sure there are no errors. Try placing this code at the top of your file to make sure all errors/warnings are reported and run the script again.
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(E_ALL);

Categories