| Conditions | 4 |
| Paths | 6 |
| Total Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | |||
| 155 |