Viewed   475 times

I'm trying to use PHPMailer for a small project, but I'm a bit confused about error handling with this software. Hoping someone has experience with it. When I've set up an email and I use:

$result = $mail->Send();

if(!$result) {
    // There was an error
    // Do some error handling things here
} else {
    echo "Email successful";
}

Which works fine, more or less. The problem is when there's an error, PHPMailer also seems to echo the error out, so if there's a problem, it just sends that info directly to the browser, essentially breaking any error handling I"m trying to do.

Is there a way to silence these messages? Its not throwing an exception, its just printing out the error, which in my test case is:

invalid address: @invalid@email You must provide at least one recipient email address.

Its meant to be an error, but it should be residing in $mail->ErrorInfo; not being echo'd out by the software.

 Answers

3

PHPMailer uses Exceptions. Try to adopt the following code:

require_once '../class.phpmailer.php';

$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch

try {
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
  $mail->SetFrom('name@yourdomain.com', 'First Last');
  $mail->AddReplyTo('name@yourdomain.com', 'First Last');
  $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
  $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
  $mail->MsgHTML(file_get_contents('contents.html'));
  $mail->AddAttachment('images/phpmailer.gif');      // attachment
  $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
  $mail->Send();
  echo "Message Sent OKn";
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
}
Tuesday, September 20, 2022
3

Here is another possible way without re-creating the Class each time.

<?php

require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();                                      // Set mailer to         use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'yyy.com';                 // SMTP username
$mail->Password = 'yyy';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
$person = array(0 => 'xxx.com', 'xxx2.com');

$mail->setFrom('yyy.com');
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = "xxx";
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$errors = [];
for ($x = 0; $x < 2; $x++) {
    $mail->addAddress($person[$x]);
    if(!$mail->send()) {
        $errors[] = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
    }
    $mail->clearAddresses();
}

if(empty($errors)) {
    // success
} else {
    // handle errors
}

Using $mail->clearAddresses(); within the loop.

Tuesday, November 8, 2022
 
2

You are inside a namespace so you should use Exception to specify the global namespace:

try {

  $this->buildXMLHeader();

} catch (Exception $e) {

    return $e->getMessage();
}

In your code you've used catch (Exception $e) so Exception is being searched in/as:

AppServicesPayUServiceException

Since there is no Exception class inside AppServicesPayUService so it's not being triggered. Alternatively, you can use a use statement at the top of your class like use Exception; and then you can use catch (Exception $e).

Monday, November 28, 2022
 
3

Please try to do the following:

In .htaccess

    # supress php errors
    php_flag display_startup_errors off
    php_flag display_errors off
    php_value docref_root 0
    php_value docref_ext 0


    # enable PHP error logging
    php_flag  log_errors on
    php_value error_log  /correct_path_to_your_website/error_modes/PHP_errors.log


    # general directive for setting php error level
    php_value error_reporting -1

In php file

Instead of intentional mistake in you wrote in your php file you can try doing something like:

    <?

    echo $_SERVER['DOCUMENT_ROOT']; // this will enable you to see 
                                    // the correct path to your website dir 
                                    // which should be written in .htaccess 
                                    // instead of correct_path_to_your_website
                                    // (check it just in case)

    $foo = $bar['nope'];// this should generate notice 

    call_undefined(); // this should generate fatal error


    ?>

Worked good with me)

Hope it'll help.

Wednesday, September 7, 2022
1

Encoding is not a variable: $this->Encoding

Wednesday, August 10, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :