0

When I sent email from server then it gives me two error -

SMTP Error: Could not connect to SMTP host. Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

I found many hints on another answers on Stack overflow but doesn't work. I tried port no 465/587/65. Even below code works proper on my local system If i set port 587. But in server, It doesn't work.

require('class.phpmailer.php');
require('class.smtp.php');

$mail = new PHPMailer();

$mail->IsSMTP();      
$mail->SMTPSecure = "ssl";
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com"; 
$mail->Username = "FromEmailId";
$mail->Password = "Password"; 
$mail->Port = 465;
$mail->From = "FromEmailId";
$mail->AddAddress("ToEmail");      
$mail->IsHTML(true);                                  
$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
echo "Message has been sent";
Darren
  • 12,786
  • 3
  • 34
  • 71
Puneet Chawla
  • 4,891
  • 4
  • 13
  • 33

4 Answers4

1

Try phpmailer in debug mode to check the error

$mail->SMTPDebug = 3;       // Enable verbose debug output
Ranjeet Singh
  • 538
  • 4
  • 12
  • Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone May 30 '16 at 07:15
  • Out of the answers posted, this is the only one going in the right direction, but it should be a comment. – Synchro May 30 '16 at 08:13
  • @Synchro Agree with you. This simple line help you to debug phpmailer. It basically gives verbose error to help you trace the bug or error. – Ranjeet Singh May 30 '16 at 09:52
  • Thanks for replying Ranjeet. I discussed with my friend and he told, there is a need to do some setting on godaddy server (configuration email). That's why It's not working. – Puneet Chawla May 30 '16 at 14:41
1

First logout the your gmail account

then open this url use this yrl

enter image description here

click the continue button

Next change the port and SMTPsecure

$mail->SMTPSecure = "tls";
$mail->Port = 587;
Yogesh Prajapati
  • 253
  • 3
  • 11
0

The Gmail Help :

Still can't send mail?

If you tried configuring your SMTP server on port 465 (with SSL/TLS) and port 587 (with STARTTLS), but are still having trouble sending mail, try configuring your SMTP to use port 25 (with SSL/TLS).

Community
  • 1
  • 1
0

This question failed to do a basic search and missed a vital piece of information later mentioned in comments: the OP is using GoDaddy. GoDaddy is well known to block outbound SMTP, so any suggestions relating to switching port numbers or security protocols will not help. You need to read other questions on GoDaddy's mail handling, and read their support documentation, which will push you in the direction of using GoDaddy's mail servers (using securehosting domains) or via localhost as a relay to the same. All of this is also covered in the PHPMailer troubleshooting guide.

In future, search before posting, as per the SO guidelines.

Community
  • 1
  • 1
Synchro
  • 29,823
  • 14
  • 69
  • 85