* * @return boolean true if this LSaddon is fully supported, false otherwise */ function LSaddon_mail_support() { $retval=true; // Lib dependencies (check/load) if (!class_exists('Mail')) { if(!LSsession::includeFile(PEAR_MAIL, true)) { LSerror :: addErrorCode('MAIL_SUPPORT_01'); $retval=false; } } if (!class_exists('Mail_mime')) { if(!LSsession::includeFile(PEAR_MAIL_MIME, true)) { LSerror :: addErrorCode('MAIL_SUPPORT_02'); $retval=false; } } if ($retval) LScli :: add_command( 'test_send_mail', 'cli_test_send_mail', 'Send a test email', "[-s subject] [-b body] [recipient]", array ( " -s/--subject The test email subject (optional)", " -b/--body The test email body (optional)", " recipient The test email recipient (required)", ), false, // This command does not need LDAP connection 'cli_test_send_mail_autocompleter' ); return $retval; } /** * Send an email * * @param string|array $to Email recipient(s) * @param string $subject Email subject * @param string $msg Email body * @param array|null $headers Email headers * @param array|null $attachments Email attachments as an array with * filepath as key and filename as value * @param string|null $eol End of line string (default : \n) * @param string|null $encoding Email encoding (default: utf8) * @param boolean $html Set to true to send an HTML email (default: false) * * @author Benjamin Renard * * @return boolean true si MAIL est pleinement supporté, false sinon */ function sendMail($to, $subject, $msg, $headers=null, $attachments=null, $eol=null, $encoding=null, $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)); $logger = LSlog :: get_logger('LSaddon_mail'); if (!$headers) $headers = array(); if (isset($MAIL_HEARDERS) && is_array($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'])) { $from = $headers['From']; unset($headers['From']); } else { $from = LSsession :: getEmailSender(); } $headers["To"] = $to; $to = array ( 'To' => $to ); foreach(array_keys($headers) as $header) { if(in_array(strtoupper($header), array('BCC', 'CC'))) { if (isset($MAIL_CATCH_ALL) && $MAIL_CATCH_ALL) { $logger -> debug("Mail catched: remove $header header"); $msg .= sprintf( _("\nBCC: %s"), (is_array($headers[$header])?implode(',', $headers[$header]):$headers[$header])); unset($headers[$header]); continue; } $to[strtoupper($header)] = $headers[$header]; } } if (!$encoding) $encoding = "utf8"; $mime = new Mail_mime( array( 'eol' => ($eol?$eol:"\n"), ($html?'html_charset':'text_charset') => $encoding, 'head_charset' => $encoding, ) ); if ($from) $mime->setFrom($from); if ($subject) $mime->setSubject($subject); if ($html) $mime->setHTMLBody($msg); else $mime->setTXTBody($msg); if (is_array($attachments) && !empty($attachments)) { $finfo = new finfo(FILEINFO_MIME_TYPE); foreach ($attachments as $file => $filename) { $mime->addAttachment($file, $finfo->file($file), $filename); } } $body = $mime->get(); $headers = $mime->headers($headers); $ret = $mail_obj -> send($to, $headers, $body); if ($ret instanceof PEAR_Error) { LSerror :: addErrorCode('MAIL_01'); LSerror :: addErrorCode('MAIL_00', $ret -> getMessage()); return false; } return true; } if (php_sapi_name() != 'cli') return true; /** * CLI command to send a test email * @param array $command_args Command arguments * @return bool */ function cli_test_send_mail($command_args) { $recipient = null; $subject = "Test email"; $body = "This is a test message."; for ($i=0; $i < count($command_args); $i++) { switch ($command_args[$i]) { case '--subject': case '-s': $i++; if (!isset($command_args[$i])) LScli :: usage("You must provide the email subject after -s/--subject parameter."); $subject = $command_args[$i]; if (!$subject) LScli :: usage("Invalid subject provided."); break; case '--body': case '-b': $i++; if (!isset($command_args[$i])) LScli :: usage("You must provide the email body after -b/--body parameter."); $body = $command_args[$i]; if (!$body) LScli :: usage("Invalid body provided."); break; default: if (!$recipient && checkEmail($command_args[$i])) $recipient = $command_args[$i]; else LScli :: usage("Invalid parameter '".$command_args[$i]."'."); } } if (!$recipient) LScli :: usage("You must provide test email recipient as first positional parameter"); $logger = LSlog :: get_logger('LSaddon_mail'); if (!sendMail($recipient, $subject, $body)) { $logger -> error("Fail to send test email sent to '$recipient'."); return false; } $logger -> info("Test email sent to '$recipient'."); return true; } /** * Args autocompleter for CLI test_send_mail command * * @param array $comp_words List of already typed words of the command * @param int $comp_word_num The command word number to autocomplete * @param string $comp_word The command word to autocomplete * @param array $opts List of global available options * * @return array List of available options for the word to autocomplete **/ function cli_test_send_mail_autocompleter($comp_words, $comp_word_num, $comp_word, $opts) { switch ($comp_words[$comp_word_num-1]) { case '-s': case '--subject': case '-b': case '--body': return array(); break; } $opts = array_merge( $opts, array ( '-s', '--subject', '-b', '--body', ) ); return LScli :: autocomplete_opts($opts, $comp_word); }