Welcome to Geeklog, Anonymous Sunday, October 06 2024 @ 09:48 pm EDT
Geeklog Forums
Feature Request: Allow mail to be handled by plugins
Status: offline
jmucchiello
Forum User
Full Member
Registered: 08/29/05
Posts: 985
It would be nice if plugins could take over sendmail duty. This would allow a plugin (ultimately, only one plugin could do this) to save/retain outgoing emails, throttle email for webhosts that have extremely low hourly mail requirements, and similar duties.
First task is to split the existing COM_mail into public interface and actual functionality since the actual functionality of COM_mail is very good, it just lacks hooks.
As for the hooks, create a $_CONF['sendmail_plugin'] which can be none (send no mail), internal (use normal internal mailer), custom (call CUSTOM_email) or the name of a plugin (calling plugin_sendmailhandler_$plugin). For backwards compatibility, if the $_CONF is not set, it uses CUSTOM_email if exists or the internal mailer otherwise.
Here is the code:
function COM_mail( $to, $subject, $message, $from = '', $html = false, $priority = 0, $cc = '' )
{
global $_CONF, $_PLUGINS;
static $mail_func = null;
if ($mail_func === null) {
if (isset($_CONF['sendmail_plugin'])) {
$pi_name = $_CONF['sendmail_plugin'];
if ($pi_name == 'none') {
$mail_func = 'none';
} else if ($pi_name == 'internal') {
$mail_func = 'INTERNAL_sendmail';
} else if ($pi_name == 'custom' && function_exists('CUSTOM_mail')) {
$mail_func = 'CUSTOM_mail';
} else {
$function = 'plugin_sendmailhandler_'.$pi_name;
if (in_array($pi_name, $_PLUGINS) && function_exists($function)) {
$mail_func = $function;
}
}
} else if (function_exists('CUSTOM_mail')) {
$mail_func = 'CUSTOM_mail';
}
if ($mail_func === null) {
foreach ($_PLUGINS as $pi_name) {
$function = 'plugin_sendmailhandler_'.$pi_name;
if (function_exists($function)) {
$mail_func = $function;
break; // first found, first used.
}
}
}
if ($mail_func === null) {
$mail_func = 'INTERNAL_sendmail';
}
}
if ($mail_func == 'none') {
return true; // success
}
if( empty( $from ))
{
$from = COM_formatEmailAddress( $_CONF['site_name'], $_CONF['site_mail']);
}
$to = substr( $to, 0, strcspn( $to, "\r\n" ));
$cc = substr( $cc, 0, strcspn( $cc, "\r\n" ));
$from = substr( $from, 0, strcspn( $from, "\r\n" ));
$subject = substr( $subject, 0, strcspn( $subject, "\r\n" ));
$subject = COM_emailEscape( $subject );
return $mail_func($to, $subject, $message, $from, $html, $priority, $cc);
}
function INTERNAL_sendMail( $to, $subject, $message, $from, $html, $priority, $cc)
{
global $_CONF;
static $mailobj;
include_once( 'Mail.php' );
include_once( 'Mail/RFC822.php' );
// as existing COM_mail() from here
First task is to split the existing COM_mail into public interface and actual functionality since the actual functionality of COM_mail is very good, it just lacks hooks.
As for the hooks, create a $_CONF['sendmail_plugin'] which can be none (send no mail), internal (use normal internal mailer), custom (call CUSTOM_email) or the name of a plugin (calling plugin_sendmailhandler_$plugin). For backwards compatibility, if the $_CONF is not set, it uses CUSTOM_email if exists or the internal mailer otherwise.
Here is the code:
Text Formatted Code
// adapted from CVSfunction COM_mail( $to, $subject, $message, $from = '', $html = false, $priority = 0, $cc = '' )
{
global $_CONF, $_PLUGINS;
static $mail_func = null;
if ($mail_func === null) {
if (isset($_CONF['sendmail_plugin'])) {
$pi_name = $_CONF['sendmail_plugin'];
if ($pi_name == 'none') {
$mail_func = 'none';
} else if ($pi_name == 'internal') {
$mail_func = 'INTERNAL_sendmail';
} else if ($pi_name == 'custom' && function_exists('CUSTOM_mail')) {
$mail_func = 'CUSTOM_mail';
} else {
$function = 'plugin_sendmailhandler_'.$pi_name;
if (in_array($pi_name, $_PLUGINS) && function_exists($function)) {
$mail_func = $function;
}
}
} else if (function_exists('CUSTOM_mail')) {
$mail_func = 'CUSTOM_mail';
}
if ($mail_func === null) {
foreach ($_PLUGINS as $pi_name) {
$function = 'plugin_sendmailhandler_'.$pi_name;
if (function_exists($function)) {
$mail_func = $function;
break; // first found, first used.
}
}
}
if ($mail_func === null) {
$mail_func = 'INTERNAL_sendmail';
}
}
if ($mail_func == 'none') {
return true; // success
}
if( empty( $from ))
{
$from = COM_formatEmailAddress( $_CONF['site_name'], $_CONF['site_mail']);
}
$to = substr( $to, 0, strcspn( $to, "\r\n" ));
$cc = substr( $cc, 0, strcspn( $cc, "\r\n" ));
$from = substr( $from, 0, strcspn( $from, "\r\n" ));
$subject = substr( $subject, 0, strcspn( $subject, "\r\n" ));
$subject = COM_emailEscape( $subject );
return $mail_func($to, $subject, $message, $from, $html, $priority, $cc);
}
function INTERNAL_sendMail( $to, $subject, $message, $from, $html, $priority, $cc)
{
global $_CONF;
static $mailobj;
include_once( 'Mail.php' );
include_once( 'Mail/RFC822.php' );
// as existing COM_mail() from here
6
6
Quote
All times are EDT. The time is now 09:48 pm.
- Normal Topic
- Sticky Topic
- Locked Topic
- New Post
- Sticky Topic W/ New Post
- Locked Topic W/ New Post
- View Anonymous Posts
- Able to post
- Filtered HTML Allowed
- Censored Content