2020-04-18 00:51:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// Load PHP PEAR Mail and Mail_mime libs
|
2023-01-29 11:51:41 +01:00
|
|
|
require_once(isset($php_mail_path)?$php_mail_path:"Mail.php");
|
|
|
|
require_once(isset($php_mail_mime_path)?$php_mail_mime_path:"Mail/mime.php");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an email
|
|
|
|
*
|
|
|
|
* @param string|null $from Email sender
|
|
|
|
* @param string|array<string> $to Email recipient(s)
|
|
|
|
* @param string $subject Email subject
|
|
|
|
* @param string $msg Email body
|
|
|
|
* @param boolean $html Set to true to send an HTML email (default: false)
|
|
|
|
* @param array<string,string>|null $attachments Email attachments as an array with
|
|
|
|
* filepath as key and filename as value
|
|
|
|
* @param array<string,string>|null $headers Email headers
|
|
|
|
* @param string|null $encoding Email encoding (default: utf8)
|
|
|
|
* @param string|null $eol End of line string (default : \n)
|
|
|
|
*
|
|
|
|
* @return boolean true If mail was sent, false otherwise
|
|
|
|
*/
|
|
|
|
function send_mail($from, $to, $subject, $msg, $html=false, $attachments=null, $headers=null,
|
|
|
|
$encoding=null, $eol=null) {
|
|
|
|
global $mail_send_method, $mail_headers, $mail_send_params, $mail_sender, $mail_catch_all;
|
|
|
|
$mail_obj = & Mail::factory($mail_send_method, $mail_send_params);
|
|
|
|
|
|
|
|
if (!$headers) $headers = array();
|
|
|
|
if(isset($mail_headers) && is_array($mail_headers)) {
|
|
|
|
$headers = array_merge($headers, $mail_headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
logging(
|
|
|
|
'TRACE', 'Mail catch all: %s',
|
|
|
|
isset($mail_catch_all) && $mail_catch_all?
|
|
|
|
vardump($mail_catch_all):'not set'
|
|
|
|
);
|
|
|
|
if (isset($mail_catch_all) && $mail_catch_all) {
|
|
|
|
logging(
|
|
|
|
'DEBUG', 'Mail catch to %s',
|
|
|
|
is_array($mail_catch_all)?implode(',', $mail_catch_all):$mail_catch_all
|
|
|
|
);
|
|
|
|
$msg .= sprintf(
|
|
|
|
(
|
|
|
|
$html?
|
|
|
|
_("</hr><p><small>Mail initialy intended for %s.</small></p>"):
|
|
|
|
_("\n\n\nMail initialy intended for %s.")
|
|
|
|
),
|
|
|
|
(is_array($to)?implode(',', $to):$to));
|
|
|
|
$headers["X-Orig-To"] = $to;
|
|
|
|
$to = (
|
|
|
|
is_array($mail_catch_all)?
|
|
|
|
implode(',', $mail_catch_all):$mail_catch_all
|
|
|
|
);
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
|
2023-01-29 11:51:41 +01:00
|
|
|
if ($subject) {
|
|
|
|
$headers["Subject"] = $subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($headers['From'])) {
|
|
|
|
if (!$from)
|
|
|
|
$from = $headers['From'];
|
|
|
|
unset($headers['From']);
|
|
|
|
}
|
|
|
|
elseif (!$from) {
|
|
|
|
$from = $mail_sender;
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
2023-01-29 11:51:41 +01:00
|
|
|
|
2020-04-18 00:51:33 +02:00
|
|
|
$headers["To"] = $to;
|
|
|
|
|
|
|
|
$to = array (
|
|
|
|
'To' => $to
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach(array_keys($headers) as $header) {
|
2023-01-29 11:51:41 +01:00
|
|
|
if(in_array(strtoupper($header), array('BCC', 'CC'))) {
|
|
|
|
if (isset($mail_catch_all) && $mail_catch_all) {
|
|
|
|
logging('DEBUG', "Mail catched: remove $header header");
|
|
|
|
$msg .= sprintf(
|
|
|
|
(
|
|
|
|
$html?
|
|
|
|
_("<p><small>%s: %s</small></p>"):
|
|
|
|
_("\n%s: %s")
|
|
|
|
),
|
|
|
|
strtoupper($header),
|
|
|
|
(is_array($headers[$header])?implode(',', $headers[$header]):$headers[$header]));
|
|
|
|
unset($headers[$header]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$to[strtoupper($header)] = $headers[$header];
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 11:51:41 +01:00
|
|
|
if (!$encoding) $encoding = "utf8";
|
2020-04-18 00:51:33 +02:00
|
|
|
$mime = new Mail_mime(
|
|
|
|
array(
|
2023-01-29 11:51:41 +01:00
|
|
|
'eol' => ($eol?$eol:"\n"),
|
|
|
|
($html?'html_charset':'text_charset') => $encoding,
|
|
|
|
'head_charset' => $encoding,
|
2020-04-18 00:51:33 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2023-01-29 11:51:41 +01:00
|
|
|
if ($from)
|
|
|
|
$mime->setFrom($from);
|
|
|
|
|
|
|
|
if ($subject)
|
2020-04-18 00:51:33 +02:00
|
|
|
$mime->setSubject($subject);
|
|
|
|
|
2023-01-29 11:51:41 +01:00
|
|
|
if ($html)
|
|
|
|
$mime->setHTMLBody($msg);
|
|
|
|
else
|
|
|
|
$mime->setTXTBody($msg);
|
2020-04-18 00:51:33 +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);
|
|
|
|
|
2023-01-29 11:51:41 +01:00
|
|
|
$ret = $mail_obj -> send($to, $headers, $body);
|
2020-04-18 00:51:33 +02:00
|
|
|
|
2023-01-29 11:51:41 +01:00
|
|
|
if (PEAR::isError($ret)) {
|
|
|
|
$msg = "Error sending email: ".$ret -> getMessage();
|
|
|
|
logging('ERROR', $msg);
|
|
|
|
return false;
|
2020-04-18 00:51:33 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2023-01-29 11:51:41 +01:00
|
|
|
|
|
|
|
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab
|