PHP error: SMTP Error: Could not authenticate - php

I tried to create a mail-confirm signup system but when I signup, database save my data but there is a error that says:
SMTP Error: Could not authenticate.
note: I am using class.phpmailer.php
I could not understand the problem. Here is my code:
sendmail.php
<?php
require 'PHPMailerAutoload.php';
function send_mail($sendto, $subject, $body)
{
$mail = new PHPMailer;
//$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 = '**.****.****#gmail.com'; // SMTP username
$mail->Password = '**********'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('e.d***.c**#gmail.com', 'Restro | Restaurants in Mumbai');
$mail->addAddress($sendto); // Name is optional
$mail->addReplyTo('e.d****.c***#gmail.com', 'Restro | Restaurants in Mumbai');
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = 'Please open this on Chrome';
if (!$mail->send()) {
//echo 'Message could not be sent.';
return 'Mailer Error: ' . $mail->ErrorInfo;
} else {
return 'Message has been sent';
}
}

Use port 465
port 587 if your client begins with plain text before issuing the STARTTLS command.

Related

SMTP connection failed

I'm facing this issue from last three days, before this script worked perfectly. Now getting error:
SMTP ERROR: Failed to connect to server: (0) 2017-10-06 21:05:34 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message was not sent.Mailer error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
ahsanazhar12#gmail.com
Here is my script:
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 = 'example#gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->setFrom('example#gmail.com', 'Your Name');
$mail->addAddress('example#gmail.com', 'My Friend');
$mail->Subject = 'First PHPMailer Message';
$mail->Body = 'Hi! This is my first e-mail sent through PHPMailer.';
if (!$mail->send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
I think Gmail may have changed settings for sending emails using SMTP, or something like that.
Finally i'm able to send emails from localhost. Here is my code.
To install:
Download PHPMailer
Add it to your project (i put it on root)
Add Autoload class to your Script.
Rest of code is below
require "PHPMailer/PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//$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 = 'example#gmail.com'; // SMTP username
$mail->Password = 'securepass'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('example#gmail.com', "Mailer");
$mail->addAddress("example#gmail.com","receiver Name");
$mail->isHTML(true);
$mail->Subject = "Subject";
$mail->Body = "Body";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if( $mail->send()){
return array("msg"=>msg("success","Email has been sent.<br>"));
} else {
return array("msg"=>msg("error","Email can't send.Try Again<br>"));
}

PHP outputting the code and not a proper output

I am new to PHP. I've been trying to get this mailer to work and I have gotten most of it working.
Here is my PHP:
<?php
require __DIR__ . '/vendor/autoload.php';
$included_files = get_included_files();
foreach ($included_files as $filename) {
echo "$filename\n";
}
$mail = new PHPMailer;
$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 = 'rottenrobbie911#gmail.com'; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('yalnifcam2#gmail.com', 'Joe User');
$mail->Subject = 'GenericSubject';
$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';
}
?>
When I run my code here without a password this comes up:
/var/www/html/mail.php /var/www/html/vendor/autoload.php /var/www/html/vendor/composer/autoload_real.php /var/www/html/vendor/composer/ClassLoader.php /var/www/html/vendor/composer/autoload_static.php /var/www/html/vendor/guzzlehttp/promises/src/functions_include.php /var/www/html/vendor/guzzlehttp/promises/src/functions.php /var/www/html/vendor/guzzlehttp/psr7/src/functions_include.php /var/www/html/vendor/guzzlehttp/psr7/src/functions.php /var/www/html/vendor/paragonie/random_compat/lib/random.php /var/www/html/vendor/guzzlehttp/guzzle/src/functions_include.php /var/www/html/vendor/guzzlehttp/guzzle/src/functions.php /var/www/html/vendor/sendgrid/sendgrid/lib/SendGrid.php /var/www/html/vendor/sendgrid/sendgrid/lib/helpers/mail/Mail.php /var/www/html/vendor/swiftmailer/swiftmailer/lib/swift_required.php /var/www/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift.php
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
But put a password in and it doesn't like it. Instead prints the code to the webpage like so.
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 = 'rottenrobbie911#gmail.com'; // SMTP username $mail->Password = 'password';
// SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 465; $mail->setFrom('from#example.com', 'Mailer'); $mail->addAddress('yalnifcam2#gmail.com', 'Joe User'); $mail->Subject
= 'GenericSubject'; $mail->Body = 'This is the HTML message body in bold!'; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } ?>
It's just the password. When I enter a password it doesn't work but leave it bank and it just fails to authenticate.
Reference gmail smtp settings
Use port no 587 for TLS.
change this $mail->Port = 465; to $mail->Port = 587;

PHP Mailer Multiple recipients

I'm using php mailer for mail triggering.
Its working fine. But I gave 2 to 5 recipients, it sends the mail to only one recipient. In future, I have to trigger a mail to nearly 100 recipients..
I've shared my code below.. Please check it..
require 'phpmailer/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 = 'karthick****#gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('karth*******#gmail.com', 'A**n');
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
$mail->AddAddress($address);
}
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
You're code looks like it should be doing the trick. Make sure that $address doesn't contain any whitespace for the entries. For safe measure, add the trim()
function.
$mail->AddAddress(trim($address));
If that does not work, make sure that you're recipient addresses are real.
Additionally, in case the privacy of the recipients is of concern, I would recommend you use AddBCC() instead of AddAddress() so that their addresses are not revealed.
A basic idea is that make different connection (object) for each your mailing address like below if You don't have so much addresses in your array.
require 'phpmailer/PHPMailerAutoload.php';
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
$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 = 'karthick****#gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('karth*******#gmail.com', 'A**n');
$mail->AddAddress($address);
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
UPDATE :
The second idea is that you can remove recepients each time and add new one and then send like below
require 'phpmailer/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 = 'karthick****#gmail.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('karth*******#gmail.com', 'A**n');
$addresses = explode(',',$emailM);
foreach ($addresses as $address) {
// for clear last recipients
$mail->ClearAllRecipients( )
$mail->AddAddress($address);
$mail->isHTML(true);
$mail->Subject = 'Need for '.$keyword.'';
$mail->Body = 'Hi,The Message';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Qoute has been sent to all the Manufacturers';
echo "$address";
}
}

PHP Mailer can't send emails using my work internet connection

i'm developing a website to send reminders to employees in my company using phpmailer, the emails are working fine, but when i connect to the workplace internet connection it stops working with no errors but:
Connection: opening to smtp.mail.yahoo.com:587, timeout=300, options=array ( )
i'm guessing it might be a closed port, so i tried using 587 and 25.
i'm not that good in networking, is there's any way i can know what is the exact error?
function SendOutlook(){
//$recipient = 'xxxxx#yahoo.com';
require 'PHPMailer/PHPMailerAutoload.php';
$Emp_recipient = "employee#yahoo.com";
$mail = new PHPMailer;
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = "smtp.mail.yahoo.com"; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxx#yahoo.com'; // SMTP username (server username)
$mail->Password = 'xxxxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('xxxxx#yahoo.com', 'One');
$mail->addAddress($Emp_recipient,"name"); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'test';
$mail->Body = "scheduled on your outlook calender";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit();
}else {
echo '<p>Message has been sent</p>';
}
echo !extension_loaded('openssl')?"Not Available":"Available";
}

Trying to send mail using yahoo smtp in php

Please can some one suggest me codes to send mails using yahoo smatp, and php..
require("class.phpmailer.php"); // be sure to change this to your location!
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.mail.yahoo.com"; // sets yahoo as the SMTP server
$mail->Port = 25; // set the SMTP port
$mail->Username = "sumthing#yahoo.com"; // yahoo username
$mail->Password = "password"; // yahoo password
$mail->From = "sumthing#yahoo.com";
$mail->FromName = "myname";
$mail->AddAddress("you#example.com");
$mail->Subject = "Test PHPMailer Message";
$mail->Body = "Hi! \n\n This was sent with phpMailer_example3.php.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
This code is giving me errrors 'Could not connect to SMTP host".
Many of the sites providing SMTP do not support SSL (SMTP-S). If supported is usually in different port, such as 465. Connecting to 465 often fails. Try commenting out
$mail->SMTPSecure = "ssl"
Try also first without authentication to make sure the connection works.

Categories