Related
I'm using PHPMailer in a Simple Script For Send Email's Through Gmail, and I'm getting an "Unknown Error" (At least for me!):
SMTP Error: Could not authenticate.
Error: SMTP Error: Could not
authenticate.
SMTP server error: 5.7.1 Username and
Password not accepted. Learn more at
535 5.7.1
http://mail.google.com/support/bin/answer.py?answer=14257
p38sm2467302ybk.16
I've read about Configure OpenSSL For SSL/TLS Connections, and I did it. Apache And PHP Are properly-Configured (With OpenSSL extension Running in PHP and mod_ssl running in Apache 2.2.16).
This is The PHP Script:
<?php
require_once ("PHPMailer\class.phpmailer.php");
$Correo = new PHPMailer();
$Correo->IsSMTP();
$Correo->SMTPAuth = true;
$Correo->SMTPSecure = "tls";
$Correo->Host = "smtp.gmail.com";
$Correo->Port = 587;
$Correo->UserName = "foo#gmail.com";
$Correo->Password = "gmailpassword";
$Correo->SetFrom('foo#gmail.com','De Yo');
$Correo->FromName = "From";
$Correo->AddAddress("bar#hotmail.com");
$Correo->Subject = "Prueba con PHPMailer";
$Correo->Body = "<H3>Bienvenido! Esto Funciona!</H3>";
$Correo->IsHTML (true);
if (!$Correo->Send())
{
echo "Error: $Correo->ErrorInfo";
}
else
{
echo "Message Sent!";
}
?>
The Username and Password are OK, And I tried in Thunderbird, without any problem.
I've also Used SSL Authentication and Port 465, getting the same Error.
I encountered this problem. To get it working, I had to go to myaccount.google.com -> "Sign-in & security" -> "Apps with account access", and turn "Allow less secure apps" to "ON" (near the bottom of the page).
Alternatively you can follow this direct link to these settings
this is GMail issue
read this Google Help (https://support.google.com/mail/answer/14257?p=client_login&rd=1)
Open your web browser and sign in to Gmail at http://mail.google.com/mail. If you see a word verification request, type the letters in the distorted picture and finish signing in.
Close your browser and try accessing your messages in your email client again.
If you're still having problems, visit http://www.google.com/accounts/DisplayUnlockCaptcha and sign in with your Gmail username and password. If necessary, enter the letters in the distorted picture.
Click Continue.
Restart your mail client and try accessing messages in your email client again.
Try this instead :
$Correo->Username = "foo#gmail.com";
I tested it and its working perfectly without no other change
I received the same error and in mycase it was the password. My password has special characters.
If you supply the password without escaping the special characters the error will persist.
E.g $mail->Password = " por$ch3"; is valid but will not work using the code above .
The solution should be as follows: $mail->Password = "por\$ch3";
Note the Backslash I placed before the dollar character within my password.
That should work if you have a password using special characters
Because Allow less secure apps is no longer available
The solution was to enable 2-step verification and generate app password
select mail and computer from the list then click generate
copy the code shown in the box and replace your google password with your app password it works like a charm.
I experienced the same error when configuring the WP-Mail-SMTP plugin in Wordpress.
The problem would persist even when I have 'triple checked' the settings and login credentials, and am able to log in manually using a browser.
There's a list of steps you can take to fix this.
Create a new password for the Gmail account you want to use
Enable less secure apps in Google Security settings
Use the Display Unlock Captcha page to give your app or website permission to sign in to Gmail. Click Continue or follow the instructions.
Sign in using the app or website. The smtp settings that work for me are 1) SMTP Host: smtp.gmail.com 2) SMTP port: 587 3) Encryption: TLS 4) Authentication: SMTP authentication 5) Username: example#gmail.com 6) Password: examplesecret
my solution is:
change gmail password
on gmail "Manage your google Account" > Security > Turn on 3rd party app Access
This the new step that i discover by UnlockingCaptcha that told in this site, the exact site is https://accounts.google.com/b/0/DisplayUnlockCaptcha, but maybe you want to read the former site first.
That all, hope it works for you
I received this error because of percentage signs in the password.
For me I had a special characters in my password field, and I put it like $mail->Password = " por$ch3" which work for gmail smpt server but not for other; so I just changed double quotes to single quotes and it works for me. $mail->Password = ' por$ch3';
If you still face error in sending email, with the same error message. Try this:
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
just Before the line:
$send = $mail->Send();
or in other sense, before calling the Send() Function.
Tested and Working.
The other post is correct to resolve the issue but doesn't address how to do it if the 2-step-verification is turned on. The option to allow the less secure apps is NOT available then. Here is an answer to how to do it:
a. Go to the URL of `https://myaccount.google.com/` and click `Sing-in and security`
b. Click on the app password.
You will reach a page like this,
c. Create name of your app and generate a password for the respective app.
d. Use that password acquired here inside the app.
This should resolve the issue.
I had the same issue and did all the tips including Gmail setting (e.g. less secure apps access) with no luck. But finally when I changed password to something different, for some reason it worked! FYI, the initial password did not have any special characters.
first go to https://myaccount.google.com
Select Security tab
Scroll down and select 'Less secure app access'
Turn on access
This will solve my “SMTP Error: Could not authenticate” in PHPMailer error.
I had the same issue and did all the tips with no luck. Finally when I changed password to something different, for some reason it worked! (the initial password or the new one did not have any special characters)
The correct answer:
Go to "Manage your google accounts => Security => Signing in to Google => App passwords".
Generate your maill account password there (that will be used from other device)
I had the same problem with authentication. The fix was to set up 2-step verification and create an application specific password for the device ( error messages for blocking the device will appear in your account settings->"Notifications and alerts" if you problem is the same with mine)
It was the selinux issue. I just updated the below given part in /etc/selinux/config file
SELINUX=permissive (it was SELINUX=enforcing before).
then just reboot the system by giving
reboot
Now the mail goes without any hassle.
Configuration
From Email Address : [noreply#yourdomain.com]
From Name : [your domain name]
SMTP Host : smtp.gmail.com
Type of Encryption : SSL
SMTP Port : 465
SMTP Authentication : YES
Username : [your mail id]
Password : [your password]
SMTP Error: could not authenticate
I had the same problem. The following troubleshooting steps helped me.
I turned off two-factor authentication in my gmail account.
I allowed less secure apps to access my gmail account. To get it working, I had to go to myaccount.google.com -> Sign-in & security -> Apps with account access, and turn Allow less secure apps to ON (near the bottom of the page).
At this step, when I tried to register a user, I would get the same error. Google would sent me a warning message that someone has my password and the login was blocked.
Gmail will then provide you with options. You either click whether the activity was yours or not yours. Click the option that the activity was yours.
Try registration again. It should now work.
There is no issue with your code.
Follow below two simple steps to send emails from phpmailer.
You have to disable 2-step verification setting for google account if you have enabled.
Turn ON allow access to less secure app.
I had the same issue which was fixed following the instructions below
Test enabling “Access for less secure apps” (which just means the client/app doesn’t use OAuth 2.0 - https://oauth.net/2/) for the account you are trying to access. It's found in the account settings on the Security tab, Account permissions (not available to accounts with 2-step verification enabled): https://support.google.com/accounts/answer/6010255?hl=en
original link for the answer: https://support.google.com/mail/thread/5621336?msgid=6292199
SOLVED
In 2022 the reason i found that
You need to do two-factor authentication in your mail service.
I am using Mail.ru https://account.mail.ru/user/2-step-auth/passwords/
Source:
https://www.courier.com/error-solutions/smtp-error-could-not-authenticate/
After moving to a new Linux Hosting plan with cPanel (previously I had classic Linux Hosting plan with no cPanel), my old PHPMailer no longer functions. I keep getting SMTP() Failed, and in the log files, *
"PHP Warning: stream_socket_enable_crypto(): SSL operation failed
with code 1. OpenSSL Error messages: error:14090086:SSL
routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in
[file name]"
I browsed Stack Overflow for some time, and have tried a variety of solutions, be it changing a port number, host name, or SSL/TSL options, nothing seems to resolve the issue. I am rather lost, considering, previously it wasn't even necessary to specify such details. I've contacted GoDaddy's Tech Support, they've recommended using post 25 and host relay-hosting.secureserver.net, which alas, didn't make any difference.
I am using PHPMailer developed by Marcus Bointon, https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
If it helps, my PHPMailer details are:
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = false;
That's me! GoDaddy is a frequent pain point... They block all outbound SMTP except via their own servers. The certificate validation is due to them presenting an invalid certificate, and you're probably seeing it now because your upgrade also moved you to PHP 5.6+, which validates certificates by default. Since this is done inside GoDaddy's network, I suggest you try disabling TLS altogether:
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = false;
Talking to their relay-hosting.secureserver.net host is definitely the right way to go.
I'm trying to send an email when a form is submitted. I'm using PHPMailer to send the mail using the below configuration.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.example.in';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'user#example.in';
$mail->Password = 'password';
$mail->setFrom("user#example.in" , "User");
$mail->addAddress('receiver#example.in', 'Receiver');
$mail->addBCC('anotheruser#somedomain.com', 'Another user');
$mail->AddReplyTo('user#example.in', 'User');
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $message;
if($mail->send())
echo "Your request has been received. We will soon contact you.";
else echo "Unable to send your request. Please try again";
This works fine in localhost. But, when I deploy it to my server (example.in) I get the below exception.
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
--EDIT--
I tried connecting to the SMTP server using telnet command, but I'm unable to add the recipient. I get the below error?
Last login: Fri Sep 16 11:08:06 on ttys000
admin:~ admin$ telnet mail.example.in 25
Trying 111.91.153.112...
Connected to mail.example.in.
Escape character is '^]'.
220 fbs-ho-mailserver.example.in ESMTP Service (Lotus Domino Release 8.5.3FP3) ready at Fri, 16 Sep 2016 11:36:01 +0530
HELO example.in
250 fbs-ho-mailserver.example.in Hello example.in ([111.91.127.222]), pleased to meet you
MAIL from: marketing#example.in
250 marketing#example.in... Sender OK
RCPT to: john.hh#gmail.com
554 Relay rejected for policy reasons.
-- EDIT --
I was able to setup this account in outlook. I'm really confused what's happening.
Your error says:
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
So let's look at https://github.com/PHPMailer/PHPMailer/wiki/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).
Some techniques to diagnose the source of this error are discussed
below.
GoDaddy
Popular US hosting provider GoDaddy imposes very strict (to the point
of becoming almost useless) constraints on sending email. They block
outbound SMTP to ports 25, 465 and 587 to all servers except their
own. This problem is the subject of many frustrating questions on
Stack Overflow. If you find your script works on your local machine,
but not when you upload it to GoDaddy, this will be what's happening
to you. The solution is extremely poorly documented by GoDaddy: you
must send through their servers, and also disable all security
features, username and password (great, huh?!), giving you this config
for PHPMailer:
$mail->isSMTP();
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = false;
GoDaddy also refuses to send with a From address belonging to any aol,
gmail, yahoo, hotmail, live, aim, or msn domain (see their docs). This
is because all those domains deploy SPF and DKIM anti-forgery
measures, and faking your from address is forgery.
You may find it easier to switch to a more enlightened hosting
provider.
The problem - rather the two unrelated problems - that you're experiencing are quite straightforward:
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed.
and you have verified that the server is indeed accepting connections:
I tried connecting to the SMTP server using telnet command
Last login: Fri Sep 16 11:08:06 on ttys000
admin:~ admin$ telnet mail.example.in 25
Trying 111.91.153.112...
Connected to mail.example.in.
Your script cannot connect to the SMTP server when run from its production server.
The likely cause is that the production server has a firewall that, to avoid abuse, prevents any connection to the outside. The server can serve Web requests, but no more.
If your test had verified that port 25 was not responding, then (after checking that the host address was correct) you could have tried telnet mail.example.in 587 instead. If that worked, it could have meant that the server is not accepting insecure connections (port 25) but is accepting secure connections. With PHPMailer you could then have tried activating secure connection:
$mail->SMTPSecure = 'tls';
or
$mail->SMTPSecure = 'ssl';
If that does not work, you might still have a firewall issue; or you might need to look at phpinfo() and verify you do have OpenSSL support available in PHP.
What you need to do
ask the IT people that maintain the production server to open the firewall;
more promisingly, ask them how to send emails from that server. Chances are that you need to use the mail() function, or use localhost or 127.0.0.1 as SMTP server. Then the emails will go out through your production server's service network.
They might tell you that port 25 is not allowed, but port (say) 465 or 567 would be allowed. You will have to update your configuration and/or add TLS/SSL accordingly (see above).
or you might be allowed to connect to a third party SMTP server of which you will have to supply the IP address, to allow the IT guys to open a suitable firewall window. Then the emails will go out through the third party server.
The second problem (possibly NOT a problem)
250 marketing#example.in... Sender OK
RCPT to: john.hh#gmail.com
554 Relay rejected for policy reasons
Also to avoid abuse, SMTP Servers will not let everyone connect and send emails, but only their own customers. I see that in the PHPMailer configuration you specified an user and a password. In the telnet session you did not. So it might well be that PHPmailer could send, but not connect, while your telnet can connect, but not send.
Once you solve the connection problem, your authentication problem will either be solved or will have gone away (because you'll be using a different server supplied to you by the IT guys, for example localhost).
The third problem (might never arise)
A third way of abusing services is over-use - sending too many emails to too many people. Verify with the IT guys what the acceptable policies are for sending emails.
Problems, problems
Other things to look into are the credibility of the source (you might want to send emails on behalf of some domain which has not designated your SMTP server of choice as permitted sender), and the confidentiality of the data (even with TLS/SSL connections, if you are given localhost as the SMTP server, your IT guys will have complete, unfettered, undetectable access to any email you send. You might, or might not, be okay with that).
1st quote (from ibm community):
john.hh#gmail.com was a member of a group (Reject) that was listed in
gmail.com's "Deny messages intended for the following internet
addresses" field (in the destination server's Domino Configuration
document's Router/SMTP, Restrictions and Controls, SMTP Inbound
Controls tab's "Inbound Intended Recipient's Controls" section).
Removing Mary's hierarchical name (Mary Jones/ABC) from the members
list in the Reject (group) document allows Mary to receive messages
from the Internet.
2nd quote:
Most mail servers, to prevent them being used as anonymous spam
relays, are configured only to relay mail from certain hosts.
It's a bad idea to use your own SMTP.
Depending of what you have to do with it, you have some great chances to have your emails blocked in some ways or marked as SPAM. And you will have to spend some times to keep your server up to date.
Use online services that are white-listed for every provider and that expose API to send your transactionnal mails :
https://www.mailjet.com/
http://mailchimp.com/
...
They often propose a free account for small volume (under 2000 emails per days).
Using the API is quite trivial and can be put in place in some minutes (ex : https://dev.mailjet.com/)
Ask to your hosting provider if smtp is enabled on not on that server.I had same issue before for smtp and curl both.Contact hosting provider.
What's the phpmailer version?
You might forget to add this line
$mail->IsSMTP(); // use SMTP
after you initiate $mail.
Have a try.
So you have a GoDaddy server and you want to send email through your newly acquired SMTP Relay. Well “you can’t” is probably what you are going to find if you do some poking around Google.
If you poke around a little more you will probably find GoDaddy’s official solution is to use their internal mail relay. This is not ideal, because of arbitrary barriers they have set, such as this one:
Our dedicated servers have an outbound email limit of 1000 per day. If
you need to send more than 1000 emails per day, please call Customer
Support or open a support ticket to request a higher limit.
Quick and Working Solution: Get SMTP Relay that supports sending emails using HTTP API.
My Suggestion: (A simple and effective working solution, I am using personally)
Signup for an account in Sendgrid. Sendgrid provides free 12k emails per month, and I think that should suffice any basic needs.
First Step: Sendgrid will ask you to add few DNS records to authenticate you for sending emails on behalf of that host. So do it.
Second Step Step: Configure White Lables https://sendgrid.com/docs/User_Guide/Settings/Whitelabel/index.html (This is not important, but it will give professional look to the emails you are sending)
Third Step: Generate API keys
You can manage your API Keys from the SendGrid Customer Portal. Additionally, you can manage your API keys via the API itself.
Forth Step: Go ahead and write our codes.
Sending mails using PHP on sendgrid using this library available on github is easy:
// using SendGrid's PHP Library
// https://github.com/sendgrid/sendgrid-php
require 'vendor/autoload.php';
$sendgrid = new SendGrid("SENDGRID_APIKEY");
$email = new SendGrid\Email();
$email->addTo("test#sendgrid.com")
->setFrom("you#youremail.com")
->setSubject("Sending with SendGrid is Fun")
->setHtml("and easy to do anywhere, even with PHP");
$sendgrid->send($email);
How this works
GoDaddy blocks port 25, 2525, 587 and 485 which are ideal ports for SMTP. So we are using the port 80/443 and requesting the sengrid's API to send the email on our behalf behalf using port 80, and yes, we fooled GoDaddy.
I think within a few steps we can get this working properly.
In your Godaddy/Hostgator Cpanel > Select MX records > Select Domain in question > Select "Remote Mail Exchanger"
You can test mail delivery at this time, issue may be resolved.
I am sure it is set up right but trust me when it comes to good ole Cpanel, PhpMailer and Godaddy/Hostgator. It's a place where common sense makes no sense.
replace:
$mail->Host = 'mail.example.in';
with:
$mail->Host = 'localhost';
I believe this should resolve any issues assuming things like login credentials are valid.
I faced similar timeout issue , the reason was my hosting provider (Linode) .
TLDR; SMTP restrictions are in place by default for Linode on accounts created after November 5th 2019. You'll need to configure rDNS for your instance and open a Support ticket confirming CAN-SPAM compliance, and Support should lift the restrictions pretty quickly. I guess many people face this issue because of smtp restrictions
Timeout error is typical of packets dropped, so could be firewall issues between web server and SMTP server.
If you got ssh access to your server, you can try to connect to smtp using:
telnet <smtp server> 25
On timeout error even with telnet, you're quite sure that the problem is on network/firewall side.
You need to change some setting in your php.ini :
upload_max_filesize = 2M
;or whatever size you want
max_execution_time = 60
; also, higher if you must - sets the maximum time in seconds.
Were your PHP.ini is depends on your enviroment, more information:
reference
OR
Place this at the top of your PHP script and let your script loose
ini_set('max_execution_time', 600); //600 seconds = 10 minutes or set your time
I got your point. Same issue happen with me , I have just add one line and its working for me. please use ,
$mail->SMTPSecure = 'ssl';
and configure your setting according to ssl . Also properly check mail configure at your serve end.
I am trying to setup a php page to automatically send a verification email. I would like to use the gmail smtp servers, and everywhere I've looked suggests to use PHPMailer. I installed it and used the following example code:
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once ("incl\PHPMailer\PHPMailerAutoload.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom('myemail#gmail.com','Me');
$mail->AddAddress("ToAddress#gmail.com");
$mail->Subject = "Verify your email";
$mail->Body = "Thank you for signing up. Please verify your email using the link below:";
$mail->IsHTML (true);
if($mail->Send()){
echo "Success";
}else{
echo "Error";
}
When trying to access the page via Firefox, the page will load for a few minutes, then give this error:
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
The server is Windows Server 2008 R2 Standard running IIS 7.5 and PHP Version 5.5.8. I can access all other pages without issues, but trying to call $mail->Send() seems to be timing out or something. I know this because I commented every line and slowly added pieces back in and $mail->Send() is the line that causes the behavior.
My Google abilities are failing me here as I simply cannot figure out how to make this work. Any ideas on what might be wrong?
Update
I opened the server log then tried loading the page again, but no new errors were added to the log. However, I noticed the following errors from today in System32\LogFiles\httperr1.log
2014-10-27 06:29:21 1.161.23.122 3148 212.83.145.123 80 HTTP/1.0 CONNECT mx2.mail2000.com.tw:25 400 - URL -
2014-10-27 10:10:12 95.183.244.244 33553 212.83.145.123 80 HTTP/1.1 GET / 400 - Hostname -
2014-10-27 11:25:25 207.38.185.197 51157 212.83.145.123 80 HTTP/1.1 GET /tmUnblock.cgi 400 - Hostname -
2014-10-27 12:46:21 1.168.221.158 7952 212.83.145.123 80 - - - - - Timer_ConnectionIdle -
UPDATE 2
I am positive that my gmail account details are correct and have tested sending from it using Thunderbird on the server. When trying to sent without secured methods, as suggested in this comment I get this error:
MAIL FROM command failed,550,5.7.3 Requested action aborted; user not authenticated
My PHP Mailer version is 5.2.9 and I've now also tried the following:
Using \\ in file paths instead of \
No change
Including class.phpmailer.php instead of PHPMailerAutoload.php
Fatal error: Class 'SMTP' not found in C:\inetpub\wwwroot\incl\PHPMailer\class.phpmailer.php on line 1195
Using ssl over port 465
SMTP connect() failed
Sending with a hotmail address over port 25 with $mail->SMTPAuth = false;
MAIL FROM command failed,550,5.7.3 Requested action aborted; user not authenticated
Update 3
After reloading the problem page, I checked through the Event Viewer and saw a new entry in Windows Logs -> System:
PHP Error : syntax error, unexpected BOOL_TRUE in C:\PHP\php.ini on line 101
That line is:
php_flag display_errors on
Looks like you're dealing with bunch of issues and here is a checklist: you're having issues with ssl, the actual mail software and credentials. It became an issue of 3 from one main issue and I'm guessing you don't either have your credentials typed in correctly or your open ssl isn't setup and you're also having issues using the mail software.
535 535 5.7.3 Authentication Unsuccessful means auth is unsuccessful, check your credentials username and password.
550 error code, it means that the receiving system could not deliver your email to the user to whom it was addressed because the mailbox is unavailable.
There are plenty of other solutions to resolve your issue. Why don't you try something more simple instead of using the autoload PHPMailerAutoload.php. Create a new file and place the code below, create a message body (test_message.html) and call it from the browser to test gmail smtp with the gmail credentials. This is a snippet from PHPMailer on how to use it with gmail smtp and it shouldn't go wrong if you have everything filled in correctly.
<?php
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('test_message.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
If you're getting user auth error, then your credentials aren't typed in
correctly. If that's not the issue, you'll get an ssl error if you're not using the right port.
If there are software issues with include, you should try something better.
I was debating this morning if I should should PHPMailer, ended up using Swiftmailer which worked right after I installed it with PHPComposer.
My Internal mail server is extremely picky and has ton of firewall settings and rules on hardware and software level and I was surprised it didn't give me any problems; the emails were being sent right away without any problems on port 587.
require_once 'vendor/swiftmailer/swiftmailer/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('mail.hellog***.com', 587)
->setUsername('apache#hellog***.com')
->setPassword('apa******')
;
This is what you need for gmail:
$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername($this->username)
->setPassword($this->password);
$this->mailer = Swift_Mailer::newInstance($transporter);
If none of what you like, you can try phpgmailer:
https://github.com/GregDracoulis/LectureBank/tree/master/phpgmailer
here is an example on how to use it:
http://www.vulgarisoverip.com/2006/10/13/update-send-email-with-php-and-gmail-hosted-for-your-domain/
this class is dedicated for gmail use.
If all your problems and issues relate to openSSL, you should follow the steps below:
1. Make sure the OpenSSL module DLL file is included in the PHP installation:
C:\user>dir \local\php\ext\php_openssl.dll
Directory of C:\local\php\ext
php_openssl.dll
2. Create the PHP configuration file, \local\php\php.ini, if it does not exist:
C:\user>copy \local\php\php.ini-production \local\php\php.ini
1 file(s) copied.
3. Enable the OpenSSL module by editing the configuration file with a text editor. You need to remove the comment maker (;) on two configuration lines:
...
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "ext"
...
extension=php_openssl.dll
...
That’s it. You should be all set setting up openSSL on your Windows IIS WebServer. Follow these options and steps, it could be your issue.
This problem is slashes ( \ )
Edit
require_once ("incl\\PHPMailer\\PHPMailerAutoload.php");
I have tried with you code, and i have only changed this line :
require_once ("incl\PHPMailer\PHPMailerAutoload.php");
by this one :
require_once ("incl\PHPMailer\class.phpmailer.php");
And it was worked successfully.
Also you should keep attention about your PHPMailer version and your PHP version.
Have you tried to send an email with not secured method ?
Are you sure that your google credentials are valid ?
UPDATED
From the command line of your server, what is the output of this command and how much time that it takes to respond
telnet smtp.gmail.com 587
I wonder if you are running into the same issue that this question ran into. Google actively frowns on people using Gmail as their own personal SMTP server.
http://www.labnol.org/internet/email/gmail-daily-limit-sending-bulk-email/2191/
Google also limits the number of email messages that you can send through your Gmail account in a day. If you exceed the daily quota, Google may temporarily disable your Gmail account without any warnings and you may have to wait for up to 24 hours before you can regain access to your Gmail mailbox.
I could see an error like that potentially producing a 500 error.
From the comments to the question, it seems pretty clear that this issue is OpenSSL related.
You mentioned that you already have extension=php_openssl.dll uncommented, but phpinfo() does show it not beeing loaded.
Some thoughts:
Have you restarted the webserver, after uncommenting the openssl php module in your php.ini?
This is the really obvious one, but still worth mentioning. The php.ini is loaded on start-up, and not doing a service restart is simply a common mistake.
Check the value of Loaded Configuration File in phpinfo()
If this is not the same location than the php.ini which you have edited, it also becomes clear why it has not been loaded. Also following lines from phpinfo() might be worth checking.
Configuration File (php.ini) Path
Loaded Configuration File
Scan this dir for additional .ini files
There are other things too which can be done like uninstalling and reinstalling OpenSSL, but it might be good to check these more obvious things first.
Further thoughts:
Checking the error logs
For php the path can (again) be found in your phpinfo(). Look for the value of error_log and check the latest entries, right after you tried to reload your page.
Also you may wanna check the log files of IIS. If you don't know the path look in the IIS Manager, select the computer on the left pane, and in the middle pane, go under "logging" in the IIS area. Usually these will point to something like C:\inetpub\logs\LogFiles or C:\Windows\System32\LogFiles with a subfolder like W3SVC[x] where [x] is the website ID. (Will be just 1 if you only have one page, otherwise check the top level websites folder in IIS Manager, view the lists of sites in the right hand pane, it's the App ID)
Setting Up and Configuring IIS
Start by going to the ServerManager on you Server. Most often this is the server you’ll be running your php website on. Go to Add features in the Features summary section. Check SMTP Services in the Add Features Wizard and hit install. Now wait for the installation to finish. Open IIS6 (or IIS7) Manager under Administrative Tools -> Internet Information Services 6.0 in the Start Menu, the configuration of SMTP makes use of the management console from IIS6. Under [SMTP Virtual Server], click your right mouse button and select properties from the context menu. Go to the Access Tab and hit the Relay button.Add a new entry to the list by clicking the Add button and enter 127.0.0.1 (or your web server's IP) in the single computer entry field. Hit Ok two times to apply the new settings.
Configuring Php
To make Php send emails you need to make some configurations. Basically there are two options.
In case you’re only going to use the SMTP server in a single project and do not want it to interfere with other projects, you can set the configuration from within your php code by adding the following lines anywhere before sending the email:
ini_set('SMTP','localhost' );
ini_set('sendmail_from', 'administrator#YourWebsite.com');
Another way, to make global use of these settings, add the next lines to your php.ini file.
[mail function]
SMTP = localhost
sendmail_from = me#example.com
Make sure you restart your server after making these changes, to be sure they’re loaded.
This solution is provided here http://geekswithblogs.net/tkokke/archive/2009/05/31/sending-email-from-php-on-windows-using-iis.aspx on how to set up IIS and php and was provided as answer to this question 500 error when trying to use the MAIL function
Try to use SSL encryption with a port 465:
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
Warning: require_once(mailer/PHPMailerAutoload.php): failed to open
stream: No such file or directory in....
This is the way I had my file routed
Before:
require_once "mailer/PHPMailerAutoload.php";
After:
require_once "../mailer/PHPMailerAutoload.php";
...Same process you would normally do to link an img as background using css.
I'm using PHPMailer in a Simple Script For Send Email's Through Gmail, and I'm getting an "Unknown Error" (At least for me!):
SMTP Error: Could not authenticate.
Error: SMTP Error: Could not
authenticate.
SMTP server error: 5.7.1 Username and
Password not accepted. Learn more at
535 5.7.1
http://mail.google.com/support/bin/answer.py?answer=14257
p38sm2467302ybk.16
I've read about Configure OpenSSL For SSL/TLS Connections, and I did it. Apache And PHP Are properly-Configured (With OpenSSL extension Running in PHP and mod_ssl running in Apache 2.2.16).
This is The PHP Script:
<?php
require_once ("PHPMailer\class.phpmailer.php");
$Correo = new PHPMailer();
$Correo->IsSMTP();
$Correo->SMTPAuth = true;
$Correo->SMTPSecure = "tls";
$Correo->Host = "smtp.gmail.com";
$Correo->Port = 587;
$Correo->UserName = "foo#gmail.com";
$Correo->Password = "gmailpassword";
$Correo->SetFrom('foo#gmail.com','De Yo');
$Correo->FromName = "From";
$Correo->AddAddress("bar#hotmail.com");
$Correo->Subject = "Prueba con PHPMailer";
$Correo->Body = "<H3>Bienvenido! Esto Funciona!</H3>";
$Correo->IsHTML (true);
if (!$Correo->Send())
{
echo "Error: $Correo->ErrorInfo";
}
else
{
echo "Message Sent!";
}
?>
The Username and Password are OK, And I tried in Thunderbird, without any problem.
I've also Used SSL Authentication and Port 465, getting the same Error.
I encountered this problem. To get it working, I had to go to myaccount.google.com -> "Sign-in & security" -> "Apps with account access", and turn "Allow less secure apps" to "ON" (near the bottom of the page).
Alternatively you can follow this direct link to these settings
this is GMail issue
read this Google Help (https://support.google.com/mail/answer/14257?p=client_login&rd=1)
Open your web browser and sign in to Gmail at http://mail.google.com/mail. If you see a word verification request, type the letters in the distorted picture and finish signing in.
Close your browser and try accessing your messages in your email client again.
If you're still having problems, visit http://www.google.com/accounts/DisplayUnlockCaptcha and sign in with your Gmail username and password. If necessary, enter the letters in the distorted picture.
Click Continue.
Restart your mail client and try accessing messages in your email client again.
Try this instead :
$Correo->Username = "foo#gmail.com";
I tested it and its working perfectly without no other change
I received the same error and in mycase it was the password. My password has special characters.
If you supply the password without escaping the special characters the error will persist.
E.g $mail->Password = " por$ch3"; is valid but will not work using the code above .
The solution should be as follows: $mail->Password = "por\$ch3";
Note the Backslash I placed before the dollar character within my password.
That should work if you have a password using special characters
Because Allow less secure apps is no longer available
The solution was to enable 2-step verification and generate app password
select mail and computer from the list then click generate
copy the code shown in the box and replace your google password with your app password it works like a charm.
I experienced the same error when configuring the WP-Mail-SMTP plugin in Wordpress.
The problem would persist even when I have 'triple checked' the settings and login credentials, and am able to log in manually using a browser.
There's a list of steps you can take to fix this.
Create a new password for the Gmail account you want to use
Enable less secure apps in Google Security settings
Use the Display Unlock Captcha page to give your app or website permission to sign in to Gmail. Click Continue or follow the instructions.
Sign in using the app or website. The smtp settings that work for me are 1) SMTP Host: smtp.gmail.com 2) SMTP port: 587 3) Encryption: TLS 4) Authentication: SMTP authentication 5) Username: example#gmail.com 6) Password: examplesecret
my solution is:
change gmail password
on gmail "Manage your google Account" > Security > Turn on 3rd party app Access
This the new step that i discover by UnlockingCaptcha that told in this site, the exact site is https://accounts.google.com/b/0/DisplayUnlockCaptcha, but maybe you want to read the former site first.
That all, hope it works for you
I received this error because of percentage signs in the password.
For me I had a special characters in my password field, and I put it like $mail->Password = " por$ch3" which work for gmail smpt server but not for other; so I just changed double quotes to single quotes and it works for me. $mail->Password = ' por$ch3';
If you still face error in sending email, with the same error message. Try this:
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
just Before the line:
$send = $mail->Send();
or in other sense, before calling the Send() Function.
Tested and Working.
The other post is correct to resolve the issue but doesn't address how to do it if the 2-step-verification is turned on. The option to allow the less secure apps is NOT available then. Here is an answer to how to do it:
a. Go to the URL of `https://myaccount.google.com/` and click `Sing-in and security`
b. Click on the app password.
You will reach a page like this,
c. Create name of your app and generate a password for the respective app.
d. Use that password acquired here inside the app.
This should resolve the issue.
I had the same issue and did all the tips including Gmail setting (e.g. less secure apps access) with no luck. But finally when I changed password to something different, for some reason it worked! FYI, the initial password did not have any special characters.
first go to https://myaccount.google.com
Select Security tab
Scroll down and select 'Less secure app access'
Turn on access
This will solve my “SMTP Error: Could not authenticate” in PHPMailer error.
I had the same issue and did all the tips with no luck. Finally when I changed password to something different, for some reason it worked! (the initial password or the new one did not have any special characters)
The correct answer:
Go to "Manage your google accounts => Security => Signing in to Google => App passwords".
Generate your maill account password there (that will be used from other device)
I had the same problem with authentication. The fix was to set up 2-step verification and create an application specific password for the device ( error messages for blocking the device will appear in your account settings->"Notifications and alerts" if you problem is the same with mine)
It was the selinux issue. I just updated the below given part in /etc/selinux/config file
SELINUX=permissive (it was SELINUX=enforcing before).
then just reboot the system by giving
reboot
Now the mail goes without any hassle.
Configuration
From Email Address : [noreply#yourdomain.com]
From Name : [your domain name]
SMTP Host : smtp.gmail.com
Type of Encryption : SSL
SMTP Port : 465
SMTP Authentication : YES
Username : [your mail id]
Password : [your password]
SMTP Error: could not authenticate
I had the same problem. The following troubleshooting steps helped me.
I turned off two-factor authentication in my gmail account.
I allowed less secure apps to access my gmail account. To get it working, I had to go to myaccount.google.com -> Sign-in & security -> Apps with account access, and turn Allow less secure apps to ON (near the bottom of the page).
At this step, when I tried to register a user, I would get the same error. Google would sent me a warning message that someone has my password and the login was blocked.
Gmail will then provide you with options. You either click whether the activity was yours or not yours. Click the option that the activity was yours.
Try registration again. It should now work.
There is no issue with your code.
Follow below two simple steps to send emails from phpmailer.
You have to disable 2-step verification setting for google account if you have enabled.
Turn ON allow access to less secure app.
I had the same issue which was fixed following the instructions below
Test enabling “Access for less secure apps” (which just means the client/app doesn’t use OAuth 2.0 - https://oauth.net/2/) for the account you are trying to access. It's found in the account settings on the Security tab, Account permissions (not available to accounts with 2-step verification enabled): https://support.google.com/accounts/answer/6010255?hl=en
original link for the answer: https://support.google.com/mail/thread/5621336?msgid=6292199
SOLVED
In 2022 the reason i found that
You need to do two-factor authentication in your mail service.
I am using Mail.ru https://account.mail.ru/user/2-step-auth/passwords/
Source:
https://www.courier.com/error-solutions/smtp-error-could-not-authenticate/