1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Email Class |
5
|
|
|
* |
6
|
|
|
* Sending emails via SMTP. |
7
|
|
|
* It uses PHPMailer library to send emails. |
8
|
|
|
* |
9
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
10
|
|
|
* @author Omar El Gabry <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class Email{ |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This is the constructor for Email object. |
17
|
|
|
* |
18
|
|
|
* @access private |
19
|
|
|
*/ |
20
|
|
|
private function __construct(){} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* send an email |
24
|
|
|
* |
25
|
|
|
* @access public |
26
|
|
|
* @static static method |
27
|
|
|
* @param string $type Email constant - check config.php |
28
|
|
|
* @param string $email |
29
|
|
|
* @param array $userData |
30
|
|
|
* @param array $data any associated data with the email |
31
|
|
|
* @throws Exception If failed to send the email |
32
|
|
|
*/ |
33
|
|
|
public static function sendEmail($type, $email, $userData, $data){ |
34
|
|
|
|
35
|
|
|
$mail = new PHPMailer(); |
36
|
|
|
$mail->IsSMTP(); |
37
|
|
|
|
38
|
|
|
// good for debugging, otherwise keep it commented |
39
|
|
|
// $mail->SMTPDebug = EMAIL_SMTP_DEBUG; |
40
|
|
|
$mail->SMTPAuth = Config::get('EMAIL_SMTP_AUTH'); |
|
|
|
|
41
|
|
|
$mail->SMTPSecure = Config::get('EMAIL_SMTP_SECURE'); |
42
|
|
|
$mail->Host = Config::get('EMAIL_SMTP_HOST'); |
43
|
|
|
$mail->Port = Config::get('EMAIL_SMTP_PORT'); |
|
|
|
|
44
|
|
|
$mail->Username = Config::get('EMAIL_SMTP_USERNAME'); |
45
|
|
|
$mail->Password = Config::get('EMAIL_SMTP_PASSWORD'); |
46
|
|
|
|
47
|
|
|
$mail->SetFrom(Config::get('EMAIL_FROM'), Config::get('EMAIL_FROM_NAME')); |
48
|
|
|
$mail->AddReplyTo(Config::get('EMAIL_REPLY_TO')); |
49
|
|
|
|
50
|
|
|
switch($type){ |
51
|
|
|
case (Config::get('EMAIL_EMAIL_VERIFICATION')): |
52
|
|
|
$mail->Body = self::getEmailVerificationBody($userData, $data); |
53
|
|
|
$mail->Subject = Config::get('EMAIL_EMAIL_VERIFICATION_SUBJECT'); |
54
|
|
|
$mail->AddAddress($email); |
55
|
|
|
break; |
56
|
|
|
case (Config::get('EMAIL_REVOKE_EMAIL')): |
57
|
|
|
$mail->Body = self::getRevokeEmailBody($userData, $data); |
58
|
|
|
$mail->Subject = Config::get('EMAIL_REVOKE_EMAIL_SUBJECT'); |
59
|
|
|
$mail->AddAddress($email); |
60
|
|
|
break; |
61
|
|
|
case (Config::get('EMAIL_UPDATE_EMAIL')): |
62
|
|
|
$mail->Body = self::getUpdateEmailBody($userData, $data); |
63
|
|
|
$mail->Subject = Config::get('EMAIL_UPDATE_EMAIL_SUBJECT'); |
64
|
|
|
$mail->AddAddress($email); |
65
|
|
|
break; |
66
|
|
|
case (Config::get('EMAIL_PASSWORD_RESET')): |
67
|
|
|
$mail->Body = self::getPasswordResetBody($userData, $data); |
68
|
|
|
$mail->Subject = Config::get('EMAIL_PASSWORD_RESET_SUBJECT'); |
69
|
|
|
$mail->AddAddress($email); |
70
|
|
|
break; |
71
|
|
|
case (Config::get('EMAIL_REPORT_BUG')): |
72
|
|
|
$mail->Body = self::getReportBugBody($userData, $data); |
73
|
|
|
$mail->Subject = "[".ucfirst($data["label"])."] " . Config::get('EMAIL_REPORT_BUG_SUBJECT') . " | " . $data["subject"]; |
74
|
|
|
$mail->AddAddress($email); |
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// If you don't have an email setup, you can instead save emails in log.txt file using Logger. |
79
|
|
|
// Logger::log("EMAIL", $mail->Body); |
|
|
|
|
80
|
|
|
if(!$mail->Send()) { |
81
|
|
|
throw new Exception("Email couldn't be sent to ". $userData["id"] ." for type: ". $type); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Construct the body of Password Reset email |
87
|
|
|
* |
88
|
|
|
* @access private |
89
|
|
|
* @static static method |
90
|
|
|
* @param array $userData |
91
|
|
|
* @param array $data |
92
|
|
|
* @return string The body of the email. |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
private static function getPasswordResetBody($userData, $data){ |
|
|
|
|
95
|
|
|
|
96
|
|
|
$body = ""; |
97
|
|
|
$body .= "Dear " . $userData["name"] . ", \n\nYou can reset your password from the following link: "; |
98
|
|
|
$body .= Config::get('EMAIL_PASSWORD_RESET_URL') . "?id=" . urlencode(Encryption::encryptId($userData["id"])) . "&token=" . urlencode($data["password_token"]); |
99
|
|
|
$body .= "\n\nIf you didn't request to reset your password, Please contact the admin directly."; |
100
|
|
|
$body .= "\n\nRegards\nmini PHP Team"; |
101
|
|
|
|
102
|
|
|
return $body; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Construct the body of Email Verification email |
107
|
|
|
* |
108
|
|
|
* @access private |
109
|
|
|
* @static static method |
110
|
|
|
* @param array $userData |
111
|
|
|
* @param array $data |
112
|
|
|
* @return string The body of the email. |
113
|
|
|
* |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
private static function getEmailVerificationBody($userData, $data){ |
|
|
|
|
116
|
|
|
|
117
|
|
|
$body = ""; |
118
|
|
|
$body .= "Dear " . $userData["name"] . ", \n\nPlease verify your email from the following link: "; |
119
|
|
|
$body .= Config::get('EMAIL_EMAIL_VERIFICATION_URL') . "?id=" . urlencode(Encryption::encryptId($userData["id"])) . "&token=" . urlencode($data["email_token"]); |
120
|
|
|
$body .= "\n\nIf you didn't edit/add your email, Please contact the admin directly."; |
121
|
|
|
$body .= "\n\nRegards\nmini PHP Team"; |
122
|
|
|
|
123
|
|
|
return $body; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Construct the body of Revoke Email Changes email |
128
|
|
|
* |
129
|
|
|
* @access private |
130
|
|
|
* @static static method |
131
|
|
|
* @param array $userData |
132
|
|
|
* @param array $data |
133
|
|
|
* @return string The body of the email. |
134
|
|
|
* |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
private static function getRevokeEmailBody($userData, $data){ |
|
|
|
|
137
|
|
|
|
138
|
|
|
$body = ""; |
139
|
|
|
$body .= "Dear " . $userData["name"] . ", \n\nYour email has been changed, You can revoke your changes from the following link: "; |
140
|
|
|
$body .= Config::get('EMAIL_REVOKE_EMAIL_URL') . "?id=" . urlencode(Encryption::encryptId($userData["id"])) . "&token=" . urlencode($data["email_token"]); |
141
|
|
|
$body .= "\n\nIf you didn't update your email, Please contact the admin directly."; |
142
|
|
|
$body .= "\n\nRegards\nmini PHP Team"; |
143
|
|
|
|
144
|
|
|
return $body; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Construct the body of Update Email email |
149
|
|
|
* |
150
|
|
|
* @access private |
151
|
|
|
* @static static method |
152
|
|
|
* @param array $userData |
153
|
|
|
* @param array $data |
154
|
|
|
* @return string The body of the email. |
155
|
|
|
* |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
private static function getUpdateEmailBody($userData, $data){ |
|
|
|
|
158
|
|
|
|
159
|
|
|
$body = ""; |
160
|
|
|
$body .= "Dear " . $userData["name"] . ", \n\nPlease confirm your new email from the following link: "; |
161
|
|
|
$body .= Config::get('EMAIL_UPDATE_EMAIL_URL') . "?id=" . urlencode(Encryption::encryptId($userData["id"])) . "&token=" . urlencode($data["pending_email_token"]); |
162
|
|
|
$body .= "\n\nIf you have no idea what is this email for, you can ignore it."; |
163
|
|
|
$body .= "\n\nRegards\nmini PHP Team"; |
164
|
|
|
|
165
|
|
|
return $body; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Construct the body of Report Bug, Feature or Enhancement email |
170
|
|
|
* |
171
|
|
|
* @access private |
172
|
|
|
* @static static method |
173
|
|
|
* @param array $userData |
174
|
|
|
* @param array $data |
175
|
|
|
* @return string The body of the email. |
176
|
|
|
* |
177
|
|
|
*/ |
178
|
|
|
private static function getReportBugBody($userData, $data){ |
179
|
|
|
|
180
|
|
|
$body = ""; |
181
|
|
|
$body .= "User: " . $userData["name"] . ", \n\n" . $data["message"]; |
182
|
|
|
$body .= "\n\n\nFrom: " . $userData["id"] . " | " . $userData["name"]; |
183
|
|
|
$body .= "\n\nRegards\nmini PHP Team"; |
184
|
|
|
|
185
|
|
|
return $body; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
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.