Understanding Email status codes

Discussion in 'Email' started by WesternRider, Jul 31, 2011.

  1. I am trying to create an email error handling script within my contact.php code.

    My email is working, but my script is not recognizing the successful email status code; thus my script returns an unsuccessful email (when it is really working). Am I missing something? This is my last migration problem in moving over to Win Host from Godaddy.

    if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
    alert('Thank you for the message. We will contact you shortly.');
    window.location = 'AboutUs.html';
    </script>
    <?php
    }
    else { ?>
    <script language="javascript" type="text/javascript">
    alert('Message failed. Please, send an email to [email protected]');
    window.location = 'AboutUs.html';
    </script>
    <?php

    Thanks,

    Greg
     
  2. FredC

    FredC Winhost Staff

    can you post the complete code snippet?
     
  3. Here is the complete code. I have modified the unsucessfull message, since the email is working. Eventually I want to add more checking for valid email addresses.

    <html>
    <head><title>Email</title></head>
    <body>



    <?

    $field_name = $_POST['cf_name'];
    $field_email = $_POST['cf_email'];
    $field_message = $_POST['cf_message'];

    require_once "Mail.php";

    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $subject = 'Message from Western Rider Magazine visitor '.$field_name;


    $body = 'From: '.$field_name."\n";
    $body .= 'E-mail: '.$field_email."\n";
    $body .= 'Message: '.$field_message;


    $host = "mail.westernridermagazine.com";
    $username = "XXXXXXXXX";
    $password = "XXXXXXXXX";


    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));


    $mail = $smtp->send($to, $headers, $body);


    if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
    alert('Thank you for the message. We will contact you shortly.');
    window.location = 'AboutUs.html';
    </script>
    <?php
    }
    else { ?>
    <script language="javascript" type="text/javascript">
    alert('Message received. Please, send an email to [email protected] if you have any further questions or comments');
    window.location = 'AboutUs.html';
    </script>
    <?php
    }
    ?>
     
  4. Ray

    Ray

    Keep in mind that you are using the PEAR module to send out email, not the typical PHP mail() function. To capture email status you'll need to call the PEAR method. Try using this code...

    if (PEAR::isError($mail)) { ?><script language="javascript" type="text/javascript">
    alert('Message received. Please, send an email to [email protected] if you have any further questions or comments');
    window.location = 'AboutUs.html';
    </script>

    <?php
    }
    else { ?><script language="javascript" type="text/javascript">
    alert('Thank you for the message. We will contact you shortly.');
    window.location = 'AboutUs.html';
    </script>

    <?php
    }
    ?>
     

Share This Page