GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Mail::sendMailWithPHPMailer()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 8.9599
c 0
b 0
f 0
cc 4
nc 6
nop 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* Using PHPMailer's namespace */
4
use PHPMailer\PHPMailer\PHPMailer;
5
6
/**
7
 * Class Mail
8
 *
9
 * Handles everything regarding mail-sending.
10
 */
11
class Mail
12
{
13
    /** @var mixed variable to collect errors */
14
    private $error;
15
16
    /**
17
     * Try to send a mail by using PHP's native mail() function.
18
     * Please note that not PHP itself will send a mail, it's just a wrapper for Linux's sendmail or other mail tools
19
     *
20
     * Good guideline on how to send mails natively with mail():
21
     * @see http://stackoverflow.com/a/24644450/1114320
22
     * @see http://www.php.net/manual/en/function.mail.php
23
     */
24
    public function sendMailWithNativeMailFunction()
25
    {
26
        // no code yet, so we just return something to make IDEs and code analyzer tools happy
27
        return false;
28
    }
29
30
    /**
31
     * Try to send a mail by using SwiftMailer.
32
     * Make sure you have loaded SwiftMailer via Composer.
33
     *
34
     * @return bool
35
     */
36
    public function sendMailWithSwiftMailer()
37
    {
38
        // no code yet, so we just return something to make IDEs and code analyzer tools happy
39
        return false;
40
    }
41
42
    /**
43
     * Try to send a mail by using PHPMailer.
44
     * Make sure you have loaded PHPMailer via Composer.
45
     * Depending on your EMAIL_USE_SMTP setting this will work via SMTP credentials or via native mail()
46
     *
47
     * @param $user_email
48
     * @param $from_email
49
     * @param $from_name
50
     * @param $subject
51
     * @param $body
52
     *
53
     * @return bool
54
     * @throws Exception
55
     * @throws phpmailerException
56
     */
57
    public function sendMailWithPHPMailer($user_email, $from_email, $from_name, $subject, $body)
58
    {
59
        $mail = new PHPMailer;
60
        
61
        // you should use UTF-8 to avoid encoding issues
62
        $mail->CharSet = 'UTF-8';
63
64
        // if you want to send mail via PHPMailer using SMTP credentials
65
        if (Config::get('EMAIL_USE_SMTP')) {
66
67
            // set PHPMailer to use SMTP
68
            $mail->IsSMTP();
69
70
            // 0 = off, 1 = commands, 2 = commands and data, perfect to see SMTP errors
71
            $mail->SMTPDebug = 0;
72
73
            // enable SMTP authentication
74
            $mail->SMTPAuth = Config::get('EMAIL_SMTP_AUTH');
75
76
            // encryption
77
            if (Config::get('EMAIL_SMTP_ENCRYPTION')) {
78
                $mail->SMTPSecure = Config::get('EMAIL_SMTP_ENCRYPTION');
79
            }
80
81
            // set SMTP provider's credentials
82
            $mail->Host = Config::get('EMAIL_SMTP_HOST');
83
            $mail->Username = Config::get('EMAIL_SMTP_USERNAME');
84
            $mail->Password = Config::get('EMAIL_SMTP_PASSWORD');
85
            $mail->Port = Config::get('EMAIL_SMTP_PORT');
86
87
        } else {
88
89
            $mail->IsMail();
90
        }
91
92
        // fill mail with data
93
        $mail->From = $from_email;
94
        $mail->FromName = $from_name;
95
        $mail->AddAddress($user_email);
96
        $mail->Subject = $subject;
97
        $mail->Body = $body;
98
99
        // try to send mail, put result status (true/false into $wasSendingSuccessful)
100
        // I'm unsure if mail->send really returns true or false every time, tis method in PHPMailer is quite complex
101
        $wasSendingSuccessful = $mail->Send();
102
103
        if ($wasSendingSuccessful) {
104
            return true;
105
106
        } else {
107
108
            // if not successful, copy errors into Mail's error property
109
            $this->error = $mail->ErrorInfo;
110
            return false;
111
        }
112
    }
113
114
    /**
115
     * The main mail sending method, this simply calls a certain mail sending method depending on which mail provider
116
     * you've selected in the application's config.
117
     *
118
     * @param $user_email string email
119
     * @param $from_email string sender's email
120
     * @param $from_name string sender's name
121
     * @param $subject string subject
122
     * @param $body string full mail body text
123
     * @return bool the success status of the according mail sending method
124
     */
125
    public function sendMail($user_email, $from_email, $from_name, $subject, $body)
126
    {
127
        if (Config::get('EMAIL_USED_MAILER') == "phpmailer") {
128
129
            // returns true if successful, false if not
130
            return $this->sendMailWithPHPMailer(
131
                $user_email, $from_email, $from_name, $subject, $body
132
            );
133
        }
134
135
        if (Config::get('EMAIL_USED_MAILER') == "swiftmailer") {
136
            return $this->sendMailWithSwiftMailer();
137
        }
138
139
        if (Config::get('EMAIL_USED_MAILER') == "native") {
140
            return $this->sendMailWithNativeMailFunction();
141
        }
142
    }
143
144
    /**
145
     * The different mail sending methods write errors to the error property $this->error,
146
     * this method simply returns this error / error array.
147
     *
148
     * @return mixed
149
     */
150
    public function getError()
151
    {
152
        return $this->error;
153
    }
154
}
155