-1

Here is my code. I really need to know what is wrong with my code.I am getting this error:- "SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting"

<?php
require_once 'PHPMailer/class.phpmailer.php';
    //sending mail for verification
    $mail = new PHPMailer();

$mail->IsSMTP(); 
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; 
$mail->Port = 587;
$mail->Host = 'mail.mydomain.com';
$mail->Username = 'myusername'; // Enter your SMTP username
$mail->Password = "mypassword"; // SMTP password


$mail->From = "support@mydomain.com";
$mail->FromName = " Verification";
$mail->AddAddress("emailid@yahoo.com", "Name");

$mail->AddReplyTo("myemail@gmail.com", "Information");




$mail->IsHTML(true);                                  

$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if($mail->Send()){
echo "Mail sent";
}else{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

My credentials are correct.I did follow the link in the error message and tried almost everything mentioned there but nothing seems to work for me. This is the shortest and trimmed version of my original code, the idea is to tell that even this is not working for me.

  • You should check PHP mailer error like this `echo 'Mailer Error: ' . $mail->ErrorInfo;` – Vikash Kumar Apr 01 '16 at 04:19
  • Didn't it occur to you to follow that link that tells you how to diagnose and fix all kinds of problems in PHPMailer? Or if you did, do you think we don't need to know what you tried? – Synchro Apr 01 '16 at 05:47
  • You're also using an old version of PHPMailer and have based your code on an obsolete example. If you want to get help you do have to put in at least a small amount of effort. – Synchro Apr 01 '16 at 05:49
  • I did actually. I have been using lot of attachments and many other important things in my original code. Here is just the sample and even this doesn't work for me. I thought focusing on superfluities won't be a good idea here. I tried almost everything. My PHPMailer Version is latest. If you want to see all the code I can email you. And I did follow the code and tried almost everything mentioned there even composer. – Waseem Abbas Apr 01 '16 at 06:10
  • Let me know if still there is something unclear @Synchro – Waseem Abbas Apr 01 '16 at 06:16
  • You are not running the latest version - you're only including the PHPMailer class and not using the autoloader and you're using SMTP - that combination has not worked (because it won't find the SMTP class) in PHPMailer for over a year. "Superfluities" are exactly what we need to know - you have not told us what you've tried, nor your results, nor any debug output, just an obsolete code example. – Synchro Apr 01 '16 at 06:44
  • I found solution to my problem. I have answered my question in the answers. You are welcome to comment on it. And let me know if there is any risk factor involved. Rest assured I am using latest version of PHPMailer and I did try including PHPMailAutoloader. It didn't work. However, including class.smtp.php made my code work. Thanks a lot for your help. @Synchro – Waseem Abbas Apr 01 '16 at 08:25
  • Your problem was because you had used an obsolete example, the code you posted in your question was not the code you were running, and you had not shown any error or debug output. It's impossible to help you when you don't provide the information necessary to diagnose your problem. – Synchro Apr 01 '16 at 08:30
  • Possible duplicate of [PHPMailer generates PHP Warning: stream\_socket\_enable\_crypto(): Peer certificate did not match expected](http://stackoverflow.com/questions/30371910/phpmailer-generates-php-warning-stream-socket-enable-crypto-peer-certificate) – Synchro Apr 01 '16 at 09:36

1 Answers1

-1

I found the solution my this problem. Here are the changes I made and it works perfectly fine.

<?php
require_once 'PHPMailer/class.phpmailer.php';
require_once 'assets/import/PHPMailer/class.smtp.php'; //I added this here which wasn't added in original script
    //sending mail for verification
    $mail = new PHPMailer();

$mail->IsSMTP(); 
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true; 
$mail->Port = 587;
$mail->Host = 'mail.mydomain.com';
$mail->Username = 'myusername'; // Enter your SMTP username
$mail->Password = "mypassword"; // SMTP password


$mail->From = "support@mydomain.com";
$mail->FromName = " Verification";
$mail->AddAddress("emailid@yahoo.com", "Name");

$mail->AddReplyTo("myemail@gmail.com", "Information");

// Adding SMTPOption is the main thing that solved my problem. I got this from troubleshooting page of PHPMailer. And they
// recommended to do this only if every other option fails. 

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);


$mail->IsHTML(true);                                  

$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if($mail->Send()){
echo "Mail sent";
}else{
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • Instead of compromising the security of everyone that uses your app, the correct way to address SSL certificate validation failure is to report it to your ISP, who should not be running an insecure mail server. – Synchro Apr 01 '16 at 08:33
  • This is exactly the code I am running. I can't tell you my login credentials or Email IDs I am using. I am pretty sure that I have provided enough information to diagnose the problem. If not then tell me in exact words what you are looking for. You abuse the word 'obsolete' every time you say something. I am using right domain, password and every other thing and I don't want you or anyone to know about it. Are you trying to tell me that this won't run just because its obsolete? – Waseem Abbas Apr 01 '16 at 09:01
  • "Obsolete" is entirely appropriate. You didn't read the very first (or the second) paragraph in the troubleshooting guide; You didn't search for your error message in the guide (it's right there, and tells you how to diagnose the cause). Somehow your inability to read basic instructions is my fault? – Synchro Apr 01 '16 at 09:43
  • I mentioned it very clearly that I tried almost every possible solution mentioned at their troubleshooting page. If you don't want to answer this then don't. You have already downvoted my question and answer. Now please don't go viral and create a mess here. And I won't be impertinent here regardless of your numerous provocative efforts since the beginning. @Synchro – Waseem Abbas Apr 01 '16 at 12:17
  • You clearly had not since you had failed to do the very first thing it said to do. You still have not posted any debug output despite having been asked to do so, so *nobody* can answer your question. I'm not sure if you're being deliberately obtuse or you really don't understand what's being asked for. – Synchro Apr 01 '16 at 12:27
  • I told this in the start of the question that the browser throws this error. "SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting". And I don't understand what's being asked and I also told you how my problem was solved. I am not using it for any secure purposes and it serves my purpose best but I am willing to know the best solution for this. I honestly do not understand what you are asking. All I know is that you have been deprecating my question since the start. – Waseem Abbas Apr 01 '16 at 13:02
  • That is not debug output, that's just you echoing `ErrorInfo` - your code has `SMTPDebug = 2`, which is fairly noisy - where is the rest of the output? – Synchro Apr 01 '16 at 13:08