Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

Mailer-Example

Sections:

Info

The mailer lib support php- and smtp mailing.

Example Code

The following code demonstrates how to use the class.

<?php
/*!***********************************************************************
 *************************************************************************
 * \file            mailer-sample.php
 *
 * \author          Kai Klenovsek
 *
 * \date            First Step: 2005-02-08
 *
 * \note <br>
 * \b THE \bBEER-WARE \bLICENSE <br>
 *    As long as you retain this notice you can do whatever you want with <br>
 *    this stuff. If we meet some day, and you think this stuff is worth it, <br>
 *    you can buy me a beer in return. <br>
 *
 * \b NOTES: <br>
 *   
***************************************************************************
***************************************************************************/
  
// Include phplibex
require("../../phplibex.inc.php");

echo "<a href=\"".$SCRIPT_NAME."?action=php\">Send test mail with PHP-Mailer</a><br>";
echo "<a href=\"".$SCRIPT_NAME."?action=smtp\">Send test mail with SMTP-Mailer</a><br>";
echo "<a href=\"".$SCRIPT_NAME."?action=esmtp\">Send test mail with ESMTP-Mailer</a><br>";

if ( $action )
{
    switch ( $action )
    {
        case "php":
            $mailer = new phpmailer();
            break;
        
        case "smtp":
            $mailer = new smtpmailer("localhost");
            break;
            
        case "esmtp":
            $mailer = new smtpmailer("localhost");
            break;

        default:
            break;
    }
 
    // Set here the email submitter
    $mailer->mime_set_adrfrom("postmaster@localhost", "Postmaster");
    
    // Set receiver
    $mailer->mime_set_adrto("postmaster@localhost");
    
    $mailer->mime_set_cc("newuser@localhost");
   
    $mailer->mime_set_bcc("newuser@localhost");
  
    // Here you can set the subject or reference
    $mailer->mime_set_reference("Im the subject or the reference");
  
    // This is the body or the main message
    $mailer->mime_set_body("Im the body");
  
    /* Set the email priority
     * 1 -> High 
     * 2 -> Normal
     * 3 -> Normal 
     * 4 -> Low */
    $mailer->mime_set_priority("1");
    
    // Add an attachment to your email. 
    $mailer->mime_add_attachment("tigger.gif");
       
    // Add another attachment.
    $mailer->mime_add_attachment("test.txt");
        
    /* NOTE:
     * IF YOUR SERVER DONT HAVE INSTALLED THE MIME_CONTENT_TYPE() FUNCTION, 
     * YOU HAVE TO SET THE CONTENT TYPE MANUALLY LIKE THE FOLLOWING SAMPLE: */                         
    // $mailer->mime_add_attachment("tigger.gif", "image/gif");
    // $mailer->mime_add_attachment("mime-header.txt", "text/plain");
    
     
    if ( $action == "smtp" || $action == "esmtp")
    {
        // If we use the ESMTP protocol we have to enable it.
        if ( $action == "esmtp" )
        {
            $mailer->smtp_esmtp_init( "newuser@localhost", "wampp", true );   
        }
          
        /* Ok lets send and check it. You could send your mail
        * as normal "text/plain" or as "text/html". */
        if ( $mailer->smtp_do_send("text/plain") )
            echo "<br><b>Everything works fine. Email is out</b>";
        else
            echo "<br><b>Oh Oh sorry. an error occurred</b>";        
        
        // Get the complete stream
        $stream = $mailer->smtp_get_stream();
    
        // Show stream
        echo "<br><br><b>Stream:</b><br>";
        
        for ( $loop=0; $loop <= sizeof($stream); $loop++ )
        {
            $data = htmlentities($stream[$loop])."<br>";    
            echo nl2br($data);
        }
    
        // Get error list
        $error = $mailer->smtp_get_errorlist();
    
        // Show errors
        echo "<br><b>Errorlist:</b><br>";  
        
        for ( $loop=0; $loop <= sizeof($error); $loop++ )
            echo $error[$loop]."<br>";    
    }
    
    if ( $action == "php" )
    {
        /* Ok lets send and check it. You could send your mail
         * as normal "text/plain" or as "text/html". */
        if ( $mailer->php_do_send("text/plain") )
            echo "<br><b>Everything works fine. Email is out</b>";
        else
            echo "<br><b>Oh Oh sorry. an error occurred</b>";
            
            
        // Get error list
        $error = $mailer->php_get_errorlist();
        
        // Show errors
        echo "<br><b>Errorlist:</b><br>";  
        
        for ( $loop=0; $loop <= sizeof($error); $loop++ )
            echo $error[$loop]."<br>";        
    }
    
    $mailer->mime_clear_receiverlist();   
}

?>

Email MIME Header

Now we take a look into the generatet mail MIME header.

// The general mail MIME header. 
To: newuser@localhost
Subject: Im the subject or the reference
From: postmaster@localhost
Cc: newuser@localhost 
Bcc: newuser@localhost
X-Priority: 1 
X-Mailer: PHP-Lib-Extreme || http://phplibex.sourceforge.net/ 
  
// Its mixed because we have normal 8 Bit text and encoded data by base64.
MIME-Version: 1.0 Content-Type: multipart/mixed;
    
// Our generated boundary checksum in the class constructor
boundary=9DEF1CA5106303D80629BE16E11C3F5F
Message-Id: <XXXXXXXXXXXXXXXX>
Date: Wed, 22 Dec 2004 16:00:00 +0100 (CET) 
This is a multi-part message in MIME format 
  
// Here we start with our first part of the mail
--9DEF1CA5106303D80629BE16E11C3F5F 
Content-Type: text/plain Content-Transfer-Encoding: 8bit 
  
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Here is the main message or body message encoded by 8Bit !
// ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  
// Here starts the the second part with our first attachment
--9DEF1CA5106303D80629BE16E11C3F5F 
Content-Type: image/gif; name="tigger.gif" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename="tigger.gif" 
  
// :::::::::::::::::::::::::::::::::::::::::::::::::
// Here is the data of tigger.gif encoded by base64 !
// :::::::::::::::::::::::::::::::::::::::::::::::::
  
// Here starts the third part with our second attachment
--9DEF1CA5106303D80629BE16E11C3F5F 
Content-Type: text/plain; name="test.txt" 
Content-Transfer-Encoding: 8Bit 
Content-Disposition: attachment; filename="test.txt"
  
// :::::::::::::::::::::::::::::::::::::::::::::::::::
// Here is the data of test.txt encoded by 8Bit !
// :::::::::::::::::::::::::::::::::::::::::::::::::::
  
// Lets mark the end of the header
--9DEF1CA5106303D80629BE16E11C3F5F--

Related Links

SMTP RFC: http://www.rfc-editor.org/rfc/rfc821.txt
ESMTP RFC: http://www.rfc-editor.org/rfc/rfc1869.txt
ESMTP AUTH RFC: http://www.rfc-editor.org/rfc/rfc2554.txt
MIME-Types: http://www.iana.org/assignments/media-types

Generated on Fri Aug 26 08:13:54 2005 for PHP-Lib-Xtreme by  doxygen 1.4.3