Skip to content

Commit 95ff0b0

Browse files
committed
Notification class, and SMTP configuration!
1 parent 0854f46 commit 95ff0b0

9 files changed

Lines changed: 365 additions & 70 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
- Switch from DateJS to momentjs
1616
- Switch to jQueryValidation from FormValidation
1717
- Implement basic interface for modifying group authorization rules
18-
- User events - timestamps for things like sign-in, sign-up, password reset, etc are now stored in a `user_event` table
18+
- User events - timestamps for things like sign-in, sign-up, password reset, etc are now stored in a `user_event` table
19+
- Wrapper class Notification for sending emails, other notifications to users

userfrosting/config-userfrosting.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@
5050
'db_pass' => 'password',
5151
'db_prefix'=> 'uf_'
5252
],
53+
'mail' => 'smtp',
54+
'smtp' => [
55+
'host' => 'mail.example.com',
56+
'port' => 465,
57+
'auth' => true,
58+
'secure' => 'ssl',
59+
'user' => '[email protected]',
60+
'pass' => 'password'
61+
],
5362
'uri' => [
5463
'public' => $uri_public_root,
5564
'js' => $uri_public_root . "/js/",
@@ -84,6 +93,15 @@
8493
'db_pass' => 'password',
8594
'db_prefix'=> 'uf_'
8695
],
96+
'mail' => 'smtp',
97+
'smtp' => [
98+
'host' => 'mail.example.com',
99+
'port' => 465,
100+
'auth' => true,
101+
'secure' => 'ssl',
102+
'user' => '[email protected]',
103+
'pass' => 'password'
104+
],
87105
'uri' => [
88106
'public' => $uri_public_root,
89107
'js' => $uri_public_root . "/js/",

userfrosting/controllers/AccountController.php

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -442,36 +442,23 @@ public function register(){
442442
// Create verification request
443443
$verification_request = $user->newEventVerificationRequest();
444444

445-
// TODO: pass into template
446-
447-
// Create and send activation email
448-
$mail = new \PHPMailer;
449-
450-
$mail->From = $this->_app->site->admin_email;
451-
$mail->FromName = $this->_app->site->site_title;
452-
$mail->addAddress($user->email); // Add a recipient
453-
$mail->addReplyTo($this->_app->site->admin_email, $this->_app->site->site_title);
454-
445+
// Create and send verification email
455446
$twig = $this->_app->view()->getEnvironment();
456-
$template = $twig->loadTemplate("mail/activate-new.twig");
457-
$params = [
447+
$template = $twig->loadTemplate("mail/activate-new.twig");
448+
$notification = new Notification($template);
449+
$notification->fromWebsite(); // Automatically sets sender and reply-to
450+
$notification->addEmailRecipient($user->email, $user->display_name, [
458451
"user" => $user
459-
];
460-
461-
// Must manually merge in global variables for block rendering
462-
$params = array_merge($twig->getGlobals(), $params);
463-
$mail->Subject = $template->renderBlock('subject', $params);
464-
$mail->Body = $template->renderBlock('body', $params);
465-
466-
$mail->isHTML(true); // Set email format to HTML
452+
]);
467453

468-
if(!$mail->send()) {
454+
try {
455+
$notification->send();
456+
} catch (\Exception\phpmailerException $e){
469457
$ms->addMessageTranslated("danger", "MAIL_ERROR");
470-
error_log('Mailer Error: ' . $mail->ErrorInfo);
458+
error_log('Mailer Error: ' . $e->errorMessage());
471459
$this->_app->halt(500);
472460
}
473-
474-
// Activation required
461+
475462
$ms->addMessageTranslated("success", "ACCOUNT_REGISTRATION_COMPLETE_TYPE2");
476463
$verification_request->save();
477464
} else
@@ -578,33 +565,23 @@ public function forgotPassword(){
578565
$event = $user->newEventPasswordReset();
579566

580567
// Email the user asking to confirm this change password request
581-
$mail = new \PHPMailer;
582-
583-
$mail->From = $this->_app->site->admin_email;
584-
$mail->FromName = $this->_app->site->site_title;
585-
$mail->addAddress($user->email); // Add a recipient
586-
$mail->addReplyTo($this->_app->site->admin_email, $this->_app->site->site_title);
587-
588568
$twig = $this->_app->view()->getEnvironment();
589-
$template = $twig->loadTemplate("mail/password-reset.twig");
590-
$params = [
569+
$template = $twig->loadTemplate("mail/password-reset.twig");
570+
$notification = new Notification($template);
571+
$notification->fromWebsite(); // Automatically sets sender and reply-to
572+
$notification->addEmailRecipient($user->email, $user->display_name, [
591573
"user" => $user,
592574
"request_date" => date("Y-m-d H:i:s")
593-
];
594-
595-
// Must manually merge in global variables for block rendering
596-
$params = array_merge($twig->getGlobals(), $params);
597-
$mail->Subject = $template->renderBlock('subject', $params);
598-
$mail->Body = $template->renderBlock('body', $params);
599-
600-
$mail->isHTML(true); // Set email format to HTML
575+
]);
601576

602-
if(!$mail->send()) {
577+
try {
578+
$notification->send();
579+
} catch (\Exception\phpmailerException $e){
603580
$ms->addMessageTranslated("danger", "MAIL_ERROR");
604-
error_log('Mailer Error: ' . $mail->ErrorInfo);
581+
error_log('Mailer Error: ' . $e->errorMessage());
605582
$this->_app->halt(500);
606583
}
607-
584+
608585
$user->store();
609586
$event->save();
610587
$ms->addMessageTranslated("success", "FORGOTPASS_REQUEST_SUCCESS");
@@ -803,30 +780,20 @@ public function resendActivation(){
803780
$event = $user->newEventVerificationRequest();
804781

805782
// Email the user
806-
$mail = new \PHPMailer;
807-
808-
$mail->From = $this->_app->site->admin_email;
809-
$mail->FromName = $this->_app->site->site_title;
810-
$mail->addAddress($user->email); // Add a recipient
811-
$mail->addReplyTo($this->_app->site->admin_email, $this->_app->site->site_title);
812-
813783
$twig = $this->_app->view()->getEnvironment();
814-
$template = $twig->loadTemplate("mail/resend-activation.twig");
815-
$params = [
784+
$template = $twig->loadTemplate("mail/resend-activation.twig");
785+
$notification = new Notification($template);
786+
$notification->fromWebsite(); // Automatically sets sender and reply-to
787+
$notification->addEmailRecipient($user->email, $user->display_name, [
816788
"user" => $user,
817789
"secret_token" => $user->secret_token
818-
];
819-
820-
// Must manually merge in global variables for block rendering
821-
$params = array_merge($twig->getGlobals(), $params);
822-
$mail->Subject = $template->renderBlock('subject', $params);
823-
$mail->Body = $template->renderBlock('body', $params);
824-
825-
$mail->isHTML(true); // Set email format to HTML
790+
]);
826791

827-
if(!$mail->send()) {
792+
try {
793+
$notification->send();
794+
} catch (\Exception\phpmailerException $e){
828795
$ms->addMessageTranslated("danger", "MAIL_ERROR");
829-
error_log('Mailer Error: ' . $mail->ErrorInfo);
796+
error_log('Mailer Error: ' . $e->errorMessage());
830797
$this->_app->halt(500);
831798
}
832799

userfrosting/models/database/UFModel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function __construct($properties = [], $id = null) {
3939
$table_schema = Database::getSchemaTable(static::$_table_id);
4040
$this->table = $table_schema->name;
4141
$this->fillable = $table_schema->columns;
42+
if (!static::$app)
43+
static::$app = UserFrosting::getInstance();
4244
parent::__construct($properties);
4345
}
4446

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace UserFrosting;
4+
5+
/**
6+
* EmailRecipient Class
7+
*
8+
* A class representing an email recipient for a Notification, with associated parameters.
9+
*
10+
* @package UserFrosting
11+
* @author Alex Weissman
12+
* @link http://www.userfrosting.com/
13+
*/
14+
class EmailRecipient {
15+
16+
/**
17+
* @var string The email address for this recipient.
18+
*/
19+
protected $email;
20+
21+
/**
22+
* @var string The name for this recipient.
23+
*/
24+
protected $name;
25+
26+
/**
27+
* @var array The parameters (name => value) to use when rendering an email template for this recipient.
28+
*/
29+
protected $params = [];
30+
31+
/**
32+
* @var array A list of CCs for this recipient. Each CC is an associative array with `email` and `name` properties.
33+
*/
34+
protected $cc = [];
35+
36+
/**
37+
* @var array A list of BCCs for this recipient. Each BCC is an associative array with `email` and `name` properties.
38+
*/
39+
protected $bcc = [];
40+
41+
/**
42+
* Create a new EmailRecipient instance.
43+
*
44+
* @param string $email The primary recipient email address.
45+
* @param string $name The primary recipient name.
46+
* @param array $params An array of template parameters to render the email message with for this particular recipient.
47+
*/
48+
public function __construct($email, $name = "", $params = []){
49+
$this->email = $email;
50+
$this->name = $name;
51+
$this->params = $params;
52+
}
53+
54+
/**
55+
* Add a CC for this primary recipient.
56+
*
57+
* @param string $email The CC recipient email address.
58+
* @param string $name The CC recipient name.
59+
*/
60+
public function cc($email, $name = ""){
61+
$this->cc[] = [
62+
"email" => $email,
63+
"name" => $name
64+
];
65+
}
66+
67+
/**
68+
* Add a BCC for this primary recipient.
69+
*
70+
* @param string $email The BCC recipient email address.
71+
* @param string $name The BCC recipient name.
72+
*/
73+
public function bcc($email, $name = ""){
74+
$this->bcc[] = [
75+
"email" => $email,
76+
"name" => $name
77+
];
78+
}
79+
80+
/**
81+
* Get the primary recipient email address.
82+
*
83+
* @return string the primary recipient email address.
84+
*/
85+
public function getEmail(){
86+
return $this->email;
87+
}
88+
89+
/**
90+
* Get the primary recipient name.
91+
*
92+
* @return string the primary recipient name.
93+
*/
94+
public function getName(){
95+
return $this->name;
96+
}
97+
98+
/**
99+
* Get the parameters to use when rendering the template this recipient.
100+
*
101+
* @return array The parameters (name => value) to use when rendering an email template for this recipient.
102+
*/
103+
public function getParams(){
104+
return $this->params;
105+
}
106+
107+
/**
108+
* Get the list of CCs for this recipient.
109+
*
110+
* @return array A list of CCs for this recipient. Each CC is an associative array with `email` and `name` properties.
111+
*/
112+
public function getCCs(){
113+
return $this->cc;
114+
}
115+
116+
/**
117+
* Get the list of BCCs for this recipient.
118+
*
119+
* @return array A list of BCCs for this recipient. Each BCC is an associative array with `email` and `name` properties.
120+
*/
121+
public function getBCCs(){
122+
return $this->bcc;
123+
}
124+
}

0 commit comments

Comments
 (0)