Passed
Push — Auth ( 26ff7a...6598c8 )
by Stone
01:58
created

SendMail   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A sendResetPasswordMail() 0 8 1
A send() 0 11 1
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)
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
     */
62
    public function sendResetPasswordMail(string $to, string $token)
63
    {
64
        $url = $this->container->getRequest()->getBaseUrl();
65
        $url .= "password/reset/get?token=".$token;
66
67
        $message = "<a href=\"".$url."\">Define new password</a>";
68
69
        $this->send($to, "Define Password", $message );
70
71
    }
72
}