PHPMailer not working since uploading website - php

Since I have uploaded my website to a server I cannot use PHPMailer to send smtp mails. It worked before on my local server (XAMPP). I am using GMAIL so I also changed all the required gmail settings (i.a. I created another OAuth2 token). Nevertheless, it just won't work...
The PHP code I am using (and which is also working offline) is the following:
<?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');
//Load dependencies from composer
//If this causes an error, run 'composer install'
require 'vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
//Load dependencies 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 = 0;
//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 = "mygmail#gmail.com";
//Obtained From Google Developer Console
$mail->oauthClientId = "IWONTTELLU.apps.googleusercontent.com";
//Obtained From Google Developer Console
$mail->oauthClientSecret = "IWONTTELLU";
//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/IWONTTELLU";
//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('mygmail#gmail.com', 'BLA');
//Set who the message is to be sent to
$mail->addAddress('my#privatemail.com', 'Hi');
//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(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
When I try to use it online, it first of all takes very long until I get an error (tried to use gethostbyname('smtp.gmail.com'), too; long story short: didn't work, too) and secondly, the code I get is Error: Failed to connect to server: Connection timed out (110) & SMTP connect() failed. The service I am using is provided by 1and1 (1und1/1&1). I've been trying to fix this for more than 4 hours now and can't find out what is wrong. Less secure apps can access and I also created a token and "created" a refresh token as in https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2 . The Troubleshooting guide (https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting) couldn't really help me as well although I think it has problems with authenticating... I have also tried to use ssl (of course I also changed the port etc.) which didn't work either.

Is port 587 still supported by gmail? You might want to you SSL port 465. This article might also be useful https://www.wpsitecare.com/gmail-smtp-settings/ or perhaps 1&1 is being blocked by gmail? See this post http://docs.mailpoet.com/article/61-sending-with-gmail-doesnt-work

Related

PHPMailer script works fine in shared server but same script doesn't work in AWS server

I have 2 websites: https://www.nubeduc.cl/ in AWS and http://www.nubeduc.com/ in a cheaper shared hosting.
I use a script of PHPMailer to send emails, but this script only works in nubeduc.com
In both servers, PHPMailer files downloaded today are in folder src (http://www.nubeduc.com/scr/ and https://www.nubeduc.cl/scr/)
I created a php file called "smtp.php" for testing purposes: (http://www.nubeduc.com/smtp.php and https://www.nubeduc.cl/smtp.php)
<?php
/**
* This example shows making an SMTP connection with authentication.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
//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 'src/PHPMailer.php';
require 'src/SMTP.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_LOWLEVEL;
//Set the hostname of the mail server
$mail->Host = 'mail.nubeduc.com';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = 'mailer#nubeduc.com';
//Password to use for SMTP authentication
$mail->Password = 'mypassword';
//Set who the message is to be sent from
$mail->setFrom('mailer#nubeduc.com', 'Nubeduc Plataforma');
//Set an alternative reply-to address
$mail->addReplyTo('contacto#nubeduc.com', 'Nubeduc Contacto');
//Set who the message is to be sent to
$mail->addAddress('***my_email***#gmail.com', 'My Name');
//Set the subject line
$mail->Subject = 'PHPMailer 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(file_get_contents('contents.html'), __DIR__);
$mail->Body = "Hurray! \n\n Great.";
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent from NUBEDUC to stack overflow guys!';
}
When i run this script in nubeduc.com its work like a charm. You can try and check debbug log. But when i run in nubeduc.cl after 110 seconds give these errors on screen:
2020-03-04 20:25:20 Connection: opening to mail.nubeduc.com:25, timeout=300, options=array()
2020-03-04 20:27:30 Connection failed. Error #2: stream_socket_client(): unable to connect to mail.nubeduc.com:25 (Connection timed out) [/var/www/nubeduc.cl/src/SMTP.php line 349]
2020-03-04 20:27:30 SMTP ERROR: Failed to connect to server: Connection timed out (110)
Right now i updated Ubuntu instance, check php openssl extension (is active), but nothing works.
Why the script dont work in my AWS site?
Please help!!!
AWS is blocking your outbound SMTP. This is pretty common and is a separate issue from configuring your security groups. AWS really wants you to relay using their own mail servers with their SES service, but this support document describes how to get this restriction lifted. It says:
Amazon EC2 restricts traffic on port 25 of all EC2 instances by default, but you can request for this restriction to be removed.

Cannot understand PHPMailer debug log

I am trying to use PHPMailer,
I have enabled opensll and it loads
I use XAMPP and PHPStorm
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP z88sm9679wrb.26 - gsmtp
CLIENT -> SERVER: EHLO PhpStorm 2016.1.2
SERVER -> CLIENT: 501-5.5.4 HELO/EHLO argument "PhpStorm 2016.1.2" invalid, closing connection.501 5.5.4 https://support.google.com/mail/?p=helo z88sm9679wrb.26 - gsmtp
SMTP ERROR: EHLO command failed: 501-5.5.4 HELO/EHLO argument "PhpStorm 2016.1.2" invalid, closing connection.501 5.5.4 https://support.google.com/mail/?p=helo z88sm9679wrb.26 - gsmtp
CLIENT -> SERVER: HELO PhpStorm 2016.1.2
SERVER -> CLIENT:
SMTP ERROR: HELO command failed:
SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not connect to SMTP host.
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Here is my code
<?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 '../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';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587; //ssl : 465 --- tls: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 = "te.professionnel#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "*****";
//Set who the message is to be sent from
$mail->setFrom('te.professionnel#gmail.com', 'Abou May');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('abou.may#gmail.com', 'Abou May');
//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(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I have read lots of documentation, bu I cannot figure out the reason of this error.
Can someone givz me an example code?
What is wrong ?
I read all the section on troubleshooting
"SMTP Error: Could not connect to SMTP host."
This may also appear as SMTP connect() failed or Called Mail() without being connected in debug output. This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or other issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why. It can also be caused by not having the openssl extension loaded (See encryption notes below).
I also encounter this problem before.This is not the problem of PhpMailer.But in fact,this is cause by Gmail smtp is denied the connection,therefore you cant connect.
In order for you to solve this problem,you need to
Configure an OAuth2 app in Google Developer Console.
Step is here
If you still cant manage to solve it,I recommend you to use Postmark as your mail server,it is a lot easier to set up the connection.
Here is the guide for the setup of Postmark.For me is a lot easier.Hope it helps.
This is nothing to do with gmail or oauth.
The problem is this line:
CLIENT -> SERVER: EHLO PhpStorm 2016.1.2
When PHPMailer's SMTP client says hello to a server like this, by default it passes the name returned by the internal method serverHostname(). Normally this returns something like localhost.localdomain, or mymac.local (i.e. a real host name), but if that's not available, it tries to figure out what to use - and one of the things it falls back to is the $_SERVER['SERVER_NAME'] super global, which in this case contains PhpStorm 2016.1.2 (I'd guess because you're testing using PHPStorm's built-in web server?), which is not a valid host name, hence the error. This isn't a good move on PHPStorm's part and is probably worth reporting as a bug.
Fortunately you can override automatic determination of the client hostname using the Helo property, which exists precisely for occasions like this. Just do this:
$mail->Helo = 'my.host.name';
substituting either whatever your real host name is, or by calling some other function which gives a usable result.

Failed opening required 'PHPMailer-master/PHPMailerAutoload.php' (include_path='.:/usr/share/pear:/usr/share/php')

I have a hosting server where i have written a php code which is giving me an error, i cant access php.ini as its a hosting server to change anythng
Error : Fatal error: require(): Failed opening required 'PHPMailer-master/PHPMailerAutoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/vhosts/evoting.freevar.com/vote/mailtest.php on line 2
PHP Code:
<?php
require 'PHPMailer-master/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';
//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 = "sample#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('sample#gmail.com', 'Evoting System');
//Set an alternative reply-to address
$mail->addReplyTo('sample#gmail.com', 'Evoting System');
//Set who the message is to be sent to
$mail->addAddress('azaz3#gmail.com', '');
//Set the subject line
$mail->Subject = 'EVoting Otp ';
//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('23bj34');
//Replace the plain text body with one created manually
$mail->AltBody = 'sddsfsd23';
//Attach an image file
// $mail->addAttachment('images/phpmailer_mini.gif');
// send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
I read related post on stackoverflow, but didnt had success yet. Thanks in advance!
You could check your current working directory, like this:
echo getcwd();
If you do so, you will see which is your active folder. The next thing you need to do is to check whether there is a PHPMailer-master folder inside your active folder (case-sensitive if you are using Linux). If it does not exist, then you will need to point to the right location instead or to create the folder and copy the file(s) into it. If it exists, check whether you have the necessary privileges and whether there is a file called PHPMailerAutoload.php inside that folder (again, case-sensitive if you are using Linux).
This isn't rocket science - the error message is very clear - you just need to use the correct path to the autoloader (and the rest of the PHPMailer files), wherever that is for your server. No need to edit php.ini.

PHPMailer is causing 504 timeout error on my Digital Ocean server using nginx

I am installing an email-sending capability on one PHP page on my server. I want to be able to specify the gmail account to send from so I am using PHPMailer. However, every time I load the page that sends the email, I get a 504 Gateway timeout error after about 30 seconds. Eventually, the email is sent (I receive it about 5 minutes later) but is this normal? It is a very basic text email.
This is my code to send the email
require '../html/lib/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';
//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 = "user#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "pw";
//Set who the message is to be sent from
$mail->setFrom('from#gmail.com', 'First Last');
//Set an alternative reply-to address
//$mail->addReplyTo('#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('recip#gmail.com', 'recip');
$mail->Subject = 'PHPMailer GMail SMTP test 2';
//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(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->Body = 'This is another plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
SMTP timeouts are pretty long (at least 5 mins). The 504 you're getting is because the timeout between nginx and your PHP cgi (I'd assume you're running FPM) is shorter, so by the time PHP generates an error, nginx has already dropped the connection, so you're getting no feedback.
It's fairly likely this is a DNS or firewall issue on your host - check out the troubleshooting docs.
Digital Ocean states in their support center how to enable services like smtp to prioritize connections over IPv4 whilst still maintaining accessible IPv6 functionality to your droplet otherwise.
You can give priority to IPv4 addresses over IPv6 so that you can
continue to send out email without disabling IPv6. You would do that
by editing the Droplet's /etc/gai.conf file and removing the comment
(#) from the following line:
Default Configuration: #precedence ::ffff:0:0/96 100
Configuration with Priority to IPv4: precedence ::ffff:0:0/96 100
This has been checked and confirmed working by me for the PHPMailer-issue where request would time out (504 Gateway Time-out) but where the mail is still eventually delivered (Ubuntu 16.04 LEMP).
This is because Ngninx drops the connection to PHP-FPM whilst the php-script is still running in the background trying to resolve an IPv6 SMTP-address, before finally moving over to IPv4 on no-success.

PHPMailer - SMTP ERROR: Password command failed when send mail from my server

I have used phpmailer() concept to send mail to users from my shared server using php script, but I'm not able to send even though everything is right in my script according to phpmailer code.
My code like this:
$message = " This is testing message from my server";
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "moorthi.mrk10#gmail.com"; // My gmail username
$mail->Password = "************"; // My Gmail Password
$mail->SetFrom("moorthi.mrk10#gmail.com");
$mail->Subject = "Test Mail from my Server";
$mail->Body = $message;
$mail->AddAddress($email);
if($mail->Send())
{
print json_encode("SUCCESS");
}
else
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
Note: I have used " GMail " as my SMTP server and SMTPSecure is " ssl " and port is "465" and username & passwords are my GMail username & password
I used VPS shared server and I kept my php script on that server.
I think there is no problem in my php script and I don't know why it doesn't work.
I got the ERROR like this.
2014-02-21 12:30:11 CLIENT -> SERVER: EHLO jkcorporates.com
2014-02-21 12:30:11 CLIENT -> SERVER: AUTH LOGIN
2014-02-21 12:30:11 CLIENT -> SERVER: bW9vcnRoaS5tcmsxMEBnbWFpbC5jb20=
2014-02-21 12:30:11 CLIENT -> SERVER: OTk0MTI0MTE0MA==
2014-02-21 12:30:11 SMTP ERROR: Password command failed: 534-5.7.14
534-5.7.14 i-_eumA> Please log in via your web browser and then try again.
534 5.7.14 54 k76sm17979938yho.18 - gsmtp
2014-02-21 12:30:11 CLIENT -> SERVER: QUIT
" The ERROR is " SMTP connect() failed.
Please give some solution for that.
Remember: I use Shared Server Name 'VPS.mydomain.com' and I want to use GMail as my SMTP server to send mail to users.
A bit late, but perhaps someone will find it useful.
Links that fix the problem (you must be logged into google account):
https://security.google.com/settings/security/activity?hl=en&pli=1
https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha
Some explanation of what happens:
This problem can be caused by either 'less secure' applications trying to use the email account (this is according to google help, not sure how they judge what is secure and what is not) OR if you are trying to login several time in a row OR if you change countries (for example use VPN, move code to different server or actually try to login from different part of the world).
To resolve I had to: (first time)
login to my account via web
view recent attempts to use the account and accept suspicious access: THIS LINK
disable the feature of blocking suspicious apps/technologies: THIS LINK
This worked the first time, but few hours later, probably because I was doing a lot of testing the problem reappeared and was not fixable using the above method. In addition I had to clear the captcha (the funny picture, which asks you to rewrite a word or a sentence when logging into any account nowadays too many times) :
after login to my account I went HERE
Clicked continue
Use this:
https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha
https://security.google.com/settings/security/activity?hl=en&pli=1
this link allow acces to google account
UPDATE 19-05-2017:
These url you must to visit from the IP address that will be send email
Solved the problem - PHPMailer - SMTP ERROR: Password command failed when send mail from my server
require_once('class.phpmailer.php');
include("class.smtp.php");
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$messageField = $_POST['message'];
$phoneField = $_POST['contactno'];
$cityField = $_POST['city'];
$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
$body .= $nameField;
try {
//$mail->Host = "mail.gmail.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->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail->Username = "xxxxx#gmail.com"; // GMAIL username
$mail->Password = "********"; // GMAIL password
$mail->AddAddress('sendto#gmail.com', 'abc');
$mail->SetFrom('xxxxxx#gmail.com', 'def');
$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($body);
$mail->Send();
echo "Message Sent OK</p>\n";
header("location: ../test.html");
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Important:
Go to google Setting and do 'less secure' applications enables. It will work.
It Worked for Me.
As others already suggested, you can enable the "less secure" applications or you can simply switch from ssl to tls:
$mailer->Host = 'tls://smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->Username = "xxx#gmail.com";
$mailer->Password = "***";
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;
When using tls there's no need to grant access for less secure applications, just make sure, IMAP is enabled.
Login to your Gmail account using the web browser.
Click on this link to enable applications to access your account: https://accounts.google.com/b/0/DisplayUnlockCaptcha
Click on Continue button to complete the step.
Now try again to send the email from your PHP script. It should work.
I face the same problem, and think that I do know why this happens.
The gmail account that I use is normally used from India, and the webserver that I use is located in The Netherlands.
Google notifies that there was a login attempt from am unusualy location and requires to login from that location via a web browser.
Furthermore I had to accept suspicious access to the gmail account via https://security.google.com/settings/security/activity
But in the end my problem is not yet solved, because I have to login to gmail from a location in The Netherlands.
I hope this will help you a little! (sorry, I do not read email replies on this email address)
Just in caser anyone ends here like me.
In may case despite having enabled unsecure access to my google account, it refused to send the email throwing an SMTP ERROR: Password command failed: 534-5.7.14.
(Solution found at https://know.mailsbestfriend.com/smtp_error_password_command_failed_5345714-1194946499.shtml)
Steps:
log into your google account
Go to https://accounts.google.com/b/0/DisplayUnlockCaptcha and click continue to enable.
Setup your phpmailer as smtp with ssl:
$mail = new PHPMailer(true);
$mail->CharSet ="utf-8";
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // or 0 for no debuggin at all
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->SMTPAuth = true;
$mail->Username = 'yourgmailaccount';
$mail->Password = 'yourpassword';
And the other $mail object properties as needed.
Hope it helps someone!!
You need to use an APP password.
Visit this link to view how to create one.
For those who are still unable to get it working, try the following in addition to the method provided by #CallMeBob.
PHP.ini
Go to C:\xampp\php , edit php.ini file with notepad.
Press CTRL+F on your keyboard, input sendmail_path on the search bar and click Find Next twice.
Right now, you should be at the [mail munction] section.
Remove the semicolon for this line:
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Add a semicolon for this line:
sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
SendMail.ini
Go to C:\xampp\sendmail, edit sendmail.ini file with notepad
Change the following:
smtp_server=smtp.gmail.com
smtp_port=465
auth_username=your-gmail-username#gmail.com
auth_password=your-gmail-password
force_sender=your-gmail-username#gmail.com
Note:
** smtp_port must tally with your those written in your php code.
** Remember to change your-gmail-username and your-gmail-password to whichever account you are using.
**
Hope this helps! :)
If you are G Suit user it can be solved by Administrator
Go to your Admin panel
Type in top search bar «Security» (Select Security with Shield icon)
Open Basic settings
Goto Less secure apps section
Press: Go to settings for less secure apps ››
And now select one of Radio Button
a) Disable access to less secure apps for all users (Recommended)
b) Allow users to manage their access to less secure apps
c) Enforce access to less secure apps for all users (Not Recommended)
Usually It does not working because of a)! And will start working immediately with c) option. b) – option will need more configuration for each user in GSuit
Hope it helps
This error is due to more security features of gmail..
Once this error is generated...Please login to your gmail account..there you can find security alert from GOOGLE..follow the mail...check on click for less secure option..Then try again phpmailer..
Your mail won't be sent online unless you complete the two-step verification for your g-mail account and use that password.
after publishing the code to the server, I am using gmail account to send a mail.
I followed the following steps to solve the issue
https://www.google.com/settings/u/1/security/lesssecureapps
https://security.google.com/settings/security/activity?hl=en&pli=1
if you added the 2 step verification then you need to remove to make it work

Categories