Nikhil Patil
Nikhil Patil

Reputation: 39

phpmailer can not send email

Everything else is OK, but I can't send email for some reason:

<?php    
$msg="";

use PHPMailer\PHPMailer\PHPMailer;
include_once "PHPMailer/PHPMailer.php";
include_once "PHPMailer/Exception.php";

if(isset($_POST['submit'])) {
    $subject=$_POST['subject'];
    $email=$_POST['email'];
    $message=$_POST['message'];
   
    $mail= new PHPMailer();
 
     $mail->AddAddress('[email protected]', 'First Name');
     $mail->SetFrom('[email protected]','admin');
  
    $mail->Subject = $subject; 
   $mail->isHTML(true); 
   $mail->Body=$message;
    
    if($mail->send())
        $msg="Your rmail msg has been send";
     else
       $msg="mail msg has not been send";
}
?>

The $mail->send() function always goes to the else part. What am I doing wrong?

Upvotes: 3

Views: 1854

Answers (2)

Joseph_J
Joseph_J

Reputation: 3669

I believe that it is good coding practice to use curly braces all the time. This is in reference to your if/else statement.

Other than that, I do not see anything in your code that jumps right out and says problem area.

Please make sure that all of your $_POST variables are echoing out their expected values.

Echo you message out as well to make sure it is outputting your expected values.

You do not want any of those parameters to be empty.

The PHPMailer class has error handling. I suggest that you use a try/catch block to show any possible errors that are present and trouble shoot from there.

You can also use $mail->ErrorInfo;. This will show any errors that were generated after the $mail->send() function call. I have included both concepts in my answer.

Like so:

$msg="";

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception; //<---Add this.

//Switch your includes to requires.
require "PHPMailer/PHPMailer.php";
require "PHPMailer/Exception.php";
//require "PHPMailer/SMTP.php";  //If you are using SMTP make sure you have this line.

if(isset($_POST['submit'])) {
  try{
    $subject =$_POST['subject'];
    $email =$_POST['email'];
    $message =$_POST['message'];

    //$mail = new PHPMailer();
    $mail = new PHPMailer(true); //Set to true. Will allow exceptions to be passed.

    $mail->AddAddress('[email protected]', 'First Name');
    $mail->SetFrom('[email protected]','admin');

    $mail->Subject = $subject; 
    $mail->isHTML(true); 
    $mail->Body = $message;

    if($mail->send()){
      $msg="Your email msg has been send";
    }else{
       $msg="mail msg has not been send"; 
       echo 'Mailer Error: ' . $mail->ErrorInfo;
     }
   }catch(phpmailerException $e){
      echo $e->errorMessage();
   }
} 

If you are using SMTP you can try playing with the $mail->SMTPDebug settings. May provide you with some additional information. Check the PHPMailer docs for the values and their properties.

Upvotes: 1

tremor
tremor

Reputation: 3186

You are not declaring what is sending the mail, that could be one reason. PHPMailer does not actually send e-mail, it is designed to hook into something on your web server that can send email, eg: sendmail, postfix, an SMTP connection to a mail relay service, etc., so you may need to declare that in your settings.

For example, if you are using your webserver built-in sendmail, add this after

$mail = new PHPMailer;
// declare what mail function you are using
$mail->isSendmail();

PHPMailer supports several other options as well, like SMTP, and gmail. See this set of examples to suit your scenario best: https://github.com/PHPMailer/PHPMailer/tree/master/examples

Also, here is how I have mine setup, not sure if require or include_once is optimal, but my installation works great. Also, I have SMTP module added for using that over sendmail.

// require php mailer classes
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// require php mailer scripts
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';

This is how my personal installatoin of PHPMailer works in practice, instantiated through PHP and NOT installed via Composer. I used this answer from another post on SO - How to use PHPMailer without composer?

Upvotes: 1

Related Questions