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