3

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('nkhlpatil647@gmail.com', 'First Name');
     $mail->SetFrom('nkhlpatil647@gmail.com','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?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • If you're using Amazon SES in a region other than US West (Oregon), replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP endpoint in the appropriate region. `$mail->Host = ...` – Yevgeniy Afanasyev Sep 01 '23 at 04:46
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – DarkBee Sep 01 '23 at 05:11

2 Answers2

1

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('nkhlpatil647@gmail.com', 'First Name');
    $mail->SetFrom('nkhlpatil647@gmail.com','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.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
1

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?

tremor
  • 3,068
  • 19
  • 37
  • check the code i just added too, this is how my phpmail is setup at the top.. note, if you are using an extension like SMTP you'll want to add that too. – tremor Aug 13 '18 at 04:55
  • I revisited the PHPMailer docs and the `send()` function can be used without specifying say `isSendMail` as you referenced. It will default to the php `mail()` unless specified. – Joseph_J Aug 13 '18 at 05:04
  • The `isSendmail` option is essentially the same as using `isMail` (both end up calling a local sendmail binary, via two different mechanisms), you just get slightly more control over it, but both are generally inferior, less safe and slower than using `isSMTP` to localhost. – Synchro Aug 13 '18 at 06:48
  • 1
    While SMTP is superior, it may not be available on shared webhosting. The Gmail option or an STMP relay like sendgrid is always a great option. I found that in practice, the docs are wrong about specifying the the mail function, perhaps it works this way when installed with composer, but not instantiated by PHP. – tremor Aug 13 '18 at 13:14