Connection to SMTP server via PHPMailer - php

I have some problems with PHPMailer - it can't connect to SMTP server.
I use following settings:
$mail->Host = "mail.site.com";
$mail->SMTPAuth = true;
$mail->Port = 26;
$mail->Username = "test#site.com";
$mail->Password = "***";
And when i try to connect - i receive error "SMTP Error: Could not connect to SMTP host".
Also i try to set:
$mail->SMTPSecure = 'ssl';
OR
$mail->SMTPSecure = 'tls';
But error still presents.
In my email client (Thunderbird) with settings:
Server - mail.site.com
Port - 26
Secure Connection - STARTTLS
Username - test#site.com
I connect succesfully and can send/receive emails. Also via telnet mail.site.com 26 i succesfully connect.
Why with same settings i can't connect to server?

First of all you should read the docs. Are you sure you should be connecting to port 26? SMTP normally runs on port 25 (inbound), and you should normally send using tls on port 587.

Related

SMTP GMAIL Connection

I can't connect to SMTP GMAIL with PHPMailer.
here is error :
Error on Jun 25, 2015 22:54PM - stream_socket_client(): unable to
connect to smtp.gmail.com:587 (Connection timed out) in
/home/amiroper/public_html/beporsbedoon/app/helpers/phpmailer/smtp.php
on line 222
and this is my code :
$this->_mail->isSMTP();
$this->_mail->Host = "smtp.gmail.com";
$this->_mail->SMTPAuth = true;
$this->_mail->Username = "amiroperator#gmail.com";
$this->_mail->Password = "*********";
$this->_mail->SMTPSecure = "tls";
$this->_mail->Port = "587";
$this->_mail->SMTPDebug = 4;
$this->_mail->From = "AmirOperator";
$this->_mail->FromName = 'amiroperator#gmail.com';
$this->_mail->addAddress("amiroperator#outlook.com", "test");
$this->_mail->isHTML(true);
$this->_mail->Subject = 'Registration confirm';
$this->_mail->Body = 'Thank you for registering to activate your account please click on this link. ".DIR."account/activate/$id/$activasion"';
$this->_mail->AltBody = 'Thank you for registering to activate your account please click on this link. ".DIR."account/activate/$id/$activasion"';
if(!$this->_mail->send()) {
$data['mailsent'] = false;
} else {
$data['mailsent'] = true;
}
the php code is wrong or this is connection problem
Adjust the authentication protocol and port number in your code:
// Your Current Settings
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Updated Settings
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
I've found that PHPMailer, when set to use Gmail SMTP servers with TLS authentication protocol and port number 587, simply doesn't work. However, I've never had a problem using SSL/465.
Google SMTP Settings
Difference Between TLS and SSL
Your server cannot connect to smtp.gmail.com on port 587. I have the same problem from a testing tool:
Resolving hostname...
Connecting...
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
Message sending failed.
From my local machine, it works perfectly:
Trying 74.125.195.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP be3sm8900765wib.21 - gsmtp
Maybe there are temporary problems at Google from the US (I am in Germany here) or something like this. There is no obvious mistake in your code. You can only try it again later or with another SMTP server.
Furthermore, you can try to connect with 74.125.195.108 directly (just deactivate SSL certificate validation).
EDIT: Just also try tls://smtp.gmail.com as host.

PHPMailler with mandrill smtp gives connection error

I am using smtp by mandrill on my website through PHPMailer. I have configured everything correctly. Here are the configurations-
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.mandrillapp.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->Host = "smtp.mandrillapp.com"; // sets the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "MANDRILL_USERNAME"; // SMTP account username
$mail->Password = "MANDRILL_KEY";
When I send email, it gives an error SMTP -> ERROR: Failed to connect to server: Connection refused (111)
I am using godaddy shared hosting plan.
Please help.
Some shared hosting providers block all outbound SMTP connections or common SMTP ports. You'll need to contact GoDaddy to confirm whether you're on a plan that supports outbound SMTP connections (you'll typically need a higher-level or VPS plan to support this with most shared hosts).

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

Mail not sending with PHPMailer over SSL using SMTP

I am trying to use PHPMailer to send e-mails over SMTP but so far have had no luck. I've gone through a number of SO questions, PHPMailer tutorials and forum posts but still cannot get it to work. I'll document as many of my failed attempts as I can remember to save time, but firstly here is the code I am using:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');
require('includes/class.phpmailer.php');
include('includes/class.smtp.php');
$mail = new PHPMailer();
$name = $_POST["name"];
$guests = $_POST["guests"];
$time = $_POST["time"];
$message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "myEmail#gmail.com"; // SMTP account username
$mail->Password = "myPassword"; // SMTP account password
$mail->SetFrom('myEmail#gmail.com', 'James Cushing');
$mail->AddReplyTo("myEmail#gmail.com","James Cushing");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($message)
$address = "myOtherEmail#me.com";
$mail->AddAddress($address, "James Cushing");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Firstly, when I run this code now I get two different errors. On my local server I get the error:
SMTP -> ERROR: Failed to connect to server: Operation timed out (60)
The following From address failed: myEmail#gmail.com : Called Mail() without being connected
Mailer Error: The following From address failed: myEmail#gmail.com : Called Mail() without being connected
I get moreorless the same error running the same code on my web server, but the first line is:
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)
Obviously it's worth pointing out that I'm not using the literal "myEmail#gmail.com" but I've substituted my own email out for this post.
Things I've tried
- Using the iCloud SMTP server
- Using a different port
- Enabling the OpenSSL extension in my php.ini file
- Copying code from various PHPMailer examples
- Using Google's "DisplayUnlockCaptcha" system to enable connections
- Sending to and from different addresses
- Removing the "#gmail.com" from the Username property
- A number of other things I can't remember
This has now been driving me mad for about a day, so if anyone can solve it they will be a hero.
Thanks
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail#gmail.com";
$mail->Password = "**********";
$mail->Port = "465";
That is a working configuration.
try to replace what you have
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: So, the code below should work very well for you.
mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$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
Firstly, use these settings for Google:
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";
But also, what firewall have you got set up?
If you're filtering out TCP ports 465/995, and maybe 587, you'll need to configure some exceptions or take them off your rules list.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I got a similar failure with SMTP whenever my client machine changes network connection (e.g., home vs. office network) and somehow restarting network service (or rebooting the machine) resolves the issue for me. Not sure if this would apply to your case, but just in case.
sudo /etc/init.d/networking restart # for ubuntu
First, Google created the "use less secure accounts method" function:
https://myaccount.google.com/security
Then created the another permission:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
Hope it helps.

PHPMailer with GMail: SMTP Error

I am making use of PHPMailer to send mail through GMail. The code I use is straight from a tutorial and it works perfectly on my laptop. However, testing this on a Windows 2003 Server - it seems to always return an SMPT error:
SMTP Error: Could not connect to SMTP
host. Mailer Error: SMTP Error: Could
not connect to SMTP host.
Here is the settings I use in PHPMailer:
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // use ssl
$mail->Host = "smtp.gmail.com"; // GMAIL's SMTP server
$mail->Port = 465; // SMTP port used by GMAIL server
Can I say with confidence that this isn't a port issue, since I am connecting to another server on port 465 and it is sending mail. If not, please explain.
How can I resolve this issue?
Thanks all for any help
First thing notice off-hand: Gmail uses TLS. Don't know if having SSL instead of TLS will make much of a difference but SSL is the predecessor to TLS.
I recommend checking out also, its phpmailer customized for using gmail. PHPGMailer
To use PHPMailer with gmail, don't use SSL/465 (it's been deprecated since 1998), use TLS/587 as Noctrine suggests, and here's how:
include 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "tls://smtp.gmail.com"; // GMAIL's SMTP server
$mail->Port = 587; // SMTP port used by GMAIL server
...
You should find that works.

Categories