-
Notifications
You must be signed in to change notification settings - Fork 35
/
Mailer.php
executable file
·165 lines (134 loc) · 5.03 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace MembersBundle\Mailer;
use MembersBundle\Adapter\User\UserInterface;
use MembersBundle\Configuration\Configuration;
use Pimcore\Mail;
use Pimcore\Model\Document\Email;
use Pimcore\Model\Document\Service;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class Mailer implements MailerInterface
{
/**
* @var UrlGeneratorInterface
*/
protected $router;
/**
* @var Configuration
*/
protected $configuration;
/**
* @param UrlGeneratorInterface $router
* @param Configuration $configuration
*/
public function __construct(UrlGeneratorInterface $router, Configuration $configuration)
{
$this->router = $router;
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function sendConfirmationEmailMessage(UserInterface $user)
{
$template = $this->getMailTemplatePath('register_confirm', $user);
$url = $this->router->generate('members_user_registration_confirm', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
$mailParams = [
'user' => $user,
'confirmationUrl' => $url
];
$this->sendMessage($template, $mailParams, (string)$user->getEmail());
}
/**
* {@inheritdoc}
*/
public function sendConfirmedEmailMessage(UserInterface $user)
{
if ($this->configuration->getConfig('send_user_mail_after_confirmed') === FALSE) {
return;
}
$template = $this->getMailTemplatePath('register_confirmed', $user);
$url = $this->router->generate('members_user_security_login', [], UrlGeneratorInterface::ABSOLUTE_URL);
$mailParams = [
'user' => $user,
'loginpage' => $url
];
$this->sendMessage($template, $mailParams, (string)$user->getEmail());
}
/**
* {@inheritdoc}
*/
public function sendResettingEmailMessage(UserInterface $user)
{
$template = $this->getMailTemplatePath('register_password_resetting', $user);
$url = $this->router->generate('members_user_resetting_reset', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
$mailParams = [
'user' => $user,
'confirmationUrl' => $url
];
$this->sendMessage($template, $mailParams, (string)$user->getEmail());
}
/**
* {@inheritdoc}
*/
public function sendAdminNotificationEmailMessage(UserInterface $user)
{
if ($this->configuration->getConfig('send_admin_mail_after_register') === FALSE) {
return;
}
$template = $this->getMailTemplatePath('admin_register_notification', $user);
$url = $this->router->generate('pimcore_admin_login_deeplink', [], UrlGeneratorInterface::ABSOLUTE_URL);
$mailParams = [
'user' => $user,
'deeplink' => $url . '?' .'object_' . $user->getId() . '_object' //thanks pimcore.
];
$this->sendMessage($template, $mailParams, (string)$user->getEmail());
}
/**
* @param $documentPath
* @param $mailParams
* @param $toEmail
*
* @throws \Exception
*/
protected function sendMessage($documentPath, $mailParams, $toEmail)
{
$emailDocument = Email::getByPath($documentPath);
if (!$emailDocument instanceof Email) {
throw new \Exception(sprintf('document not found in "%s"', $documentPath));
}
$email = new Mail();
$email->addTo($toEmail);
$email->setDocument($emailDocument);
$email->setParams($mailParams);
$email->send();
}
/**
* @param string $type
* @param UserInterface $user
*
* @return string
*/
private function getMailTemplatePath($type = '', UserInterface $user)
{
$templates = $this->configuration->getConfig('emails');
$userLocale = $user->getProperty('_user_locale');
$userSite = $user->getProperty('_site_domain');
$templateBranch = $templates['default'];
if(!empty($userSite) && !empty($templates['sites'])) {
$key = array_search($userSite, array_column($templates['sites'], 'main_domain'));
if($key !== FALSE) {
$templateBranch = $templates['sites'][$key]['emails'];
}
}
$requestedTemplate = $templateBranch[$type];
if(!empty($userLocale) && strpos($requestedTemplate, '{_locale}') !== FALSE) {
$_requestedTemplate = str_replace('{_locale}', $userLocale, $requestedTemplate);
//fallback: there is maybe a nice locale to url transform, like "de_CH" => "de-ch"
if(!Service::pathExists($_requestedTemplate) && strpos($userLocale, '_') !== FALSE) {
$_requestedTemplate = str_replace('{_locale}', strtolower(str_replace('_', '-', $userLocale)), $requestedTemplate);
}
$requestedTemplate = $_requestedTemplate;
}
return $requestedTemplate;
}
}