Solved

trap and record smtp reply

Posted on 2013-05-13
5
199 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 110

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 110

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 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