Friday, May 13, 2011

PHP Pear Mail Test Kit

Requested by the support team, I had build a PHP page that able to test the functionality of the PHP Pear Mail module.



The features included
  • Validate installation of PEAR
  • Validate installation of PEAR Mail module
  • Support all send mail options that supported by PEAR Mail - mail, direct sendmail, SMTP.
  • Enable or disable SMTP authentication.
  • Full validation on user input
  • ALL in a SINGLE file, easy to install.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<HTML>
    <HEAD>
        <TITLE>PHP Pear Mail Test Tool</TITLE>
        <META NAME="Generator" CONTENT="TextPad 4.6">
        <META NAME="Author" CONTENT="comcrazy">
        <META NAME="Keywords" CONTENT="php pear mail test tool">
        <META NAME="Description" CONTENT="PHP Pear Mail test tool, support mail, sendmail and SMTP">

        <style type="text/css">
            .errormsg
            {
                color: red;
                font-weight: bold;
            }
            .notimsg
            {
                color: blue;
                font-weight: bold;
            }
        </style>

        <script type="text/javascript">
            // <![CDATA[

            function authChange()
            {
                if(document.getElementById('smtpAuth').checked)
                {
                    document.getElementById('emailUsername').disabled = false;
                    document.getElementById('emailPassword').disabled = false;
                    document.getElementById('emailUserRequired').style.display = "";
                    document.getElementById('emailPasswordRequired').style.display = "";
                }
                else
                {
                    document.getElementById('emailUsername').disabled = true;
                    document.getElementById('emailPassword').disabled = true;
                    document.getElementById('emailUsername').value = "";
                    document.getElementById('emailPassword').value = "";
                    document.getElementById('emailUserRequired').style.display = "none";
                    document.getElementById('emailPasswordRequired').style.display = "none";
                }
            }

            function validateForm()
            {
                var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

                document.getElementById('fromEmailError').innerHTML = "";
                document.getElementById('toEmailError').innerHTML = "";
                document.getElementById('subjectError').innerHTML = "";
                document.getElementById('smtpserverError').innerHTML = "";
                document.getElementById('smtpportError').innerHTML = "";
                document.getElementById('emailUsernameError').innerHTML = "";
                document.getElementById('emailPasswordError').innerHTML = "";

                var errDetect = false;
                var notDataErrMsg = "Required.";
                var invalidEmailMsg = "Invalid email format";

                var fromEmail = document.getElementById('fromEmail').value;
                var toEmail = document.getElementById('toEmail').value;
                var subject = document.getElementById('subject').value;
                var emailContent = document.getElementById('emailContent').value;
                var smtpserver = document.getElementById('smtpserver').value;
                var smtpport = document.getElementById('smtpport').value;
                var emailUsername = document.getElementById('emailUsername').value;
                var emailPassword = document.getElementById('emailPassword').value;

                if (fromEmail.length == 0)
                {
                    document.getElementById('fromEmailError').innerHTML = notDataErrMsg;
                    errDetect = true;
                }
                else if(emailReg.test(fromEmail) == false)
                {
                    document.getElementById('fromEmailError').innerHTML = invalidEmailMsg;
                    errDetect = true;
                }

                if (toEmail.length == 0)
                {
                    document.getElementById('toEmailError').innerHTML = notDataErrMsg;
                    errDetect = true;
                }
                else if(emailReg.test(toEmail) == false)
                {
                    document.getElementById('toEmailError').innerHTML = invalidEmailMsg;
                    errDetect = true;
                }

                if (subject.length == 0)
                {
                    document.getElementById('subjectError').innerHTML = notDataErrMsg;
                    errDetect = true;
                }
              
                if (document.getElementById('mailerSMTP').checked)
                {
                    if (smtpserver.length == 0)
                    {
                        document.getElementById('smtpserverError').innerHTML = notDataErrMsg;
                        errDetect = true;
                    }

                    if (smtpport.length == 0)
                    {
                        document.getElementById('smtpportError').innerHTML = notDataErrMsg;
                        errDetect = true;
                    }
                    else if (!parseInt(smtpport) || parseInt(smtpport) <= 0 || parseInt(smtpport) > 65535)
                    {
                        document.getElementById('smtpportError').innerHTML = "Invalid port number, should between 1 - 65535";
                        errDetect = true;
                    }

                    if (document.getElementById('smtpAuth').checked)
                    {
                        if (emailUsername.length == 0)
                        {
                            document.getElementById('emailUsernameError').innerHTML = notDataErrMsg;
                            errDetect = true;
                        }

                        if (emailPassword.length == 0)
                        {
                            document.getElementById('emailPasswordError').innerHTML = notDataErrMsg;
                            errDetect = true;
                        }
                    }
                }

                return (errDetect == true? false : true);
            }
            // ]]>
        </script>
    </HEAD>

    <BODY>

        <H1 style="margin-bottom: 0;">PHP Pear Mail Tester</H1>
        <div style="margin-top: 0">by <a href="http://comcrazy-devcenter.blogspot.com/" alt="comcrazy development centre">comcrazy</a></div>

        <HR/>

        <?php
        include_once 'System.php';
        include_once 'Mail.php';
        if (class_exists('System', false))
        {
            if (!class_exists('Mail', false))
            {
                echo "<span class='errormsg'>ERROR: Pear Mail Module is not installed or not define at include path.</span>";
                echo "<HR/>";
                exit;
            }
        }
        else
        {
            echo "<span class='errormsg'>ERROR: Pear is not installed or not define at include path.</span>";
            echo "<HR/>";
            exit;
        }

        $errMsg = "";
        $notiMsg = "";

        if (isset($_REQUEST['mailer']))
        {
            $to = $_REQUEST['toEmail'];
            $subject = $_REQUEST['subject'];
            $message = $_REQUEST['emailContent'];
            $from = $_REQUEST['fromEmail'];

            if ($_REQUEST['mailer'][0] == "mail")
            {
                $mailer = Mail::factory('mail');
            }
            else if ($_REQUEST['mailer'][0] == "sendmail")
            {
                $mailer = Mail::factory('sendmail');
            }
            else if ($_REQUEST['mailer'][0] == "smtp")
            {
                $host = $_REQUEST['smtpserver'];
                $port = $_REQUEST['smtpport'];
                $auth = $_REQUEST['smtpAuth'] == "on" ? TRUE : FALSE;

                if ($auth)
                {
                    $username = $_REQUEST['emailUsername'];
                    $password = $_REQUEST['emailPassword'];
                }
                else
                {
                    $username = "";
                    $password = "";
                }

                $mailer = Mail::factory('smtp', array('host' => $host,
                        'port' => $port,
                        'auth' => $auth,
                        'username' => $username,
                        'password' => $password,
                        'debug' => true));               
            }
            else
            {
                $errMsg = "No Mailer is choosen";
            }

            if (!strlen($errMsg))
            {
                $headers = array('From' => $from,
                    'To' => $to,
                    'Subject' => $subject);

                $mail = $mailer->send($to, $headers, $message);

                if (PEAR::isError($mail))
                {
                    $errMsg = $mail->getMessage();
                }
                else
                {
                    $notiMsg = "Email successfully sent!";
                }
            }

            if (strlen($errMsg))
            {
                echo "<BR/><span class='errormsg'>$errMsg</span>";
            }
            else if (strlen($notiMsg))
            {
                echo "<BR/><span class='notimsg'>$notiMsg</span>";
            }
            echo "<HR/>";
        }
        ?>
        <form method="post" action="" id="mailForm" onsubmit="return validateForm();">
            <table border="1" cellpadding="2" style="float: left">
                <tr>
                    <th colspan="2">
                        <input name="mailer[]" type="radio" value="mail" CHECKED onclick="document.getElementById('mailserver').style.display='none'"/>Mail()&nbsp;&nbsp;
                        <input name="mailer[]" type="radio" value="sendmail" onclick="document.getElementById('mailserver').style.display='none'"/>Direct Sendmail&nbsp;&nbsp;
                        <input id="mailerSMTP" name="mailer[]" type="radio" value="smtp" onclick="document.getElementById('mailserver').style.display=''"/>SMTP
                    </th>
                </tr>
                <tr>
                    <th><span style="color:red">*</span>From:</th>
                    <td><input id="fromEmail" name="fromEmail" size="30"/><div class="errormsg" id="fromEmailError"></div></td>
                </tr>
                <tr>
                    <th><span style="color:red">*</span>To:</th>
                    <td><input id="toEmail" name="toEmail" size="30"/><div class="errormsg" id="toEmailError"></div></td>
                </tr>
                <tr>
                    <th><span style="color:red">*</span>Subject:</th>
                    <td><input id="subject" name="subject" size="30"/><div class="errormsg" id="subjectError"></div></td>
                </tr>
                <tr>
                    <td colspan="2"><textarea id="emailContent" name="emailContent" cols="50" rows="8">This is a message</textarea></td>
                </tr>
            </table>

            <table id="mailserver" border="1" cellpadding="2" style="float: left; display:none">
                <tr>
                    <th colspan="2">Email Server Configuration</th>
                </tr>
                <tr>
                    <th><span style="color:red">*</span>SMTP Host:</th>
                    <td><input id="smtpserver" name="smtpserver" size="30" value="ssl://smtp.gmail.com"/><div class="errormsg" id="smtpserverError"></div></td>
                </tr>
                <tr>
                    <th><span style="color:red">*</span>SMTP Port:</th>
                    <td><input id="smtpport" name="smtpport" size="30" value="465"/><div class="errormsg" id="smtpportError"></div></td>
                </tr>
                <tr>
                    <th>Authentication:</th>
                    <td><input id="smtpAuth" name="smtpAuth" type="checkbox" CHECKED onchange="authChange()"/>Authentication Required</td>
                </tr>
                <tr>
                    <th><span id="emailUserRequired" style="color:red">*</span>Username:</th>
                    <td><input id="emailUsername" name="emailUsername" size="30"/><div class="errormsg" id="emailUsernameError"></div></td>
                </tr>
                <tr>
                    <th><span id="emailPasswordRequired" style="color:red">*</span>Password:</th>
                    <td><input id="emailPassword" type="password" name="emailPassword" size="30"/><div class="errormsg" id="emailPasswordError"></div></td>
                </tr>
            </table>
            <div style="height:5px; width: 100%; clear:both"></div>
            <input type="submit" value="Send Email">
        </form>
    </BODY>
</HTML>

No comments:

Post a Comment