forked from sonata-project/SonataNewsBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailer.php
86 lines (70 loc) · 2.65 KB
/
Mailer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\NewsBundle\Mailer;
use Sonata\NewsBundle\Model\CommentInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Templating\EngineInterface;
use Sonata\NewsBundle\Util\HashGeneratorInterface;
use Sonata\NewsBundle\Model\BlogInterface;
class Mailer implements MailerInterface
{
protected $router;
protected $templating;
protected $emails;
protected $hashGenerator;
protected $mailer;
protected $blog;
/**
* @param \Sonata\NewsBundle\Util\HashGeneratorInterface $generator
* @param \Symfony\Component\Routing\RouterInterface $router
* @param \Symfony\Component\Templating\EngineInterface $templating
* @param array $emails
*/
public function __construct($mailer, BlogInterface $blog, HashGeneratorInterface $generator, RouterInterface $router, EngineInterface $templating, array $emails)
{
$this->blog = $blog;
$this->mailer = $mailer;
$this->hashGenerator = $generator;
$this->router = $router;
$this->templating = $templating;
$this->emails = $emails;
}
/**
* @param \Sonata\NewsBundle\Model\CommentInterface $comment
*/
public function sendCommentNotification(CommentInterface $comment)
{
$rendered = $this->templating->render($this->emails['notification']['template'], array(
'comment' => $comment,
'post' => $comment->getPost(),
'hash' => $this->hashGenerator->generate($comment),
'blog' => $this->blog,
));
$this->sendEmailMessage($rendered, $this->emails['notification']['from'], $this->emails['notification']['emails']);
}
/**
* @param string $renderedTemplate
* @param string $fromEmail
* @param string $toEmail
*/
protected function sendEmailMessage($renderedTemplate, $fromEmail, $toEmail)
{
// Render the email, use the first line as the subject, and the rest as the body
$renderedLines = explode("\n", trim($renderedTemplate));
$subject = $renderedLines[0];
$body = implode("\n", array_slice($renderedLines, 1));
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($fromEmail)
->setTo($toEmail)
->setBody($body);
$this->mailer->send($message);
}
}