2017-11-20 20:37:30 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Util/Emailer.php
|
|
|
|
*/
|
|
|
|
namespace Friendica\Util;
|
|
|
|
|
2020-01-26 19:23:58 +00:00
|
|
|
use Friendica\App;
|
|
|
|
use Friendica\Core\Config\IConfig;
|
2018-12-26 06:06:24 +00:00
|
|
|
use Friendica\Core\Hook;
|
2020-01-26 19:23:58 +00:00
|
|
|
use Friendica\Core\PConfig\IPConfig;
|
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
|
|
|
use Friendica\Object\EMail\IEmail;
|
2017-12-01 19:41:27 +00:00
|
|
|
use Friendica\Protocol\Email;
|
2020-01-26 19:23:58 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-11-20 20:37:30 +00:00
|
|
|
|
|
|
|
/**
|
2020-01-19 06:05:23 +00:00
|
|
|
* class to handle emailing
|
2017-11-20 20:37:30 +00:00
|
|
|
*/
|
|
|
|
class Emailer
|
|
|
|
{
|
2020-01-26 19:23:58 +00:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
/** @var IPConfig */
|
|
|
|
private $pConfig;
|
|
|
|
/** @var LoggerInterface */
|
|
|
|
private $logger;
|
|
|
|
/** @var App\BaseURL */
|
|
|
|
private $baseUrl;
|
|
|
|
|
|
|
|
public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
$this->pConfig = $pConfig;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->baseUrl = $baseURL;
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:37:30 +00:00
|
|
|
/**
|
|
|
|
* Send a multipart/alternative message with Text and HTML versions
|
|
|
|
*
|
2020-01-26 19:23:58 +00:00
|
|
|
* @param IEmail $email The email to send
|
2017-11-20 20:37:30 +00:00
|
|
|
*
|
2019-01-21 16:36:01 +00:00
|
|
|
* @return bool
|
2020-01-26 19:23:58 +00:00
|
|
|
* @throws InternalServerErrorException
|
2017-11-20 20:37:30 +00:00
|
|
|
*/
|
2020-01-26 19:23:58 +00:00
|
|
|
public function send(IEmail $email)
|
2017-11-20 20:37:30 +00:00
|
|
|
{
|
2019-05-26 03:45:10 +00:00
|
|
|
$params['sent'] = false;
|
|
|
|
|
2018-12-26 06:06:24 +00:00
|
|
|
Hook::callAll('emailer_send_prepare', $params);
|
2017-11-20 20:37:30 +00:00
|
|
|
|
2019-05-26 03:45:10 +00:00
|
|
|
if ($params['sent']) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:37:30 +00:00
|
|
|
$email_textonly = false;
|
2020-01-26 19:23:58 +00:00
|
|
|
if (!empty($email->getRecipientUid())) {
|
|
|
|
$email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly');
|
2017-11-20 20:37:30 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 19:23:58 +00:00
|
|
|
$fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
|
|
|
|
$fromEmail = $email->getFromEmail();
|
|
|
|
$replyTo = $email->getReplyTo();
|
|
|
|
$messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8');
|
2017-11-20 20:37:30 +00:00
|
|
|
|
|
|
|
// generate a mime boundary
|
2020-01-26 19:23:58 +00:00
|
|
|
$mimeBoundary = rand(0, 9) . '-'
|
|
|
|
. rand(100000000, 999999999) . '-'
|
|
|
|
. rand(100000000, 999999999) . '=:'
|
|
|
|
. rand(10000, 99999);
|
2017-11-20 20:37:30 +00:00
|
|
|
|
|
|
|
// generate a multipart/alternative message header
|
2020-01-26 19:23:58 +00:00
|
|
|
$messageHeader = $email->getAdditionalMailHeader() .
|
|
|
|
"From: $fromName <{$fromEmail}>\n" .
|
|
|
|
"Reply-To: $fromName <{$replyTo}>\n" .
|
|
|
|
"MIME-Version: 1.0\n" .
|
|
|
|
"Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\"";
|
2017-11-20 20:37:30 +00:00
|
|
|
|
|
|
|
// assemble the final multipart message body with the text and html types included
|
2020-01-26 19:41:53 +00:00
|
|
|
$textBody = chunk_split(base64_encode($email->getMessage(true)));
|
|
|
|
$htmlBody = chunk_split(base64_encode($email->getMessage()));
|
2020-01-26 19:23:58 +00:00
|
|
|
$multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\n" .
|
|
|
|
"Content-Transfer-Encoding: base64\n\n" .
|
|
|
|
$textBody . "\n";
|
|
|
|
|
|
|
|
if (!$email_textonly && !is_null($email->getMessage())) {
|
2017-11-20 20:37:30 +00:00
|
|
|
$multipartMessageBody .=
|
2020-01-26 19:23:58 +00:00
|
|
|
"--" . $mimeBoundary . "\n" . // text/html section
|
2017-11-20 20:37:30 +00:00
|
|
|
"Content-Type: text/html; charset=UTF-8\n" .
|
|
|
|
"Content-Transfer-Encoding: base64\n\n" .
|
|
|
|
$htmlBody . "\n";
|
|
|
|
}
|
|
|
|
$multipartMessageBody .=
|
2020-01-26 19:23:58 +00:00
|
|
|
"--" . $mimeBoundary . "--\n"; // message ending
|
2017-11-20 20:37:30 +00:00
|
|
|
|
2020-01-26 19:23:58 +00:00
|
|
|
if ($this->config->get('system', 'sendmail_params', true)) {
|
2020-01-26 00:04:54 +00:00
|
|
|
$sendmail_params = '-f ' . $fromEmail;
|
2018-06-19 20:23:42 +00:00
|
|
|
} else {
|
|
|
|
$sendmail_params = null;
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:37:30 +00:00
|
|
|
// send the message
|
2018-01-15 13:05:12 +00:00
|
|
|
$hookdata = [
|
2020-01-26 19:23:58 +00:00
|
|
|
'to' => $email->getToEmail(),
|
|
|
|
'subject' => $messageSubject,
|
|
|
|
'body' => $multipartMessageBody,
|
|
|
|
'headers' => $messageHeader,
|
2019-05-26 03:45:10 +00:00
|
|
|
'parameters' => $sendmail_params,
|
2020-01-26 19:23:58 +00:00
|
|
|
'sent' => false,
|
2018-01-15 13:05:12 +00:00
|
|
|
];
|
2018-12-26 05:40:12 +00:00
|
|
|
|
2020-01-26 19:23:58 +00:00
|
|
|
Hook::callAll('emailer_send', $hookdata);
|
2018-12-26 05:40:12 +00:00
|
|
|
|
2019-05-26 03:45:10 +00:00
|
|
|
if ($hookdata['sent']) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:37:30 +00:00
|
|
|
$res = mail(
|
2018-06-19 20:23:42 +00:00
|
|
|
$hookdata['to'],
|
|
|
|
$hookdata['subject'],
|
|
|
|
$hookdata['body'],
|
|
|
|
$hookdata['headers'],
|
|
|
|
$hookdata['parameters']
|
2017-11-20 20:37:30 +00:00
|
|
|
);
|
2020-01-26 19:23:58 +00:00
|
|
|
$this->logger->debug('header ' . 'To: ' . $email->getToEmail() . '\n' . $messageHeader);
|
|
|
|
$this->logger->debug('return value ' . (($res) ? 'true' : 'false'));
|
2017-11-20 20:37:30 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
}
|