phpmailer can not send email

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



phpmailer can not send email



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


<?php
$msg="";

use PHPMailerPHPMailerPHPMailer;
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?


$mail->send()


else




2 Answers
2



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 PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

// 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?





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 at 4: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 at 5:04



send()


isSendMail


mail()





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 at 6:48


isSendmail


isMail


isSMTP





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 at 13:14



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.


$mail->ErrorInfo;


$mail->send()



Like so:


$msg="";

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException; //<---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.


$mail->SMTPDebug





sir i try your code but still getting same msg mail msg has not been send
– Nikhil Patil
Aug 13 at 4:30






Please check updated answer.
– Joseph_J
Aug 13 at 4:31





Fatal error: Uncaught PHPMailerPHPMailerException: Could not instantiate mail function.
– Nikhil Patil
Aug 13 at 4:36





Check updated. PHPMailer says that you need to specify use PHPMailerPHPMailerException; Make sure that your code for the PHPMailer is in the correct paths.
– Joseph_J
Aug 13 at 4:51


use PHPMailerPHPMailerException;





Have you been able to send an email using PHP's mail() function.
– Joseph_J
Aug 13 at 5:05


mail()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS