2008-09-25 17:15:33 +02:00
|
|
|
<?php
|
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (C) 2007 Easter-eggs
|
2021-04-13 18:04:19 +02:00
|
|
|
* https://ldapsaisie.org
|
2008-09-25 17:15:33 +02:00
|
|
|
*
|
|
|
|
* Author: See AUTHORS file in top-level directory.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
2009-01-12 11:57:27 +01:00
|
|
|
// Messages d'erreur
|
|
|
|
|
|
|
|
// Support
|
2009-01-25 15:37:03 +01:00
|
|
|
LSerror :: defineError('MAIL_SUPPORT_01',
|
2020-08-25 17:31:50 +02:00
|
|
|
___("MAIL Support : Pear::MAIL is missing.")
|
2009-01-12 11:57:27 +01:00
|
|
|
);
|
2020-04-29 15:26:15 +02:00
|
|
|
LSerror :: defineError('MAIL_SUPPORT_02',
|
2020-08-25 17:31:50 +02:00
|
|
|
___("MAIL Support : Pear::MAIL_MIME is missing.")
|
2020-04-29 15:26:15 +02:00
|
|
|
);
|
2009-01-12 11:57:27 +01:00
|
|
|
|
|
|
|
// Autres erreurs
|
2009-01-25 15:37:03 +01:00
|
|
|
LSerror :: defineError('MAIL_00',
|
2020-08-25 17:31:50 +02:00
|
|
|
___("MAIL Error : %{msg}")
|
2009-01-12 11:57:27 +01:00
|
|
|
);
|
|
|
|
|
2009-01-25 15:37:03 +01:00
|
|
|
LSerror :: defineError('MAIL_01',
|
2020-08-25 17:31:50 +02:00
|
|
|
___("MAIL : Error sending your email")
|
2009-01-12 11:57:27 +01:00
|
|
|
);
|
2020-04-29 15:26:15 +02:00
|
|
|
|
2008-09-25 17:15:33 +02:00
|
|
|
/**
|
|
|
|
* Verification du support MAIL par ldapSaisie
|
2020-04-29 15:26:15 +02:00
|
|
|
*
|
2008-09-25 17:15:33 +02:00
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*
|
|
|
|
* @retval boolean true si MAIL est pleinement supporté, false sinon
|
|
|
|
*/
|
|
|
|
function LSaddon_mail_support() {
|
|
|
|
$retval=true;
|
|
|
|
|
|
|
|
// Dependance de librairie
|
|
|
|
if (!class_exists('Mail')) {
|
2020-04-29 15:15:41 +02:00
|
|
|
if(!LSsession::includeFile(PEAR_MAIL, true)) {
|
2009-01-24 18:45:14 +01:00
|
|
|
LSerror :: addErrorCode('MAIL_SUPPORT_01');
|
2008-09-25 17:15:33 +02:00
|
|
|
$retval=false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 15:26:15 +02:00
|
|
|
|
|
|
|
if (!class_exists('Mail_mime')) {
|
|
|
|
if(!LSsession::includeFile(PEAR_MAIL_MIME, true)) {
|
|
|
|
LSerror :: addErrorCode('MAIL_SUPPORT_02');
|
|
|
|
$retval=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 11:26:06 +01:00
|
|
|
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'
|
|
|
|
);
|
|
|
|
|
2008-09-25 17:15:33 +02:00
|
|
|
return $retval;
|
|
|
|
}
|
2020-04-29 15:26:15 +02:00
|
|
|
|
2008-09-25 17:15:33 +02:00
|
|
|
/**
|
|
|
|
* Envoie d'un mail
|
2020-04-29 15:26:15 +02:00
|
|
|
*
|
2008-09-25 17:15:33 +02:00
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*
|
|
|
|
* @retval boolean true si MAIL est pleinement supporté, false sinon
|
|
|
|
*/
|
2020-09-16 10:35:47 +02:00
|
|
|
function sendMail($to, $subject, $msg, $headers=array(), $attachments=array(), $eol="\n", $encoding="utf8", $html=false) {
|
2019-03-12 12:49:19 +01:00
|
|
|
global $MAIL_SEND_PARAMS, $MAIL_HEARDERS;
|
|
|
|
$mail_obj = Mail::factory(MAIL_SEND_METHOD, (isset($MAIL_SEND_PARAMS)?$MAIL_SEND_PARAMS:null));
|
2020-04-29 15:26:15 +02:00
|
|
|
|
2019-03-12 12:49:19 +01:00
|
|
|
if (isset($MAIL_HEARDERS) && is_array($MAIL_HEARDERS)) {
|
2008-10-14 18:21:36 +02:00
|
|
|
$headers = array_merge($headers,$MAIL_HEARDERS);
|
2008-09-25 17:15:33 +02:00
|
|
|
}
|
2020-04-29 15:26:15 +02:00
|
|
|
|
|
|
|
if (isset($headers['From'])) {
|
|
|
|
$from = $headers['From'];
|
|
|
|
unset($headers['From']);
|
|
|
|
}
|
|
|
|
elseif (LSsession :: getEmailSender() != "") {
|
|
|
|
$from = LSsession :: getEmailSender();
|
2008-09-25 17:15:33 +02:00
|
|
|
}
|
2020-04-29 15:26:15 +02:00
|
|
|
else {
|
|
|
|
$from = null;
|
2008-09-25 17:15:33 +02:00
|
|
|
}
|
2020-04-29 15:26:15 +02:00
|
|
|
|
2008-10-31 13:12:31 +01:00
|
|
|
$headers["To"] = $to;
|
2012-09-07 13:33:57 +02:00
|
|
|
|
|
|
|
$to = array (
|
|
|
|
'To' => $to
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach(array_keys($headers) as $header) {
|
|
|
|
if(strtoupper($header) == 'BCC') {
|
|
|
|
$to['BCC'] = $headers[$header];
|
|
|
|
}
|
|
|
|
elseif(strtoupper($header) == 'CC') {
|
|
|
|
$to['CC'] = $headers[$header];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 15:26:15 +02:00
|
|
|
$mime = new Mail_mime(
|
|
|
|
array(
|
|
|
|
'eol' => $eol,
|
2020-09-16 10:35:47 +02:00
|
|
|
($html?'html_charset':'text_charset') => $encoding,
|
2020-04-29 15:26:15 +02:00
|
|
|
'head_charset' => $encoding,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($from)
|
|
|
|
$mime->setFrom($from);
|
|
|
|
|
|
|
|
if ($subject)
|
|
|
|
$mime->setSubject($subject);
|
|
|
|
|
2020-09-16 10:35:47 +02:00
|
|
|
if ($html)
|
|
|
|
$mime->setHTMLBody($msg);
|
|
|
|
else
|
|
|
|
$mime->setTXTBody($msg);
|
2020-04-29 15:26:15 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2008-09-25 17:15:33 +02:00
|
|
|
if ($ret instanceof PEAR_Error) {
|
2009-01-24 18:45:14 +01:00
|
|
|
LSerror :: addErrorCode('MAIL_01');
|
2020-04-29 15:26:15 +02:00
|
|
|
LSerror :: addErrorCode('MAIL_00', $ret -> getMessage());
|
2008-09-25 17:15:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-17 11:26:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
if (php_sapi_name() != 'cli')
|
2020-11-17 17:47:50 +01:00
|
|
|
return true;
|
2020-11-17 11:26:06 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|