Solved

trap and record smtp reply

Posted on 2013-05-13
5
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
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.

734 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