|
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
|
|
|
private $siteConfig; |
|
14
|
|
|
|
|
15
|
|
|
private $mailer; |
|
16
|
|
|
private $transport; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Container $container) |
|
19
|
|
|
{ |
|
20
|
|
|
parent::__construct($container); |
|
21
|
|
|
|
|
22
|
|
|
$config = new SiteConfig($this->container); |
|
23
|
|
|
$this->siteConfig = $config->getSiteConfig(); |
|
24
|
|
|
|
|
25
|
|
|
// Create the Transport for mail sending |
|
26
|
|
|
//$config = $this->siteConfig->getSiteConfig(); |
|
27
|
|
|
$this->transport = (new Swift_SmtpTransport($this->siteConfig["SMTP_server"], (int)$this->siteConfig["SMTP_port"])) |
|
28
|
|
|
->setUsername($this->siteConfig["SMTP_user"]) |
|
29
|
|
|
->setPassword($this->siteConfig["SMTP_pass"]) |
|
30
|
|
|
; |
|
31
|
|
|
|
|
32
|
|
|
// Create the Mailer using your created Transport |
|
33
|
|
|
$this->mailer = new Swift_Mailer($this->transport); |
|
34
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Send an Email |
|
39
|
|
|
* @param string $to |
|
40
|
|
|
* @param string $subject |
|
41
|
|
|
* @param string $message |
|
42
|
|
|
* @return int |
|
43
|
|
|
*/ |
|
44
|
|
|
public function send(string $to, string $subject, string $message) |
|
45
|
|
|
{ |
|
46
|
|
|
// Create a message |
|
47
|
|
|
$message = (new Swift_Message($subject)) |
|
48
|
|
|
->setFrom([$this->siteConfig["SMTP_from"]]) |
|
49
|
|
|
->setTo([$to]) |
|
50
|
|
|
->setBody($message, 'text/html') |
|
51
|
|
|
; |
|
52
|
|
|
|
|
53
|
|
|
// Send the message |
|
54
|
|
|
return $this->mailer->send($message); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* sent the reset password mail |
|
59
|
|
|
* @param string $to |
|
60
|
|
|
* @param string $token |
|
61
|
|
|
* @param int $userId |
|
62
|
|
|
*/ |
|
63
|
|
|
public function sendResetPasswordMail(string $to, string $token, int $userId) |
|
64
|
|
|
{ |
|
65
|
|
|
$url = $this->container->getRequest()->getBaseUrl(); |
|
66
|
|
|
$url .= "password/reset/get?token=".$token; |
|
67
|
|
|
$url .= "&userId=".$userId; |
|
68
|
|
|
|
|
69
|
|
|
$message = "<h1>Message from <a href='".$this->container->getRequest()->getBaseUrl()."'>".$this->siteConfig["site_name"]."</a></h1>"; |
|
70
|
|
|
$message .= "<p>You have asked to reset your password, please click <a href=\"".$url."\">Here</a> to define a new password</p>"; |
|
71
|
|
|
|
|
72
|
|
|
$this->send($to, "Define New Password", $message ); |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* sent the reset password mail |
|
78
|
|
|
* @param string $to |
|
79
|
|
|
* @param string $token |
|
80
|
|
|
* @param int $userId |
|
81
|
|
|
*/ |
|
82
|
|
|
public function sendNewPasswordMail(string $to, string $token, int $userId) |
|
83
|
|
|
{ |
|
84
|
|
|
$url = $this->container->getRequest()->getBaseUrl(); |
|
85
|
|
|
$url .= "password/reset/get?token=".$token; |
|
86
|
|
|
$url .= "&userId=".$userId; |
|
87
|
|
|
$message = "<h1>Message from <a href='".$this->container->getRequest()->getBaseUrl()."'>".$this->siteConfig["site_name"]."</a></h1>"; |
|
88
|
|
|
$message .= "<h2>Welcome to the site</h2>"; |
|
89
|
|
|
$message .= "<p>You have sucsessfuly created an account, now all you need to do is <a href=\"".$url."\">Create your new password</a></p>"; |
|
90
|
|
|
$message .= "<p>Have fun</p>"; |
|
91
|
|
|
|
|
92
|
|
|
$this->send($to, "Define Password at ".$this->siteConfig["site_name"], $message ); |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |