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