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