PHP Mailer Send a email [duplicate] - php

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.

Related

Connecting to SMTP server through PHP fails

I have a website which I have programmed one of its pages to connect to SMTP to send clients an email. The issue starts when I upload the files to host, which then it won't connect and show me an error SMTP Error: Connection timed out.
On localhost, the code works fine and connects successfully in an instance. Is there something I need to change in the code or in the host settings to make it work fine?
Using PHPMailer, these are my connection variables (excluding subject, body etc):
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->AuthType = 'LOGIN';
$mail->Username = 'email#gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false ) );
The issue starts when I upload the files to host
Why do you think that uploading files influencing the connectivity to the mail server?
$mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false ) );
That's not a good idea.
Is there something I need to change in the code or in the host settings to make it work fine?
If you change something in the code, in the host settings or in the network infrastructure where the host resides then it will probably start working (see note 1) but because you've only told us about (some of) the code, we cannot guess what that might be.
Its not uncommon for an internet facing host to be constrained in what it can do - this can be restricted by routing, firewalls, DNS availability & permissions. These are the things you should be checking (or speaking to the people whom manage the host).
note 1: for the benefit of later readers - you may also need to change something at the mail server too - but that probably doesn't apply here if the code behaves as expected when running on a different host)
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
You are having Connection timeout issue. That is most possibly because of something wrong in your configuration which is preventing your code to connect with google smtp server.
First of all make sure you are using the latest version of PHPMailer. Going further with your code that is having issue
Please follow the following update to debug and resolve the issue
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication;
$mail->Username = 'email#gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to`
`$mail->AuthType = 'LOGIN'
$mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false ) );`
I believe you should get a positive result if you try with the edited code chunk.
For further clarity on this, you might like to have a look at their (PHPMailer's) actual doc to connect with Google SMTP
Hope, this helps!

phpmailer hangs on send() [duplicate]

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

Mails not sending using PHPMailer and SSL

I configured my mail server as in the following guide:
https://www.digitalocean.com/community/tutorials/how-to-configure-a-mail-server-using-postfix-dovecot-mysql-and-spamassasin
Now I am trying to send mails via PHP using this library:
https://github.com/PHPMailer/PHPMailer
But the problem seems the SSL (cert). I am not sure if the script is not able to establish a connection or if the problem is the unverified SSL cert.
Do you guys have any idea?
Should I use startssl oder starttsl in the script?
SSLaccept error from x.com[x.x.x.x]: 0
Dec 8 17:45:23 x postfix/submission/smtpd[13479]: warning: TLS library problem: 13479:error:14094418:SSL routines:SSL3READBYTES:tlsv1 alert unknown ca:s3pkt.c:1258:SSL alert number 48:
My code so far:
function send_mail($from, $from_name, $to, $to_name, $subject, $content)
{
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.x.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply#x.com'; // SMTP username
$mail->Password = 'so secret'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->From = $from;
$mail->FromName = $from_name;
$mail->addAddress($to, $to_name); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $content;
$mail->AltBody = 'Please use an html compatible Client to view this mail!';
if(!$mail->send()) {
return false;
} else {
return true;
}
}
I had the same error, and finally figured out that phpmailer wants to use a normal smtp connection and then STARTTLS or STARTSSL to initialize encryption. In other words: it wants to connect to a submission service instead of an smtps service. Submission is on port 587 (usually) and smtps is on port 465. In fact, the smtps protocol was deprecated a long time ago. There are still many ISPs out there using smtps, but apparently they shouldn't.
If your mail provider allows you to use submission (port 586) instead of 465, then change your port number in your config and it will work! If you happen to be the sysadmin on the smtp server, then simply start a submission service, listening on port 587. If your ISP only supports smtps, then probably you are stuck. It seems that phpmailer does not handle SMTPS connections that are encrypted from the beginning. It only support connections that can be switched to encrypted (using STARTTLS).
The difference between the two is also described here:
What is the difference between ports 465 and 587?
UPDATE: before you try sending out any email, please update to the very latest version of phpmailer. Download the latest from github ( https://github.com/PHPMailer/PHPMailer ). Do not try to use any other version! Sometimes a small change in the minor version will decide between a successfully sent email and an "smtp connection error".

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

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

Categories