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!
Related
I have hosted my website on Plesk Hosting and was working on submitting the contact form.
Installed PHP Mailer using composer.
First
I tried to send email using Gmail SMPT server
it worked fine
Second
I tried to send email using my webhosting SMTP Server
it is not working for me
$mail->Host = 'webmail.abc.in'; //host
$mail->SMTPAuth = false;
$mail->Username = '******#abc.in';
$mail->Password = '*******';
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
I tested the SMPT server using SMTPER . it could send email using same credentials.
i dont know where the issue is..
is there any other library other then phpmailer??
This is a example of of code I'm using with gmail. Tested it with webhosting SMTP and worked as well.
` $mailMsg = ADD_MAIL_MESSAGE_HERE;
$mailto = ADD_TO_ADDRESS_HERE;
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSmtp();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Username = ADD_USERNAME_HERE;
$mail->Password = ADD_PASSWORD_HERE;
$mail->SetFrom(ADD_FROM_ADDRESS_HERE);
//-------------------------------------------
$mail->Subject = ADD_MAIL_SUBJECT_HERE;
$mail->Body = $mailMsg;
$mail->AddAddress($mailto);
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
`
$mail->SMTPAuth = true;
As simple as that, I think.
You said you have tested the credentials on SMTPer,
I'm sure you checked the "Use authentication" checkbox.
Maybe you thought you could make it false because you are not using SSL,
But this is about user authentication, not about encrypted communication.
Are you using a Shared Hosting with Plesk? If yes, then this can probably be a port block issue (exact reason you will get only in the maillog). Looking at your code, I can see that in case of your local SMTP testing you are using port 25 while in case of Gmail it's 465.
By default, most of the shared hosting providers block the outgoing SMTP connection on port 25. This is done in order to protect the network and infrastructure from Spamming. If this is the case, then you need to contact their support to unblock the port or use some port free mode of email sending. Means instead of connecting over SMTP, connect over HTTP API for sending emails.
I currently have a website which uses PHPmailer to send emails. I am hosting it with 1&1.fr, but cannot find the information in order to actually send emails. Here is the following information that I need:
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
On the 1&1.fr website, they give out the following information:
In the image, they specify multiple ports as well as an entrance/exit server; which ones am I supposed to pick and enter into my PHP file.
The rest of my code works fine (it works when I use my gmail account using 000webhost).
Any help would be greatly appreciated.
// define the $mail // just in case you miss it as it is missing in your code.
$mail = new PHPMailer();
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2; // this is to enable debug if there are errors
$mail->isSMTP(); //Tell PHPMailer to use SMTP
//Set the hostname of the mail server
$mail->Host = 'auth.smtp.1and1.fr';
// Enable authentication so you must provide username and password for SMTP authentication
$mail->SMTPAuth = true;
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Here you are telling to use a secure connection with TLS/SSL
//Set the SMTP port number
$mail->Port = 587; // if specified tls. try also 465 as defined in the picture you post
// TCP port for secure connections. 465 is the secure port for outgoing
// emails and 993 is for incoming email using IMAP. If you use POP3 the
//incoming emails are received on 995 port number.
Hope now is more clear.
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
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.
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