2023-01-29 19:45:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace EesyPHP;
|
|
|
|
|
|
|
|
use PEAR;
|
|
|
|
use Mail;
|
|
|
|
use Mail_mime;
|
|
|
|
|
|
|
|
use finfo;
|
|
|
|
|
|
|
|
class Email {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialization
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-03-01 16:22:11 +01:00
|
|
|
public static function init() {
|
|
|
|
// Set config default values
|
|
|
|
App :: set_default(
|
|
|
|
'email',
|
|
|
|
array(
|
|
|
|
// Default sender
|
|
|
|
'sender' => null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sending method :
|
|
|
|
* - mail : use PHP mail function
|
|
|
|
* - sendmail : use sendmail system command
|
|
|
|
* - smtp : use an SMTP server (PHP PEAR Net_SMTP required)
|
|
|
|
*/
|
|
|
|
'send_method' => null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sending parameters
|
|
|
|
* @see http://pear.php.net/manual/en/package.mail.mail.factory.php
|
|
|
|
*/
|
|
|
|
'send_params' => array(),
|
|
|
|
|
|
|
|
// Catch all sent email recipient
|
|
|
|
'catch_all' => null,
|
|
|
|
|
|
|
|
// Default headers to add on all sent emails
|
|
|
|
'headers' => array(),
|
|
|
|
|
|
|
|
// PHP PEAR Mail lib path
|
|
|
|
'php_mail_path' => 'Mail.php',
|
|
|
|
|
|
|
|
// PHP PEAR Mail_mime lib path
|
|
|
|
'php_mail_mime_path' => 'Mail/mime.php',
|
|
|
|
)
|
|
|
|
);
|
2023-01-29 19:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public static function send($from, $to, $subject, $msg, $html=false, $attachments=null,
|
|
|
|
$headers=null, $encoding=null, $eol=null) {
|
|
|
|
if (!class_exists('Mail'))
|
2023-03-01 16:22:11 +01:00
|
|
|
require_once(App :: get('php_mail_path', null, 'string'));
|
2023-01-29 19:45:38 +01:00
|
|
|
if (!class_exists('Mail_mime'))
|
2023-03-01 16:22:11 +01:00
|
|
|
require_once(App :: get('php_mail_mime_path', null, 'string'));
|
2023-01-29 19:45:38 +01:00
|
|
|
|
2023-03-01 16:22:11 +01:00
|
|
|
$mail_obj = Mail::factory(
|
|
|
|
App :: get('email.send_method', null, 'string'),
|
|
|
|
App :: get('email.send_params', null, 'array')
|
|
|
|
);
|
2023-01-29 19:45:38 +01:00
|
|
|
|
|
|
|
if (!$headers) $headers = array();
|
2023-03-01 16:22:11 +01:00
|
|
|
$headers = array_merge($headers, App :: get('email.headers', null, 'array'));
|
2023-01-29 19:45:38 +01:00
|
|
|
|
|
|
|
Log :: trace(
|
|
|
|
'Mail catch all: %s',
|
2023-03-01 16:22:11 +01:00
|
|
|
App :: get('email.catch_all')?
|
|
|
|
vardump(App :: get('email.catch_all')):'not set'
|
2023-01-29 19:45:38 +01:00
|
|
|
);
|
2023-03-01 16:22:11 +01:00
|
|
|
if (App :: get('email.catch_all')) {
|
2023-01-29 19:45:38 +01:00
|
|
|
Log :: debug(
|
|
|
|
'Mail catch to %s',
|
2023-03-01 16:22:11 +01:00
|
|
|
is_array(App :: get('email.catch_all'))?
|
|
|
|
implode(',', App :: get('email.catch_all')):
|
|
|
|
App :: get('email.catch_all')
|
2023-01-29 19:45:38 +01:00
|
|
|
);
|
|
|
|
$msg .= sprintf(
|
|
|
|
(
|
|
|
|
$html?
|
2023-02-14 01:21:52 +01:00
|
|
|
I18n::_("</hr><p><small>Mail initialy intended for %s.</small></p>"):
|
|
|
|
I18n::_("\n\n\nMail initialy intended for %s.")
|
2023-01-29 19:45:38 +01:00
|
|
|
),
|
|
|
|
(is_array($to)?implode(',', $to):$to));
|
|
|
|
$headers["X-Orig-To"] = $to;
|
|
|
|
$to = (
|
2023-03-01 16:22:11 +01:00
|
|
|
is_array(App :: get('email.catch_all'))?
|
|
|
|
implode(',', App :: get('email.catch_all')):
|
|
|
|
App :: get('email.catch_all')
|
2023-01-29 19:45:38 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($subject) {
|
|
|
|
$headers["Subject"] = $subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($headers['From'])) {
|
|
|
|
if (!$from)
|
|
|
|
$from = $headers['From'];
|
|
|
|
unset($headers['From']);
|
|
|
|
}
|
|
|
|
elseif (!$from) {
|
2023-03-01 16:22:11 +01:00
|
|
|
$from = App::get('email.sender');
|
2023-01-29 19:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$headers["To"] = $to;
|
|
|
|
|
|
|
|
$to = array (
|
|
|
|
'To' => $to
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach(array_keys($headers) as $header) {
|
|
|
|
if(in_array(strtoupper($header), array('BCC', 'CC'))) {
|
2023-03-01 16:22:11 +01:00
|
|
|
if (App :: get('email.catch_all')) {
|
2023-01-29 19:45:38 +01:00
|
|
|
Log :: debug("Mail catched: remove $header header");
|
|
|
|
$msg .= sprintf(
|
|
|
|
(
|
|
|
|
$html?
|
2023-02-14 01:21:52 +01:00
|
|
|
I18n::_("<p><small>%s: %s</small></p>"):
|
|
|
|
I18n::_("\n%s: %s")
|
2023-01-29 19:45:38 +01:00
|
|
|
),
|
|
|
|
strtoupper($header),
|
|
|
|
(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 (PEAR::isError($ret)) {
|
|
|
|
$msg = "Error sending email: ".$ret -> getMessage();
|
|
|
|
Log :: error($msg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab
|