Skip to content

Commit b6e0758

Browse files
committed
Send activation email when user is registered
1 parent 64ba7a7 commit b6e0758

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

etc/config.sample.json

+3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
}
4242
},
4343
"email": {
44+
"recipient_from": "",
45+
"recipient_reply_to": "",
4446
"smtp_host": "smtp.example.com",
4547
"smtp_password": "",
4648
"smtp_port": 587,
49+
"smtp_tls": true,
4750
"smtp_user": ""
4851
},
4952
"memcache": {

src/controllers/User/Register.php

+67-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
use \CarlBennett\MVC\Libraries\Common;
1717
use \CarlBennett\MVC\Libraries\Controller;
1818
use \CarlBennett\MVC\Libraries\Router;
19+
use \CarlBennett\MVC\Libraries\Template;
1920
use \CarlBennett\MVC\Libraries\View;
2021

22+
use \PHPMailer\PHPMailer\Exception;
23+
use \PHPMailer\PHPMailer\PHPMailer;
24+
2125
class Register extends Controller {
2226

2327
public function &run(Router &$router, View &$view, array &$args) {
@@ -136,7 +140,7 @@ protected function tryRegister(Router &$router, UserRegisterModel &$model) {
136140
return;
137141
}
138142
} catch (UserNotFoundException $e) {}
139-
143+
140144
try {
141145
if (User::findIdByUsername($username)) {
142146
$model->error = "USERNAME_TAKEN";
@@ -158,6 +162,68 @@ protected function tryRegister(Router &$router, UserRegisterModel &$model) {
158162

159163
}
160164

165+
if ($success) {
166+
$mail = new PHPMailer( true ); // true enables exceptions
167+
$mail_config = Common::$config->email;
168+
169+
try {
170+
//Server settings
171+
$mail->Timeout = 10; // default is 300 per RFC2821 $ 4.5.3.2
172+
$mail->SMTPDebug = 0;
173+
$mail->isSMTP();
174+
$mail->Host = $mail_config->smtp_host;
175+
$mail->SMTPAuth = !empty($mail_config->smtp_user);
176+
$mail->Username = $mail_config->smtp_user;
177+
$mail->Password = $mail_config->smtp_password;
178+
$mail->SMTPSecure = $mail_config->smtp_tls ? 'tls' : '';
179+
$mail->Port = $mail_config->smtp_port;
180+
181+
//Recipients
182+
if (!empty($mail_config->recipient_from)) {
183+
$mail->setFrom($mail_config->recipient_from, 'BNETDocs');
184+
}
185+
186+
$mail->addAddress($email);
187+
188+
if (!empty($mail_config->recipient_reply_to)) {
189+
$mail->addReplyTo($mail_config->recipient_reply_to);
190+
}
191+
192+
// Content
193+
$mail->isHTML(true);
194+
$mail->Subject = 'Account Activation';
195+
$mail->CharSet = PHPMailer::CHARSET_UTF8;
196+
197+
ob_start();
198+
(new Template($mail, 'Email/User/Register.rich'))->render();
199+
$mail->Body = ob_get_clean();
200+
201+
ob_start();
202+
(new Template($mail, 'Email/User/Register.plain'))->render();
203+
$mail->AltBody = ob_get_clean();
204+
205+
$mail->send();
206+
207+
Logger::logEvent(
208+
EventTypes::EMAIL_SENT,
209+
null,
210+
getenv('REMOTE_ADDR'),
211+
json_encode([
212+
'from' => $mail->From,
213+
'to' => $mail->getToAddresses(),
214+
'reply_to' => $mail->getReplyToAddresses(),
215+
'subject' => $mail->Subject,
216+
'content_type' => $mail->ContentType,
217+
'body' => $mail->Body,
218+
'alt_body' => $mail->AltBody,
219+
])
220+
);
221+
222+
} catch (\Exception $e) {
223+
$model->error = "EMAIL_FAILURE";
224+
}
225+
}
226+
161227
if (!$success) {
162228
$model->error = "INTERNAL_ERROR";
163229
} else {

src/templates/User/Register.phtml

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ switch ($this->getContext()->error) {
7474
$af = "username";
7575
$message = "That username is taken, try another.";
7676
break;
77+
case "EMAIL_FAILURE":
78+
$af = "email";
79+
$message = "Your account was created, but the email failed to send.";
80+
break;
7781
case "INTERNAL_ERROR":
7882
$af = null;
7983
$message = "An internal error occurred while processing your request. "

0 commit comments

Comments
 (0)