Solved

trap and record smtp reply

Posted on 2013-05-13
5
196 Views
Last Modified: 2013-05-19
 $mail->Send();
  
  echo "Mail was sent.</p>\n";
  $email_wait=0;
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
  $email_wait=2;
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
  $email_wait=3;
}

Open in new window




http://www.greenend.org.uk/rjk/tech/smtpreplies.html
is all the smtp replys

but the phpmailer try catch only catches two types of exceptions
I only get this trapped
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
and not this
echo $e->getMessage(); //Boring error messages from anything else!

but I want all types of errors to be logged
0
Comment
Question by:rgb192
  • 2
  • 2
5 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39162492
This is not a php thing. Your mail server is what handles any errors (being messages from the remote mail server) and logs them
Depending on your server set up you may not have access to it with php.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39162870
For "catch" to work you have to use "try" and it's not shown in your example code.  I think I would fix that first, then create the SSCCE so we have something we can test with.  You can use var_dump() to print out the contents of the exception object.
0
 

Author Comment

by:rgb192
ID: 39163039
using phpmailer try/catch
how can I vardump and trap this error

try {
  $mail->IsHTML(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = $smtpdebug;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = $smtpauth;                  // enable SMTP authentication
$mail->SMTPSecure = $smtpsecure;                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = $port;                   // set the SMTP port for the GMAIL server
$mail->Username   = "username";  // GMAIL username
$mail->Password   = "password";            // GMAIL password  

 
  $mail->From = $mail->Username;
  $mail->FromName = "my name";
  
 $mail->AddAddress($email);




   $mail->Body='body';
   $mail->Subject='subject';
  
 $mail->Send();
  
  echo "Mail was sent.</p>\n";
  $email_wait=0;
} catch (phpmailerException $e) {
  echo $e->errorMessage(); //Pretty error messages from PHPMailer
  $email_wait=2;
} catch (Exception $e) {
  echo $e->getMessage(); //Boring error messages from anything else!
  $email_wait=3;
}

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39164416
When your script uses the catch() statement it identifies two important pieces of data.  First, it identifies the kind of exception it can catch and it will not catch other kinds of exceptions.  Second it identifies the variable name that will be assigned to the exception object.  Man pages here:
http://php.net/manual/en/language.exceptions.php
http://php.net/manual/en/language.exceptions.extending.php

This little script illustrates what happens when you've extended the built-in PHP Exception class.
http://www.laprbass.com/RAY_try_throw_catch.php?q=0
http://www.laprbass.com/RAY_try_throw_catch.php?q=1
http://www.laprbass.com/RAY_try_throw_catch.php?q=2
http://www.laprbass.com/RAY_try_throw_catch.php?q=3

<?php // RAY_try_throw_catch.php
error_reporting(E_ALL);
echo "<pre>";

// DEMONSTRATE A TRY WITH MULTIPLE CATCH BLOCKS
// EXAMPLE: RAY_try_throw_catch.php?q=1
// http://php.net/manual/en/reserved.exceptions.php
// http://php.net/manual/en/language.exceptions.php
// http://php.net/manual/en/language.exceptions.extending.php

Class ScrewUP
{
    // CONSTRUCTOR SETS A CODE PROPERTY FOR THE OBJECT
    public function __construct($q)
    {
        // CHOOSE AN INTEGER VALUE FROM ZERO TO THREE
        $this->code = 0;
        if (!empty($q)) $this->code = floor($q % 4);
    }

    // THIS WILL THROW SOME KIND OF EXCEPTION BASED ON THE CODE
    public function hiccup()
    {
        // THESE EXCEPTIONS WILL BE CAUGHT
        if ($this->code == 0) throw new Exception_zero('Hello');
        if ($this->code == 1) throw new Exception_one('World');

        // THIS WILL BE AN UNCAUGHT EXCEPTION: FATAL ERROR
        if ($this->code == 2) throw new Exception_two('Foobar');

        // THIS WILL BE AN UNEXTENDED EXCEPTION
        throw new Exception('Naked');
    }
}

Class Exception_Zero extends Exception
{
    public function __construct($x)
    {
        parent::__construct();
        echo "Exception Zero: $x ";
    }
}

Class Exception_One extends Exception
{
    public function __construct($x)
    {
        parent::__construct();
        echo "Exception One: $x ";
    }
}

// INSTANTIATE THE CLASS USING THE URL q= VARIABLE
$x = new Screwup($_GET['q']);

// RUN THE TRY/CATCH LOGIC TO THROW AN EXCEPTION
try
{
    $x->hiccup();
}
catch (exception_One $e)
{
    echo "I Have Caught Exception Number: $x->code ";
    var_dump($e);
}
catch (exception_Zero $e)
{
    echo "I Have Caught Exception Number Zero ";
    var_dump($e);
}
catch (exception $e)
{
    echo "I Have Caught an UnExtended Exception ";
    var_dump($e);
}

Open in new window

Edited to replace the code snippet.  HTH, ~Ray
0
 

Author Closing Comment

by:rgb192
ID: 39180160
thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now