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