-1

I run into situation where I need to wrap php mailer inside my own function A, this function needs to return true or false if mail was successfully sent or not and another function B will still check if A returned true and do a few things or echo to user that mail was not sent if false. In other words, I need to do other things after sending email and I need to handle the success or failure myself. But once phpmailer sends mail, it hurriedly terminates the whole code and echoes sth to user and other functions won't execute.

smacaz
  • 148
  • 8
  • 1
    Phpmailer should not stop your php script. It would only do that if a critical error occurred somewhere. Did you check any logs to see if any errors/exceptions were thrown? – Dirk Scholten Sep 27 '18 at 08:08

1 Answers1

1

You can put the if condition for the send() function and call the necessary methods inside that.

For example

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('myfriend@example.net', 'My Friend');
$mail->Subject  = 'First PHPMailer Message';
$mail->Body     = 'Hi! This is my first e-mail sent through PHPMailer.';
if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
VinuBibin
  • 679
  • 8
  • 19