2014-05-04 13:39:00 +02:00
|
|
|
<?php
|
|
|
|
class Mailer
|
|
|
|
{
|
2014-05-07 17:39:40 +02:00
|
|
|
private static $mailCounter;
|
|
|
|
private static $mailsSent;
|
|
|
|
private static $mock;
|
2014-05-07 09:26:04 +02:00
|
|
|
|
|
|
|
public static function init()
|
|
|
|
{
|
|
|
|
self::$mailCounter = 0;
|
2014-05-07 17:39:40 +02:00
|
|
|
self::$mailsSent = [];
|
2014-05-07 09:26:04 +02:00
|
|
|
self::$mock = false;
|
|
|
|
}
|
2014-05-04 13:39:00 +02:00
|
|
|
|
|
|
|
public static function getMailCounter()
|
|
|
|
{
|
|
|
|
return self::$mailCounter;
|
|
|
|
}
|
|
|
|
|
2014-05-07 17:39:40 +02:00
|
|
|
public static function getMailsSent()
|
|
|
|
{
|
|
|
|
return self::$mailsSent;
|
|
|
|
}
|
|
|
|
|
2014-05-07 09:26:04 +02:00
|
|
|
public static function mockSending()
|
|
|
|
{
|
|
|
|
self::$mock = true;
|
|
|
|
}
|
|
|
|
|
2014-05-04 13:39:00 +02:00
|
|
|
public static function sendMail(Mail $mail, array $tokens = [])
|
|
|
|
{
|
2014-05-07 09:26:04 +02:00
|
|
|
$host = isset($_SERVER['HTTP_HOST'])
|
|
|
|
? $_SERVER['HTTP_HOST']
|
|
|
|
: '';
|
|
|
|
$ip = isset($_SERVER['SERVER_ADDR'])
|
|
|
|
? $_SERVER['SERVER_ADDR']
|
|
|
|
: '';
|
|
|
|
|
2014-05-04 13:39:00 +02:00
|
|
|
if (!isset($tokens['host']))
|
2014-05-07 09:26:04 +02:00
|
|
|
$tokens['host'] = $host;
|
2014-05-04 13:39:00 +02:00
|
|
|
|
|
|
|
if (!isset($tokens['nl']))
|
|
|
|
$tokens['nl'] = PHP_EOL;
|
|
|
|
|
|
|
|
$body = wordwrap(TextHelper::replaceTokens($mail->body, $tokens), 70);
|
|
|
|
$subject = TextHelper::replaceTokens($mail->subject, $tokens);
|
|
|
|
$senderName = TextHelper::replaceTokens($mail->senderName, $tokens);
|
|
|
|
$senderEmail = TextHelper::replaceTokens($mail->senderEmail, $tokens);
|
|
|
|
$recipientEmail = $mail->recipientEmail;
|
|
|
|
|
|
|
|
if (empty($recipientEmail))
|
|
|
|
throw new SimpleException('Destination e-mail address was not found');
|
|
|
|
|
2014-05-07 09:26:04 +02:00
|
|
|
$messageId = $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '@' . $host;
|
2014-05-04 13:39:00 +02:00
|
|
|
|
|
|
|
$headers = [];
|
|
|
|
$headers []= sprintf('MIME-Version: 1.0');
|
|
|
|
$headers []= sprintf('Content-Transfer-Encoding: 7bit');
|
|
|
|
$headers []= sprintf('Date: %s', date('r', $_SERVER['REQUEST_TIME']));
|
|
|
|
$headers []= sprintf('Message-ID: <%s>', $messageId);
|
|
|
|
$headers []= sprintf('From: %s <%s>', $senderName, $senderEmail);
|
|
|
|
$headers []= sprintf('Reply-To: %s', $senderEmail);
|
|
|
|
$headers []= sprintf('Return-Path: %s', $senderEmail);
|
|
|
|
$headers []= sprintf('Subject: %s', $subject);
|
|
|
|
$headers []= sprintf('Content-Type: text/plain; charset=utf-8', $subject);
|
|
|
|
$headers []= sprintf('X-Mailer: PHP/%s', phpversion());
|
2014-05-07 09:26:04 +02:00
|
|
|
$headers []= sprintf('X-Originating-IP: %s', $ip);
|
2014-05-04 13:39:00 +02:00
|
|
|
$encodedSubject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
|
2014-05-07 09:26:04 +02:00
|
|
|
|
|
|
|
if (!self::$mock)
|
|
|
|
mail($recipientEmail, $encodedSubject, $body, implode("\r\n", $headers), '-f' . $senderEmail);
|
2014-05-04 13:39:00 +02:00
|
|
|
|
2014-05-07 17:39:40 +02:00
|
|
|
$mail->tokens = $tokens;
|
|
|
|
self::$mailsSent []= $mail;
|
2014-05-04 13:39:00 +02:00
|
|
|
self::$mailCounter ++;
|
|
|
|
|
2014-05-04 19:23:09 +02:00
|
|
|
Logger::log('Sending e-mail with subject "{subject}" to {mail}', [
|
2014-05-04 13:39:00 +02:00
|
|
|
'subject' => $subject,
|
|
|
|
'mail' => $recipientEmail]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function sendMailWithTokenLink(
|
|
|
|
UserEntity $user,
|
|
|
|
$linkDestination,
|
|
|
|
Mail $mail,
|
|
|
|
array $tokens = [])
|
|
|
|
{
|
|
|
|
//prepare unique user token
|
|
|
|
$token = TokenModel::spawn();
|
|
|
|
$token->setUser($user);
|
2014-05-07 17:39:40 +02:00
|
|
|
$token->setUsed(false);
|
2014-05-07 20:33:52 +02:00
|
|
|
$token->setExpirationTime(null);
|
2014-05-04 13:39:00 +02:00
|
|
|
TokenModel::save($token);
|
|
|
|
|
2014-05-23 23:34:50 +02:00
|
|
|
$tokens['link'] = Core::getRouter()->linkTo($linkDestination, ['tokenText' => $token->getText()]);
|
2014-05-07 17:39:40 +02:00
|
|
|
$tokens['token'] = $token->getText(); //yeah
|
2014-05-04 13:39:00 +02:00
|
|
|
|
|
|
|
return self::sendMail($mail, $tokens);
|
|
|
|
}
|
|
|
|
}
|