1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Modules; |
4
|
|
|
|
5
|
|
|
use Core\Container; |
6
|
|
|
use Core\Modules\Module; |
7
|
|
|
use Swift_Mailer; |
8
|
|
|
use Swift_Message; |
9
|
|
|
use Swift_SmtpTransport; |
10
|
|
|
|
11
|
|
|
class SendMail extends Module |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
private $siteConfig; |
15
|
|
|
|
16
|
|
|
private $mailer; |
17
|
|
|
private $transport; |
18
|
|
|
|
19
|
|
|
public function __construct(Container $container) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($container); |
22
|
|
|
|
23
|
|
|
$config = new SiteConfig($this->container); |
24
|
|
|
$this->siteConfig = $config->getSiteConfig(); |
25
|
|
|
|
26
|
|
|
// Create the Transport for mail sending |
27
|
|
|
//$config = $this->siteConfig->getSiteConfig(); |
28
|
|
|
$this->transport = (new Swift_SmtpTransport($this->siteConfig["SMTP_server"], |
29
|
|
|
(int)$this->siteConfig["SMTP_port"])) |
30
|
|
|
->setUsername($this->siteConfig["SMTP_user"]) |
31
|
|
|
->setPassword($this->siteConfig["SMTP_pass"]); |
32
|
|
|
|
33
|
|
|
// Create the Mailer using your created Transport |
34
|
|
|
$this->mailer = new Swift_Mailer($this->transport); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Send an Email |
39
|
|
|
* @param string $to |
40
|
|
|
* @param string $subject |
41
|
|
|
* @param string $message |
42
|
|
|
* @param string|null $from |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
|
|
public function send(string $to, string $subject, string $message, string $from = null) |
46
|
|
|
{ |
47
|
|
|
// Create a message |
48
|
|
|
$message = (new Swift_Message($subject)) |
49
|
|
|
->setTo([$to]) |
50
|
|
|
->setBody($message, 'text/html'); |
51
|
|
|
|
52
|
|
|
if ($from === null) { |
53
|
|
|
//if we haven't set a from, get the config value |
54
|
|
|
$from = $this->siteConfig["SMTP_from"]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$message->setFrom([$from]); |
58
|
|
|
// Send the message |
59
|
|
|
return $this->mailer->send($message); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* sent the reset password mail |
64
|
|
|
* @param string $to |
65
|
|
|
* @param string $token |
66
|
|
|
* @param int $userId |
67
|
|
|
*/ |
68
|
|
|
public function sendResetPasswordMail(string $to, string $token, int $userId) |
69
|
|
|
{ |
70
|
|
|
$url = $this->container->getRequest()->getBaseUrl(); |
71
|
|
|
$url .= "password/reset/get&token=" . $token; |
72
|
|
|
$url .= "&userId=" . $userId; |
73
|
|
|
|
74
|
|
|
$message = "<h1>Message from <a href='" . $this->container->getRequest()->getBaseUrl() . "'>" . $this->siteConfig["site_name"] . "</a></h1>"; |
75
|
|
|
$message .= "<p>You have asked to reset your password, please click <a href=\"" . $url . "\">Here</a> to define a new password</p>"; |
76
|
|
|
|
77
|
|
|
$this->send($to, "Define New Password", $message); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* sent the reset password mail |
82
|
|
|
* @param string $to |
83
|
|
|
* @param string $token |
84
|
|
|
* @param int $userId |
85
|
|
|
*/ |
86
|
|
|
public function sendNewPasswordMail(string $to, string $token, int $userId) |
87
|
|
|
{ |
88
|
|
|
$url = $this->container->getRequest()->getBaseUrl(); |
89
|
|
|
$url .= "password/reset/get&token=" . $token; |
90
|
|
|
$url .= "&userId=" . $userId; |
91
|
|
|
$message = "<h1>Message from <a href='" . $this->container->getRequest()->getBaseUrl() . "'>" . $this->siteConfig["site_name"] . "</a></h1>"; |
92
|
|
|
$message .= "<h2>Welcome to the site</h2>"; |
93
|
|
|
$message .= "<p>You have sucsessfuly created an account, now all you need to do is <a href=\"" . $url . "\">Create your new password</a></p>"; |
94
|
|
|
$message .= "<p>Have fun</p>"; |
95
|
|
|
|
96
|
|
|
$this->send($to, "Define Password at " . $this->siteConfig["site_name"], $message); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Send a test mail |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function sendTestMail() |
104
|
|
|
{ |
105
|
|
|
$to = $this->siteConfig["SMTP_from"]; |
106
|
|
|
$subject = "Test mail"; |
107
|
|
|
$message = "this is a test mail to confirm the SMPT is configured correctly"; |
108
|
|
|
|
109
|
|
|
return $this->send($to, $subject, $message); |
110
|
|
|
} |
111
|
|
|
} |