Passed
Pull Request — master (#58)
by Stone
04:29 queued 02:04
created

SendMail::sendResetPasswordMail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
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
     * @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
            //->setFrom([$this->siteConfig["SMTP_from"]])
50
            ->setTo([$to])
51
            ->setBody($message, 'text/html')
52
        ;
53
54
        if($from === null)
55
        {
56
            //if we haven't set a from, get the config value
57
            $from = $this->siteConfig["SMTP_from"];
58
        }
59
60
        $message->setFrom([$from]);
61
        // Send the message
62
        return $this->mailer->send($message);
63
    }
64
65
    /**
66
     * sent the reset password mail
67
     * @param string $to
68
     * @param string $token
69
     * @param int $userId
70
     */
71
    public function sendResetPasswordMail(string $to, string $token, int $userId)
72
    {
73
        $url = $this->container->getRequest()->getBaseUrl();
74
        $url .= "password/reset/get?token=".$token;
75
        $url .= "&userId=".$userId;
76
77
        $message = "<h1>Message from <a href='".$this->container->getRequest()->getBaseUrl()."'>".$this->siteConfig["site_name"]."</a></h1>";
78
        $message .= "<p>You have asked to reset your password, please click <a href=\"".$url."\">Here</a> to define a new password</p>";
79
80
        $this->send($to, "Define New Password", $message );
81
82
    }
83
84
    /**
85
     * sent the reset password mail
86
     * @param string $to
87
     * @param string $token
88
     * @param int $userId
89
     */
90
    public function sendNewPasswordMail(string $to, string $token, int $userId)
91
    {
92
        $url = $this->container->getRequest()->getBaseUrl();
93
        $url .= "password/reset/get?token=".$token;
94
        $url .= "&userId=".$userId;
95
        $message = "<h1>Message from <a href='".$this->container->getRequest()->getBaseUrl()."'>".$this->siteConfig["site_name"]."</a></h1>";
96
        $message .= "<h2>Welcome to the site</h2>";
97
        $message .= "<p>You have sucsessfuly created an account, now all you need to do is <a href=\"".$url."\">Create your new password</a></p>";
98
        $message .= "<p>Have fun</p>";
99
100
        $this->send($to, "Define Password at ".$this->siteConfig["site_name"], $message );
101
102
    }
103
}