Friday, April 14, 2006

Wrapper function for the PEAR::Mail classes for easy transitioning

I was looking all over for a wrapper around the PEAR::Mail class for easy transitioning to PEAR::Mail usage. Found none, so here is a brand new piece of code that should do it. By now it is only tested using the mail-backend, although I see no reasons why it should not work with the other backends as well. Please contact me about any bug-corrections and I will update this post. Thanks!

Hope you'll find it useful. Cheers!


<?php
/**
* Code for use of the mail_wrapper_pear() wrapper-function for use instead of mail() for easy
* transition to PEAR::Mail-usage (http://pear.php.net/package/Mail). Could also be used as a
* trigger for various mail-statistics functions or implementing anti-spam protection.
*
* Usage:
* 1. Set the approriate settings below.
* 2. exchange "mail()" calls with "mail_wrapper_pear()" calls
*
* For long term transition to PEAR::Mail, read the PEAR manual, as this will give you insight in how
* to send html-formatted emails and attachments (http://pear.php.net/package/Mail_Mime) and make it
* possible to queue messages for bulk delivery (http://pear.php.net/package/Mail_Queue).
*
* @author Fredrik Wollsén <fredrik@digitalsteam.se>
* @since 2006-04-13
*/

include_once('Mail.php');
include_once('Mail/mime.php');

/* Configure PEAR::Mail backend here */
$_settings["PEAR"]["Mail"]["backendtype"] = "mail";
//$_settings["PEAR"]["Mail"]["backendtype"] = "sendmail";
//$_settings["PEAR"]["Mail"]["backendtype"] = "smtp";

switch ($_settings["PEAR"]["Mail"]["backendtype"]) {

case "mail":

/* Default from address (Optional for the 'mail'-backend) */
// $_settings["PEAR"]["Mail"]["From"] = null;

break;

case "sendmail":

/* Default from address */
$_settings["PEAR"]["Mail"]["From"] = null;

/* The location of the sendmail program on the filesystem. Default is /usr/bin/sendmail */
$_settings["PEAR"]["Mail"]["params"]["sendmail_path"] = null;

/* Additional parameters to pass to the sendmail program. */
$_settings["PEAR"]["Mail"]["params"]["sendmail_args"] = null;

break;

case "smtp":

/* Default from address */
$_settings["PEAR"]["Mail"]["From"] = null;

/* The server to connect. Default is localhost */
$_settings["PEAR"]["Mail"]["params"]["host"] = null;

/* The port to connect. Default is 25 */
$_settings["PEAR"]["Mail"]["params"]["port"] = null;

/* Whether or not to use SMTP authentication. Default is FALSE */
$_settings["PEAR"]["Mail"]["params"]["auth"] = null;

/* The username to use for SMTP authentication. */
$_settings["PEAR"]["Mail"]["params"]["username"] = null;

/* The password to use for SMTP authentication. */
$_settings["PEAR"]["Mail"]["params"]["password"] = null;

/* Indicates whether or not the SMTP connection should persist over multiple calls to the send() method. */
$_settings["PEAR"]["Mail"]["params"]["persist"] = null;

break;

default:

die("mail_wrapper_pear() not configured. Choose backend by uncommenting the approriate line (above if you are watching the source-code at the moment).");

}

/**
* Functions like php's mail() but uses the PEAR::Mail package to deliver mail. The only
* difference in functionality is that "$to" can consist of an array of recipients if desired.
*/

function mail_wrapper_pear($to, $subject, $message, $additional_headers = null, $additional_parameters = null) {
global $_settings;

if (!empty($additional_parameters) && $_settings["PEAR"]["Mail"]["backend"] != "mail") {
trigger_error("additional_parameters not supported in mail_wrapper_pear() for other backends than 'mail'", E_USER_WARNING);
//Optional:
//die();
}

/* If safe mode is disabled, $params will be passed as the fifth argument to the PHP mail() function. If $params is an array, its elements will be joined as a space-delimited string. */
$_settings["PEAR"]["Mail"]["params"] = $parameters;

/* Generate a PEAR-formatted array of the supplied header-information */
$headers_raw = explode("\r\n", $additional_headers);
foreach ($headers_raw as $headerentry) {
$headerparts = explode(":", $headerentry, 2);
$headers[trim($headerparts[0])] = trim($headerparts[1]);
}

$headers["Subject"] = $subject;
if (empty($headers["From"]) && empty($headers["from"])) $headers["From"] = $_settings["PEAR"]["Mail"]["From"];
$recipients = $to;

$body = $message;

if (empty($headers["From"])) die("You must specify a from-address. ");

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory($_settings["PEAR"]["Mail"]["backendtype"], $_settings["PEAR"]["Mail"]["params"]);

$mail_object->send($recipients, $headers, $body);

}
?>