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');
|
2020-01-26 18:01:17 -05:00
|
|
|
$fromAddress = $email->getFromAddress();
|
2020-01-26 14:23:58 -05:00
|
|
|
$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() .
|
2020-01-26 18:01:17 -05:00
|
|
|
"From: $fromName <{$fromAddress}>\n" .
|
2020-01-26 14:23:58 -05:00
|
|
|
"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:41:53 -05:00
|
|
|
$textBody = chunk_split(base64_encode($email->getMessage(true)));
|
|
|
|
$htmlBody = chunk_split(base64_encode($email->getMessage()));
|
2020-01-26 14:23:58 -05: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 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-26 18:01:17 -05:00
|
|
|
$sendmail_params = '-f ' . $fromAddress;
|
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 17:47:16 -05:00
|
|
|
'to' => $email->getToAddress(),
|
2020-01-26 14:23:58 -05:00
|
|
|
'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 17:47:16 -05:00
|
|
|
$this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader);
|
2020-01-26 14:23:58 -05:00
|
|
|
$this->logger->debug('return value ' . (($res) ? 'true' : 'false'));
|
2017-11-20 15:37:30 -05:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
}
|