phpmailer hangs on send() [duplicate] - php

I had successfully setup a web app using WAMPSERVER on a desktop used by a few people internally, this used PHPMailer to an internal SMTP server without encryption or authentication and it worked.
That desktop crashed and I've migrated to a "new" desktop. I had an SVN setup so I was even using most of the same files and config. One difference which might matter is that the old desktop was 64-bit and the new is 32-bit. This means I'm using different versions of WAMPSERVER.
The mailer just hangs. I don't get a PHP error or a PHP timeout. I just never reach the end of my script. The crazy part about this is that it works with authentication, ssl, and gmail. It just won't work with the extra simple case I need.
This works:
<?php
require('class.phpmailer.php');
$mail=new PHPMailer();
$mail->ISSMTP();
$mail->Host='smtp.gmail.com';
$mail->Subject='test subj';
$mail->Body='the body email test';
$mail->SMTPDebug = 1; // 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->Username = "myemail#gmail.com"; // GMAIL username
$mail->Password = "mypassword"; // GMAIL password
$mail->AddAddress('toemail#supersecret.com', 'John Doe');
$mail->SetFrom('something#gmail.com', 'First Last');
$mail->Send();
?>
this used to, but now does not:
<?php
require('class.phpmailer.php');
$mail=new PHPMailer();
$mail->ISSMTP();
$mail->Host='smtp.internal.com';
$mail->Subject='test subj';
$mail->Body='the body email test';
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->AddAddress('myaddress#somewhere.com', 'John Doe');
$mail->SetFrom('someaddress#mightbereal.com', 'First Last');
$mail->Send();
?>
The only thing I get from debug is
CLIENT -> SMTP: EHLO thedesktophostname
No errors display on the page and nothing in the apache log, where I normally get PHP errors, if they don't display.
I can telnet to the host from the desktop on port 25 and even type in the EHLO command and get a good response from the server.
I don't remember having this issue before, although it's possibly I've already solved it once. I couldn't find anything that helped here or on The Google.
Please help. Thanks.

Hijacking the post to say i had the same issue but had set the port to 465 without setting SMTPSecure to 'ssl' in the example its set TLS by default

If you have a server hosted in Hostgator (shared hosting), this is what solved for me:
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
(even though the official example in PHPMailer suggests using ENCRYPTION_STARTTLS)

sadly this probably won't ever help anyone else who has this same problem, but I was able to get everything working by just changing the port to 465.

Eventually found solution for my configuration.
Just add ssl:// to smtp.google.com
$mail->Host = 'ssl://smtp.gmail.com';

I had the same issue. Nothing displays after the send method.
I realized that the encryption was wrong, I did use SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
// Enable TLS encryption, `PHPMailer::ENCRYPTION_SMTPS` also accepted

Related

Could not authenticate Google account through PHPMailer

I'm using PHPMailer in a Simple Script For Send Email's Through Gmail, and I'm getting an this error (I'm sure that the email and password combination is correct):
!-- 2020-12-02 14:13:16 CLIENT -> SERVER: EHLO localhost
2020-12-02 14:13:16 CLIENT -> SERVER: STARTTLS
2020-12-02 14:13:17 CLIENT -> SERVER: EHLO localhost
SMTP Error: Could not authenticate.
2020-12-02 14:13:17 CLIENT -> SERVER: QUIT
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Allow less secure apps is ON
This is the way I implement the phpMailer
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require './mailer/autoload.php';
$msg = "";
$mail = new PHPMailer();
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_CLIENT;
$mail->isSMTP();
$mail->Host = 'smtp.google.com';
$mail->SMTPAuth = true;
$mail->Username = '********#gmail.com';
$mail->Password = '********';
$mail->SMTPSecure = "tls";
$mail->Port = 25;
$mail->CharSet= 'UTF-8';
$mail->setFrom('*******#gmail.com', 'Mailer');
$mail->addAddress($_POST["mail"]);
$mail->isHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = '<h2>E-mail</h2>';
$mail->AltBody = $_POST["content"];
$mail->send();
} catch (Exception $e) {
$msg = "An Error has Ocurr";
}
How can I solve this issue?
SMTP port 25 is not used with TLS, you should use port 587 for TLS/STARTTLS or 456 for SSL. And it seems that you've also used the incorrect host URL, which should be smtp.google.com. The required configuration is stated here: https://support.google.com/mail/answer/7126229.
So you should probably change:
...
$mail->Host = 'smtp.google.com';
$mail->Port = 25;
...
To:
...
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
...
Depending on your situation, it might not be best practice to use Google's default SMTP. It is OK for personal use, but if you want to send more automated emails, you should look for other options. The default Google SMTP is strictly rate limited for example.
First of all, Google SMTP Relay yields a little more configurability if you need it.
When you really want to send automated or bulk emails, you should look into a provider specifically for this. It is not what the Google SMTP servers are made for and you will quickly notice by emails not being sent out or delivered properly.
It would really help if you actually read the error message and took the advice it gives you, by reading the guide it links to.
First of all, you're only showing client debug output, so you can't see what the server is saying, and so you can't tell what's going on, as the docs say. Do this:
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
Without seeing what that says, you're working blind.
That said, you get kicked out immediately after EHLO, and the only thing you have said is:
2020-12-02 14:13:17 CLIENT -> SERVER: EHLO localhost
Unfortunately this is untrue, and I'd guess that gmail is calling you out on it. localhost is by definition not an internet routable address, and any reverse lookup on the name will never match the IP you are connecting from, which is not localhost. If that is happening automatically, override it manually by setting the client host explicitly:
$mail->Helo = 'myhost.example.com';
While RFCs mandate port 587 for SMTP+STARTTLS, gmail supports it on port 25 too, and you can see that your STARTTLS command is working successfully, so that's not the problem here.
use port 587
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = '********#gmail.com';
$mail->Password = '********';

PHPMailer cannot send email

My project contains a function to send email, being PHPMailer. It runs well to send email from my localhost server, but it stopped sending email today, and now it shows this error message
SMTP Error: Could not connect to SMTP host.
I added this code $mail->SMTPDebug = 1; to view debug errors and is now showing me this message:
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: The requested name is valid, but no data of the requested type was found. (0) SMTP Error: Could not connect to SMTP host
I already have enabled extension=php_openssl.dll in php.ini.
This is my code:
$mail = new PHPMailer();
$mail->SMTPSecure = 'ssl';
$mail->IsSMTP();
$mail->Username = "myemail#gmail.com"; // your GMail user name
$mail->Password = "password";
$mail->AddAddress($email); // recipients email
$mail->FromName = "username"; // readable name
$mail->IsHTML(true);
$mail->Subject = "title";
$mail->Body = " Message";
$mail->SMTPDebug = 1;
$mail->Host = "ssl://smtp.gmail.com"; // GMail
$mail->Port = 465;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
if($mail->Send()){
} else {
}
thanks
This is commonly reported as a PHPMailer problem (and there are many duplicates of this question), but it's almost always down to local DNS failure, firewall blocking or other network issue on your local network.
First, make sure you are using the latest PHPMailer.
Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead:
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
or more succinctly:
$mail->Host = 'tls://smtp.gmail.com:587';
You can your connectivity this by running some commands on your server (you will need dnsutils and telnet packages installed). First check DNS is working:
dig +short smtp.gmail.com
You will get something like this if your DNS is working:
gmail-smtp-msa.l.google.com.
173.194.67.108
173.194.67.109
Next try to telnet to the host on the port you need:
telnet smtp.gmail.com 587
This should give you something like this:
Trying 173.194.67.109...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP ex2sm16805587wjd.30 - gsmtp
(Enter quit to get out of that).
If either of these fail, PHPMailer will not work either. So go fix your network, then try again. If you are not in control of your own firewall or DNS, you probably need to raise a support ticket with your ISP to fix this. If they won't fix it, you need to replace your ISP.
Back in PHPMailer, you can get lower-level feedback on the connection by setting:
$mail->SMTPDebug = 4;
A simple google search revealed this forum - http://forums.devshed.com/php-development-5/unable-to-find-the-socket-transport-ssl-667689.html

PHP Mailer Send a email [duplicate]

I've used PHPMailer on several projects but now I'm stuck. It gives me the error:
SMTP Error: Could not connect to SMTP host.
I've tried sending email from Thunderbird and it works ! But not through PHPMailer ... Here are the settings from Thunderbird:
Server name: mail.exampleserver.com
Port: 587
Username: user#exampleserver.com
Secure Authentication: No
Connection Security: STARTTLS
I've compared these with the server at my last project where I used PHPMailer and they were:
Server name: mail.exampleserver2.com
Port: 465
Username: user#exampleserver2.com
Secure Authentication: No
Connection Security: SSL/TLS
My php code is:
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = SMTP_HOST; // SMTP servers
$mail->Port = SMTP_PORT; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = SMTP_USER; // SMTP username
$mail->Password = SMTP_PASSWORD; // SMTP password
$mail->From = MAIL_SYSTEM;
$mail->FromName = MAIL_SYSTEM_NAME;
$mail->AddAddress($aSecuredGetRequest['email']);
$mail->IsHTML(true); // send as HTML
Where I am wrong?
Since this questions shows up high in google, I'd like to share here my solution for the case where PHP was just upgraded to version 5.6 (which has stricter SSL behavior).
The PHPMailer wiki has a section on this:
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#php-56-certificate-verification-failure
The suggested workaround is including the following piece of code:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
This should work for PHPMailer 5.2.10 (and up).
Note: Obviously, and also as suggested in that wiki, this should be a temporary solution!
The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one.
In my case it was a lack of SSL support in PHP which gave this error.
So I enabled extension=php_openssl.dll
$mail->SMTPDebug = 1; also hinted towards this solution.
Update 2017
$mail->SMTPDebug = 2;, see: https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#enabling-debug-output
Your problem is most likely this
Connection Security: STARTTLS
Connection Security: SSL/TLS
Those are 2 different protocols, are you using the correct one, whatever one you're using in Thunderbird needs to be used.
Try setting the variable:
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
I had a similar issue and figured out that it was the openssl.cafile configuration directive in php.ini that needed to be set to allow verification of secure peers. You just set it to the location of a certificate authority file like the one you can get at http://curl.haxx.se/docs/caextract.html.
This directive is new as of PHP 5.6 so this caught me off guard when upgrading from PHP 5.5.
I had the same problem and it was because PHPMailer realized the server supported STARTTLS so it tried to automatically upgrade the connection to an encrypted connection. My mail server is on the same subnet as the web server within my network which is all behind our domain firewalls so I'm not too worried about using encryption (plus the generated emails don't contain sensitive data anyway).
So what I went ahead and did was change the SMTPAutoTLS to false in the class.phpmailer.php file.
/**
* Whether to enable TLS encryption automatically if a server supports it,
* even if `SMTPSecure` is not set to 'tls'.
* Be aware that in PHP >= 5.6 this requires that the server's certificates are valid.
* #var boolean
*/
public $SMTPAutoTLS = false;
does mail.exampleserver.com exist ??? , if not try the following code (you must have gmail account)
$mail->SMTPSecure = "ssl";
$mail->Host='smtp.gmail.com';
$mail->Port='465';
$mail->Username = 'you#gmail.com'; // SMTP account username
$mail->Password = 'your gmail password';
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->CharSet = 'utf-8';
$mail->SMTPDebug = 0;
Followed code worked for me:
$mail = new PHPMailer(true);
$mail->isSMTP();// Set mailer to use SMTP
$mail->CharSet = "utf-8";// set charset to utf8
$mail->SMTPAuth = true;// Enable SMTP authentication
$mail->SMTPSecure = 'tls';// Enable TLS encryption, `ssl` also accepted
$mail->Host = 'smtp.gmail.com';// Specify main and backup SMTP servers
$mail->Port = 587;// TCP port to connect to
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->isHTML(true);// Set email format to HTML
$mail->Username = 'Sender Email';// SMTP username
$mail->Password = 'Sender Email Password';// SMTP password
$mail->setFrom('example#mail.com', 'John Smith');//Your application NAME and EMAIL
$mail->Subject = 'Test';//Message subject
$mail->MsgHTML('HTML code');// Message body
$mail->addAddress('User Email', 'User Name');// Target email
$mail->send();
$mail->SMTPDebug = 2; // to see exactly what's the issue
In my case this helped:
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
Since this is a popular error, check out the PHPMailer Wiki on troubleshooting.
Also this worked for me
$mailer->Port = '587';
Well this is really old but I still want to share my solution.
If you are using phpmail with an local server like xampp turn off your antivirus.
That solved it for me :)
I had a similar issue. I had installed PHPMailer version 1.72 which is not prepared to manage SSL connections. Upgrading to last version solved the problem.
I recently dealt with this problem, and the cause of the problem turned out to be that the root certificate on the SMTP server that I was connecting to was the Sectigo root certificate that recently expired.
If you're connecting to the SMTP server by SSL/TLS or STARTTLS, and you've not changed anything recently in the environment where your PHPMailer script is running, and this problem suddenly occurred - then you might want to check for an expired or invalid certificate somewhere in the certificate chain on the server.
You can view the server's certificate chain using openssl s_client.
For SSL/TLS on port 465:
openssl s_client -connect server.domain.tld:465 | openssl x509 -text
For STARTTLS on port 587:
openssl s_client -starttls smtp -crlf -connect server.domain.tld:587 | openssl x509 -text
In my case in CPANEL i have 'Register mail ids' option where i add my email address and after 30 minutes it works fine with simple php mail function.

PHPmailer can't connect to smtp server

I've been using PHPmailer (https://github.com/Synchro/PHPMailer) to send email through amazon SES for a few months. At some time in the last two weeks it has stopped working, and I haven't touched it. I'm getting an error msg:
SMTP Error: Could not connect to SMTP host.
This is my code.
public function sendEmail($to,$subject,$body){
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'amazonaws....'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mySMTPuname'; // SMTP username
$mail->Password = 'smtpPword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'example';
$mail->FromName = 'me';
$mail->AddAddress($to); // Name is optional
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
return $mail->Send();
}
My amazon account is still upto date and active. Is there any way to print out more detailed error msgs for debugging? Has there been any known issues lately?
Try :
$mail->SMTPDebug = 1;
// if you're using SSL
$mail->SMTPSecure = 'ssl';
// OR use TLS
$mail->SMTPSecure = 'tls';
This is a very old question but I just had the same problem so it may still be relevant to others.
If it stopped working without you changing anything it is probably connected to your hosting company / isp blocking SMTP traffic from your server to other servers.
There are a few topics on this, as multiple hosting companies using Cpanel and also Godaddy have implemented such measures for combating spam.
Try:
$mail->SMTPDebug = 3;
to get the maximum level of detail on the error.
One solution is to use the mail server in the same hosting account (if it blocks SMTP to the outside it probably has an internal service you can use).
To keep using Amazon SES you need to open SMTP traffic on your server.
IF you have control over Cpanel/WHM "tweak settings" you can do it yourself, otherwise you need to ask your hosting provider.
Check this answer for details "Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer

Unable to send mail with PHP (phpmailer) via Office365 in Hostgator. How to fix this?

I want to be able to send mails in Hostgator via office365. I was able to do it with Gmail, but can not set it up to work with office365.
It works on my 2 other servers. The only problem is Hostgator.
Do I have to fix something or Hostgator have to take some action?
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "pod51014.outlook.com";
$mail->Port = 587;
$mail->Username = "usernamehere";
$mail->Password = "************";
/* ... addaddres, reply, subject, message -> the usual stuff you need ... */
$mail->Send();
?>
I just keep getting following response:
SMTP -> ERROR: Failed to connect to server: Connection refused (111)
I was on the support chat with them and the 587 port should be open.
I think hostgator is blocking outgoing emails but accepts incoming emails.
If your hosting provider doesn't allow outbound SMTP mail, I suggest you take a look at Microsoft Graph - a REST API which let's you also send e-mails and do much more. You can use for example oauth2-azure library to interact with it very easily from your PHP code.
Try these things. Maybe something will work.
Set the host to:
$mail->Host = 'smtp.office365.com';
Do not set a port at all:
//$mail->Port = 587;

Categories