LSaddon::mail: add MAIL_CATCH_ALL parameter

This commit is contained in:
Benjamin Renard 2023-01-11 19:08:04 +01:00
parent a717f051b9
commit cbf1ff1769
3 changed files with 58 additions and 19 deletions

View file

@ -60,13 +60,15 @@ define('MAIL_SEND_METHOD','smtp');
* o $params["persist"] - Indicates whether or not the SMTP connection * o $params["persist"] - Indicates whether or not the SMTP connection
* should persist over multiple calls to the send() method. * should persist over multiple calls to the send() method.
*/ */
$MAIL_SEND_PARAMS = NULL; $GLOBALS['MAIL_SEND_PARAMS'] = NULL;
/* /*
* Headers : * Headers :
*/ */
$MAIL_HEARDERS = array( $GLOBALS['MAIL_HEARDERS = array();
);
// Catch all sent emails
$GLOBALS['MAIL_CATCH_ALL'] = array();
</programlisting> </programlisting>
<para>Cet &LSaddon; offre la possibilité d'utilisé la fonction &php; <para>Cet &LSaddon; offre la possibilité d'utilisé la fonction &php;

View file

@ -73,10 +73,12 @@ define('MAIL_SEND_METHOD','smtp');
* o $params["persist"] - Indicates whether or not the SMTP connection * o $params["persist"] - Indicates whether or not the SMTP connection
* should persist over multiple calls to the send() method. * should persist over multiple calls to the send() method.
*/ */
$MAIL_SEND_PARAMS = NULL; $GLOBALS['MAIL_SEND_PARAMS'] = NULL;
/* /*
* Headers : * Headers :
*/ */
$MAIL_HEARDERS = array( $GLOBALS['MAIL_HEARDERS'] = array();
);
// Catch all sent emails
$GLOBALS['MAIL_CATCH_ALL'] = array();

View file

@ -20,7 +20,7 @@
******************************************************************************/ ******************************************************************************/
// Messages d'erreur // Error messages
// Support // Support
LSerror :: defineError('MAIL_SUPPORT_01', LSerror :: defineError('MAIL_SUPPORT_01',
@ -30,7 +30,7 @@ LSerror :: defineError('MAIL_SUPPORT_02',
___("MAIL Support : Pear::MAIL_MIME is missing.") ___("MAIL Support : Pear::MAIL_MIME is missing.")
); );
// Autres erreurs // Other errors
LSerror :: defineError('MAIL_00', LSerror :: defineError('MAIL_00',
___("MAIL Error : %{msg}") ___("MAIL Error : %{msg}")
); );
@ -40,16 +40,16 @@ LSerror :: defineError('MAIL_01',
); );
/** /**
* Verification du support MAIL par ldapSaisie * Check support of this LSaddon
* *
* @author Benjamin Renard <brenard@easter-eggs.com> * @author Benjamin Renard <brenard@easter-eggs.com>
* *
* @return boolean true si MAIL est pleinement supporté, false sinon * @return boolean true if this LSaddon is fully supported, false otherwise
*/ */
function LSaddon_mail_support() { function LSaddon_mail_support() {
$retval=true; $retval=true;
// Dependance de librairie // Lib dependencies (check/load)
if (!class_exists('Mail')) { if (!class_exists('Mail')) {
if(!LSsession::includeFile(PEAR_MAIL, true)) { if(!LSsession::includeFile(PEAR_MAIL, true)) {
LSerror :: addErrorCode('MAIL_SUPPORT_01'); LSerror :: addErrorCode('MAIL_SUPPORT_01');
@ -83,19 +83,49 @@ LSerror :: defineError('MAIL_01',
} }
/** /**
* Envoie d'un mail * Send an email
*
* @param string|array<string> $to Email recipient(s)
* @param string $subject Email subject
* @param string $msg Email body
* @param array<string,string> $headers Email headers
* @param array<string,string> $attachments Email attachments as an array with
* filepath as key and filename as value
* @param string $eol End of line string (default : \n)
* @param string $encoding Email encoding (default: utf8)
* @param boolean $html Set to true to send an HTML email (default: false)
* *
* @author Benjamin Renard <brenard@easter-eggs.com> * @author Benjamin Renard <brenard@easter-eggs.com>
* *
* @return boolean true si MAIL est pleinement supporté, false sinon * @return boolean true si MAIL est pleinement supporté, false sinon
*/ */
function sendMail($to, $subject, $msg, $headers=array(), $attachments=array(), $eol="\n", $encoding="utf8", $html=false) { function sendMail($to, $subject, $msg, $headers=array(), $attachments=array(),
global $MAIL_SEND_PARAMS, $MAIL_HEARDERS; $eol="\n", $encoding="utf8", $html=false) {
global $MAIL_SEND_PARAMS, $MAIL_HEARDERS, $MAIL_CATCH_ALL;
$mail_obj = Mail::factory(MAIL_SEND_METHOD, (isset($MAIL_SEND_PARAMS)?$MAIL_SEND_PARAMS:null)); $mail_obj = Mail::factory(MAIL_SEND_METHOD, (isset($MAIL_SEND_PARAMS)?$MAIL_SEND_PARAMS:null));
$logger = LSlog :: get_logger('LSaddon_mail');
if (isset($MAIL_HEARDERS) && is_array($MAIL_HEARDERS)) { if (isset($MAIL_HEARDERS) && is_array($MAIL_HEARDERS)) {
$headers = array_merge($headers,$MAIL_HEARDERS); $headers = array_merge($headers,$MAIL_HEARDERS);
} }
$logger -> trace(
'Mail catch all: '.(
isset($MAIL_CATCH_ALL) && $MAIL_CATCH_ALL?
varDump($MAIL_CATCH_ALL):'not set')
);
if (isset($MAIL_CATCH_ALL) && $MAIL_CATCH_ALL) {
$logger -> debug(
'Mail catch to '.
(is_array($MAIL_CATCH_ALL)?implode(',', $MAIL_CATCH_ALL):$MAIL_CATCH_ALL)
);
$msg .= sprintf(
_("\n\n\nMail initialy intended for %s."),
(is_array($to)?implode(',', $to):$to));
$to = (
is_array($MAIL_CATCH_ALL)?
implode(',', $MAIL_CATCH_ALL):$MAIL_CATCH_ALL
);
}
if (isset($headers['From'])) { if (isset($headers['From'])) {
$from = $headers['From']; $from = $headers['From'];
@ -112,11 +142,16 @@ LSerror :: defineError('MAIL_01',
); );
foreach(array_keys($headers) as $header) { foreach(array_keys($headers) as $header) {
if(strtoupper($header) == 'BCC') { if(in_array(strtoupper($header), array('BCC', 'CC'))) {
$to['BCC'] = $headers[$header]; if (isset($MAIL_CATCH_ALL) && $MAIL_CATCH_ALL) {
} $logger -> debug("Mail catched: remove $header header");
elseif(strtoupper($header) == 'CC') { $msg .= sprintf(
$to['CC'] = $headers[$header]; _("\nBCC: %s"),
(is_array($headers[$header])?implode(',', $headers[$header]):$headers[$header]));
unset($headers[$header]);
continue;
}
$to[strtoupper($header)] = $headers[$header];
} }
} }