0

I want to send mail using phpmailer. I have uploaded my phpmailer files on my Godaddy server. The below code is running in my localhost but not on my server.

2019-02-03 16:54:12 SMTP ERROR: Failed to connect to server: Connection refused (111)

The below is the code for php

<?php
include_once('PHPMailer/src/PHPMailer.php');
include_once('PHPMailer/src/SMTP.php');

$mail = new PHPMailer\PHPMailer\PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587,465
$mail->IsHTML(true);
$mail->Username = "mail@gmail.com";
$mail->Password = "password";
$mail->SetFrom("mail@gmail.com");
$mail->Subject = "Test mail";
$mail->Body = "Hello World";
$mail->AddAddress("mail@gmail.com");
if($mail->Send()) {
    echo "Message has been sent";
}
?>
James Z
  • 12,209
  • 10
  • 24
  • 44
  • maybe see here : https://stackoverflow.com/questions/30202038/gmail-account-smtp-error-failed-to-connect-to-server-connection-refused-11 – Honk der Hase Feb 03 '19 at 18:05
  • Search before you post. This has been answered many times before, and is explicitly covered in the PHPMailer troubleshooting guide. GoDaddy blocks outbound SMTP. You’re also using an obsolete version of PHPMailer. Upgrade it. – Synchro Feb 03 '19 at 19:33
  • I have refered to similar questions but didnt find the solution – mukesh behwal Feb 03 '19 at 19:34
  • Possible duplicate of [PHPMailer SMTP Connection Failed - GoDaddy](https://stackoverflow.com/questions/38254105/phpmailer-smtp-connection-failed-godaddy) – Synchro Feb 03 '19 at 19:34
  • 2019-02-03 20:03:22 CLIENT -> SERVER: xyz.com 2019-02-03 20:03:22 CLIENT -> SERVER: STARTTLS now this is comming – mukesh behwal Feb 03 '19 at 20:05

1 Answers1

0
use PHPMailer\PHPMailer\PHPMailer; <-- make sure these are not in a function
use PHPMailer\PHPMailer\Exception;

require 'path/src/Exception.php';
require 'path/src/PHPMailer.php';
require 'path/src/SMTP.php';

Use tls and not ssl it can be buggy

$mail->SMTPSecure = "tls";
$mail->Port = 587;

Check out my answer here this will tell you everything you need to know to connect to gmail with phpmailer

Cesar Bielich
  • 4,754
  • 9
  • 39
  • 81
  • The above code is working in XAMPP properly but not working on Godaddy server – mukesh behwal Feb 03 '19 at 19:29
  • Are you shared hosting or dedicated? – Cesar Bielich Feb 03 '19 at 19:34
  • dedicated hosting – mukesh behwal Feb 03 '19 at 20:18
  • Add the changes I made to my answer and try that. If it still doesnt work try adding port 587 TCP OUT to the firewall – Cesar Bielich Feb 03 '19 at 20:19
  • This page isn't working error is coming now @Cear Bielich – mukesh behwal Feb 04 '19 at 14:00
  • I think the Godaddy server is blocking Port 465... i have tried every method. It is either not connection or Page isn't working Error is coming – mukesh behwal Feb 04 '19 at 14:11
  • I have also tried tls and port 587 but still not working – mukesh behwal Feb 04 '19 at 14:11
  • If you have a dedicated server then the only one blocking 465 is you. Did you go to your WHM firewall and add port 587 TCP OUT to the firewall? By default 465 is already open in WHM – Cesar Bielich Feb 04 '19 at 16:35
  • please assist me to do the above. how to add 465 and 587 port to WHM firewall @Cesar Bielich – mukesh behwal Feb 05 '19 at 14:03
  • Log into your Godaddy account and under My Products Expand "Server" and click on "Manage". Then click on "cpanel" then "Manage Server". That will get you to WHM. In WHM in the search box type "firewall" and then click on "ConfigServer Security & Firewall". Then click on "Firewall Configuration". Look for `Allow outgoing TCP ports` and put them in there. Save it and your done. – Cesar Bielich Feb 05 '19 at 19:55
  • if im commenting the //$mail->IsSMTP(); the mail is getting sent in spam with an alert account could not verified. But while i use $mail->IsSMTP(); SMTP connect() failed error is comming. – mukesh behwal Feb 05 '19 at 20:21
  • As I described in the link I put in my answer if you comment out `$mail->IsSMTP();` then your local server will be sending the mail out since phpmailer by default will send it that way if you are not using SMTP. And most likely you do not have a local mail server configured correctly so yes it will go to spam everytime. Did you add all the extra lines I showed you in my answer? – Cesar Bielich Feb 05 '19 at 20:24
  • Also make sure you have your gmail to [allow less secure apps](https://support.google.com/accounts/answer/6010255?hl=en) enabled – Cesar Bielich Feb 05 '19 at 20:32
  • Yes sir i have enabled my google account less secure – mukesh behwal Feb 05 '19 at 20:33
  • In a terminal from your server run this command `telnet smtp.gmail.com 587`. It should say something like `Connected to smtp.gmail.com`. If not then your ports are not open – Cesar Bielich Feb 05 '19 at 20:35
  • Sir im not using any terminal in Cpanel – mukesh behwal Feb 05 '19 at 20:40
  • In cpanel you have a button called "Terminal" click that – Cesar Bielich Feb 05 '19 at 20:45
  • In my cpanel home there is no Terminal – mukesh behwal Feb 05 '19 at 20:45