mirror of
https://github.com/Mibew/mibew.git
synced 2024-11-15 16:44:11 +03:00
Use SwiftMailer to send emails
This commit is contained in:
parent
a66e23fe9b
commit
4e4f0adfd6
@ -26,7 +26,8 @@
|
||||
"symfony/translation": "2.6.*",
|
||||
"tedivm/stash": "0.12.*",
|
||||
"canteen/html5": "1.1.*",
|
||||
"vierbergenlars/php-semver": "3.0.*"
|
||||
"vierbergenlars/php-semver": "3.0.*",
|
||||
"swiftmailer/swiftmailer": "5.3.*"
|
||||
},
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "1.*"
|
||||
|
@ -34,6 +34,9 @@ use Mibew\Http\Exception\AccessDeniedException as AccessDeniedHttpException;
|
||||
use Mibew\Http\Exception\HttpException;
|
||||
use Mibew\Http\Exception\MethodNotAllowedException as MethodNotAllowedHttpException;
|
||||
use Mibew\Http\Exception\NotFoundException as NotFoundHttpException;
|
||||
use Mibew\Mail\MailerFactory;
|
||||
use Mibew\Mail\MailerFactoryAwareInterface;
|
||||
use Mibew\Mail\MailerFactoryInterface;
|
||||
use Mibew\Routing\RouterAwareInterface;
|
||||
use Mibew\Routing\RouterInterface;
|
||||
use Mibew\Routing\Exception\AccessDeniedException as AccessDeniedRoutingException;
|
||||
@ -51,7 +54,8 @@ use Symfony\Component\Routing\Exception\ResourceNotFoundException as ResourceNot
|
||||
class Application implements
|
||||
RouterAwareInterface,
|
||||
AuthenticationManagerAwareInterface,
|
||||
CacheAwareInterface
|
||||
CacheAwareInterface,
|
||||
MailerFactoryAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var RouterInterface|null
|
||||
@ -83,6 +87,11 @@ class Application implements
|
||||
*/
|
||||
protected $cache = null;
|
||||
|
||||
/**
|
||||
* @var MailerFactoryInterface|null;
|
||||
*/
|
||||
protected $mailerFactory = null;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
@ -225,6 +234,30 @@ class Application implements
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setMailerFactory(MailerFactoryInterface $factory)
|
||||
{
|
||||
$this->mailerFactory = $factory;
|
||||
|
||||
if (!is_null($this->controllerResolver)) {
|
||||
$this->controllerResolver->setMailerFactory($factory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMailerFactory()
|
||||
{
|
||||
if (is_null($this->mailerFactory)) {
|
||||
$this->mailerFactory = new MailerFactory();
|
||||
}
|
||||
|
||||
return $this->mailerFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of Controller Resolver related with the application.
|
||||
*
|
||||
@ -237,7 +270,8 @@ class Application implements
|
||||
$this->getRouter(),
|
||||
$this->getAuthenticationManager(),
|
||||
$this->getAssetManager(),
|
||||
$this->getCache()
|
||||
$this->getCache(),
|
||||
$this->getMailerFactory()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -32,6 +32,8 @@ use Mibew\Handlebars\Helper\CsrfProtectedRouteHelper;
|
||||
use Mibew\Handlebars\Helper\CssAssetsHelper;
|
||||
use Mibew\Handlebars\Helper\JsAssetsHelper;
|
||||
use Mibew\Handlebars\Helper\RouteHelper;
|
||||
use Mibew\Mail\MailerFactoryAwareInterface;
|
||||
use Mibew\Mail\MailerFactoryInterface;
|
||||
use Mibew\Routing\RouterAwareInterface;
|
||||
use Mibew\Routing\RouterInterface;
|
||||
use Mibew\Style\StyleInterface;
|
||||
@ -47,7 +49,8 @@ abstract class AbstractController implements
|
||||
RouterAwareInterface,
|
||||
AuthenticationManagerAwareInterface,
|
||||
AssetManagerAwareInterface,
|
||||
CacheAwareInterface
|
||||
CacheAwareInterface,
|
||||
MailerFactoryAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var RouterInterface|null
|
||||
@ -74,6 +77,11 @@ abstract class AbstractController implements
|
||||
*/
|
||||
protected $cache = null;
|
||||
|
||||
/**
|
||||
* @var MailerFactoryInterface|null
|
||||
*/
|
||||
protected $mailerFactory = null;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -171,6 +179,22 @@ abstract class AbstractController implements
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setMailerFactory(MailerFactoryInterface $factory)
|
||||
{
|
||||
$this->mailerFactory = $factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMailerFactory()
|
||||
{
|
||||
return $this->mailerFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a URL from the given parameters.
|
||||
*
|
||||
@ -283,6 +307,18 @@ abstract class AbstractController implements
|
||||
->generate($relative_path, $reference_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an email.
|
||||
*
|
||||
* It's just a handy shortcut for
|
||||
* `$this->getMailerFactory()->getMailer()->send($message);` sequence.
|
||||
* @param \Swift_Message $message A message that should be sent.
|
||||
*/
|
||||
public function sendMail(\Swift_Message $message)
|
||||
{
|
||||
$this->getMailerFactory()->getMailer()->send($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns style object related with the controller.
|
||||
*
|
||||
|
@ -20,6 +20,7 @@
|
||||
namespace Mibew\Controller\Chat;
|
||||
|
||||
use Mibew\Http\Exception\NotFoundException;
|
||||
use Mibew\Mail\Utils as MailUtils;
|
||||
use Mibew\Settings;
|
||||
use Mibew\Thread;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@ -129,7 +130,7 @@ class MailController extends AbstractController
|
||||
$mail_template['body']
|
||||
);
|
||||
|
||||
mibew_mail($email, MIBEW_MAILBOX, $mail_template['subject'], $body);
|
||||
$this->sendMail(MailUtils::buildMessage($email, MIBEW_MAILBOX, $mail_template['subject'], $body));
|
||||
|
||||
$page = setup_logo($group);
|
||||
$page['email'] = $email;
|
||||
|
@ -39,6 +39,7 @@ class ThreadController extends AbstractController
|
||||
$processor = ThreadProcessor::getInstance();
|
||||
$processor->setRouter($this->getRouter());
|
||||
$processor->setAuthenticationManager($this->getAuthenticationManager());
|
||||
$processor->setMailerFactory($this->getMailerFactory());
|
||||
|
||||
return $processor->handleRequest($request);
|
||||
}
|
||||
|
@ -24,6 +24,8 @@ use Mibew\Asset\AssetManagerInterface;
|
||||
use Mibew\Authentication\AuthenticationManagerAwareInterface;
|
||||
use Mibew\Authentication\AuthenticationManagerInterface;
|
||||
use Mibew\Cache\CacheAwareInterface;
|
||||
use Mibew\Mail\MailerFactoryAwareInterface;
|
||||
use Mibew\Mail\MailerFactoryInterface;
|
||||
use Mibew\Routing\RouterAwareInterface;
|
||||
use Mibew\Routing\RouterInterface;
|
||||
use Stash\Interfaces\PoolInterface;
|
||||
@ -33,7 +35,8 @@ class ControllerResolver implements
|
||||
RouterAwareInterface,
|
||||
AuthenticationManagerAwareInterface,
|
||||
AssetManagerAwareInterface,
|
||||
CacheAwareInterface
|
||||
CacheAwareInterface,
|
||||
MailerFactoryAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var RouterInterface|null
|
||||
@ -55,6 +58,11 @@ class ControllerResolver implements
|
||||
*/
|
||||
protected $cache = null;
|
||||
|
||||
/**
|
||||
* @var MailerFactoryInterface|null
|
||||
*/
|
||||
protected $mailerFactory = null;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
@ -69,12 +77,14 @@ class ControllerResolver implements
|
||||
RouterInterface $router,
|
||||
AuthenticationManagerInterface $authentication_manager,
|
||||
AssetManagerInterface $asset_manager,
|
||||
PoolInterface $cache
|
||||
PoolInterface $cache,
|
||||
MailerFactoryInterface $mailer_factory
|
||||
) {
|
||||
$this->router = $router;
|
||||
$this->authenticationManager = $authentication_manager;
|
||||
$this->assetManager = $asset_manager;
|
||||
$this->cache = $cache;
|
||||
$this->mailerFactory = $mailer_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -141,6 +151,22 @@ class ControllerResolver implements
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setMailerFactory(MailerFactoryInterface $factory)
|
||||
{
|
||||
$this->mailerFactory = $factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMailerFactory()
|
||||
{
|
||||
return $this->mailerFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves controller by request.
|
||||
*
|
||||
@ -210,6 +236,10 @@ class ControllerResolver implements
|
||||
$object->setCache($this->getCache());
|
||||
}
|
||||
|
||||
if ($object instanceof MailerFactoryAwareInterface) {
|
||||
$object->setMailerFactory($this->getMailerFactory());
|
||||
}
|
||||
|
||||
return array($object, $method);
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
namespace Mibew\Controller;
|
||||
|
||||
use Mibew\Http\Exception\BadRequestException;
|
||||
use Mibew\Mail\Utils as MailUtils;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
@ -97,7 +98,7 @@ class PasswordRecoveryController extends AbstractController
|
||||
$mail_template['body']
|
||||
);
|
||||
|
||||
mibew_mail($email, $email, $mail_template['subject'], $body);
|
||||
$this->sendMail(MailUtils::buildMessage($email, $email, $mail_template['subject'], $body));
|
||||
$page['isdone'] = true;
|
||||
|
||||
return $this->render('password_recovery', $page);
|
||||
|
54
src/mibew/libs/classes/Mibew/Mail/MailerFactory.php
Normal file
54
src/mibew/libs/classes/Mibew/Mail/MailerFactory.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is a part of Mibew Messenger.
|
||||
*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Mail;
|
||||
|
||||
/**
|
||||
* A basic implementation of MailerFactoryInterface.
|
||||
*/
|
||||
class MailerFactory implements MailerFactoryInterface
|
||||
{
|
||||
/**
|
||||
* @var \Swift_Mailer|null
|
||||
*/
|
||||
protected $mailer = null;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMailer()
|
||||
{
|
||||
if (is_null($this->mailer)) {
|
||||
$transport = $this->getTransport();
|
||||
$this->mailer = \Swift_Mailer::newInstance($transport);
|
||||
}
|
||||
|
||||
return $this->mailer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds and returns appropriate mail transport.
|
||||
*
|
||||
* @return \Swift_Transport
|
||||
*/
|
||||
protected function getTransport()
|
||||
{
|
||||
return \Swift_MailTransport::newInstance();
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is a part of Mibew Messenger.
|
||||
*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Mail;
|
||||
|
||||
/**
|
||||
* An interface for all mailer factory aware objects.
|
||||
*/
|
||||
interface MailerFactoryAwareInterface
|
||||
{
|
||||
/**
|
||||
* Sets an instance of mailer factory.
|
||||
*
|
||||
* @param \Mibew\Mail\MailerFactoryInterface $factory
|
||||
*/
|
||||
public function setMailerFactory(MailerFactoryInterface $factory);
|
||||
|
||||
/**
|
||||
* Returns an instance of mailer factory.
|
||||
*
|
||||
* @return \Mibew\Mail\MailerFactoryInterface
|
||||
*/
|
||||
public function getMailerFactory();
|
||||
}
|
37
src/mibew/libs/classes/Mibew/Mail/MailerFactoryInterface.php
Normal file
37
src/mibew/libs/classes/Mibew/Mail/MailerFactoryInterface.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is a part of Mibew Messenger.
|
||||
*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Mail;
|
||||
|
||||
/**
|
||||
* MailerFactoryInterface is the interface that all mailer factory classes
|
||||
* must implement.
|
||||
*
|
||||
* Mailer factory is created to encapsulate \Swift_Mailer instantiating logic
|
||||
* and to provide a lazy way for creating instance of the mailer.
|
||||
*/
|
||||
interface MailerFactoryInterface
|
||||
{
|
||||
/**
|
||||
* Builds and returns an instance of mailer.
|
||||
*
|
||||
* @return \Swift_Mailer
|
||||
*/
|
||||
public function getMailer();
|
||||
}
|
56
src/mibew/libs/classes/Mibew/Mail/Utils.php
Normal file
56
src/mibew/libs/classes/Mibew/Mail/Utils.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is a part of Mibew Messenger.
|
||||
*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Mail;
|
||||
|
||||
/**
|
||||
* Contains a set of utility methods related with emails.
|
||||
*/
|
||||
class Utils
|
||||
{
|
||||
/**
|
||||
* Builds an instance of \Swift_message.
|
||||
*
|
||||
* @param string $to_addr Address the message should be send to.
|
||||
* @param string $reply_to The address which will be used in "Reply-to"
|
||||
* mail header.
|
||||
* @param string $subject Subject of the message.
|
||||
* @param string $body Body of the message.
|
||||
* @return \Swift_Message
|
||||
*/
|
||||
public static function buildMessage($to_addr, $reply_to, $subject, $body)
|
||||
{
|
||||
return \Swift_Message::newInstance()
|
||||
->setContentType('text/plain')
|
||||
->setCharset('utf-8')
|
||||
->setMaxLineLength(70)
|
||||
->setFrom(MIBEW_MAILBOX)
|
||||
->setTo($to_addr)
|
||||
->setReplyTo($reply_to)
|
||||
->setSubject($subject)
|
||||
->setBody(preg_replace("/\n/", "\r\n", $body));
|
||||
}
|
||||
|
||||
/**
|
||||
* This class should not be instantiated
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
}
|
@ -23,6 +23,9 @@ namespace Mibew\RequestProcessor;
|
||||
use Mibew\Authentication\AuthenticationManagerAwareInterface;
|
||||
use Mibew\Authentication\AuthenticationManagerInterface;
|
||||
use Mibew\Http\Exception\AccessDeniedException;
|
||||
use Mibew\Mail\MailerFactoryAwareInterface;
|
||||
use Mibew\Mail\MailerFactoryInterface;
|
||||
use Mibew\Mail\Utils as MailUtils;
|
||||
use Mibew\Settings;
|
||||
use Mibew\Thread;
|
||||
use Mibew\API\API as MibewAPI;
|
||||
@ -49,7 +52,8 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
*/
|
||||
class ThreadProcessor extends ClientSideProcessor implements
|
||||
RouterAwareInterface,
|
||||
AuthenticationManagerAwareInterface
|
||||
AuthenticationManagerAwareInterface,
|
||||
MailerFactoryAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var AuthenticationManagerInterface|null
|
||||
@ -70,6 +74,11 @@ class ThreadProcessor extends ClientSideProcessor implements
|
||||
*/
|
||||
protected $router = null;
|
||||
|
||||
/**
|
||||
* @var MailerFactoryInterface|null
|
||||
*/
|
||||
protected $mailerFactory = null;
|
||||
|
||||
/**
|
||||
* Loads thread by id and token and checks if thread loaded
|
||||
*
|
||||
@ -161,6 +170,22 @@ class ThreadProcessor extends ClientSideProcessor implements
|
||||
return $this->authenticationManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setMailerFactory(MailerFactoryInterface $factory)
|
||||
{
|
||||
$this->mailerFactory = $factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMailerFactory()
|
||||
{
|
||||
return $this->mailerFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*/
|
||||
@ -785,7 +810,9 @@ class ThreadProcessor extends ClientSideProcessor implements
|
||||
);
|
||||
|
||||
// Send
|
||||
mibew_mail($inbox_mail, $email, $subject, $body);
|
||||
$this->getMailerFactory()->getMailer()->send(
|
||||
MailUtils::buildMessage($inbox_mail, $email, $subject, $body)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,30 +20,6 @@
|
||||
use Mibew\Database;
|
||||
use Symfony\Component\Yaml\Parser as YamlParser;
|
||||
|
||||
function mibew_mail($to_addr, $reply_to, $subject, $body)
|
||||
{
|
||||
$headers = "From: " . MIBEW_MAILBOX . "\r\n"
|
||||
. "Reply-To: " . $reply_to . "\r\n"
|
||||
. "Content-Type: text/plain; charset=utf-8\r\n"
|
||||
. 'X-Mailer: PHP/' . phpversion();
|
||||
|
||||
$real_subject = "=?utf-8?B?" . base64_encode($subject) . "?=";
|
||||
|
||||
$body = preg_replace("/\n/", "\r\n", $body);
|
||||
|
||||
$old_from = ini_get('sendmail_from');
|
||||
@ini_set('sendmail_from', MIBEW_MAILBOX);
|
||||
@mail(
|
||||
$to_addr,
|
||||
$real_subject,
|
||||
wordwrap($body, 70),
|
||||
$headers
|
||||
);
|
||||
if (isset($old_from)) {
|
||||
@ini_set('sendmail_from', $old_from);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads an email template.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user