-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from CakeDC/email-refactoring
Email refactoring based on 3.1 email improvements
- Loading branch information
Showing
10 changed files
with
586 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com) | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace CakeDC\Users\Email; | ||
|
||
use Cake\Datasource\EntityInterface; | ||
use Cake\Mailer\Email; | ||
use Cake\Mailer\MailerAwareTrait; | ||
|
||
/** | ||
* Email sender class | ||
* | ||
*/ | ||
class EmailSender | ||
{ | ||
use MailerAwareTrait; | ||
|
||
/** | ||
* Send validation email | ||
* | ||
* @param EntityInterface $user User entity | ||
* @param Email $email instance, if null the default email configuration with the | ||
* @return void | ||
*/ | ||
public function sendValidationEmail(EntityInterface $user, Email $email = null) | ||
{ | ||
$this | ||
->getMailer( | ||
'CakeDC/Users.Users', | ||
$this->_getEmailInstance($email) | ||
) | ||
->send('validation', [$user, __d('Users', 'Your account validation link')]); | ||
} | ||
|
||
/** | ||
* Send the reset password email | ||
* | ||
* @param EntityInterface $user User entity | ||
* @param Email $email instance, if null the default email configuration with the | ||
* @param string $template email template | ||
* Users.validation template will be used, so set a ->template() if you pass an Email | ||
* instance | ||
* @return array email send result | ||
*/ | ||
public function sendResetPasswordEmail(EntityInterface $user, Email $email = null, $template = 'CakeDC/Users.reset_password') | ||
{ | ||
$this | ||
->getMailer( | ||
'CakeDC/Users.Users', | ||
$this->_getEmailInstance($email) | ||
) | ||
->send('resetPassword', [$user, $template]); | ||
} | ||
|
||
/** | ||
* Send social validation email to the user | ||
* | ||
* @param EntityInterface $socialAccount social account | ||
* @param EntityInterface $user user | ||
* @param Email $email Email instance or null to use 'default' configuration | ||
* @return mixed | ||
*/ | ||
public function sendSocialValidationEmail(EntityInterface $socialAccount, EntityInterface $user, Email $email = null) | ||
{ | ||
if (empty($email)) { | ||
$template = 'CakeDC/Users.social_account_validation'; | ||
} else { | ||
$template = $email->template()['template']; | ||
} | ||
$this | ||
->getMailer( | ||
'CakeDC/Users.Users', | ||
$this->_getEmailInstance($email) | ||
) | ||
->send('socialAccountValidation', [$user, $socialAccount, $template]); | ||
} | ||
|
||
/** | ||
* Get or initialize the email instance. Used for mocking. | ||
* | ||
* @param Email $email if email provided, we'll use the instance instead of creating a new one | ||
* @return Email | ||
*/ | ||
protected function _getEmailInstance(Email $email = null) | ||
{ | ||
if ($email === null) { | ||
$email = new Email('default'); | ||
$email->emailFormat('both'); | ||
} | ||
return $email; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com) | ||
* | ||
* Licensed under The MIT License | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com) | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace CakeDC\Users\Mailer; | ||
|
||
use Cake\Datasource\EntityInterface; | ||
use Cake\Mailer\Email; | ||
use Cake\Mailer\Mailer; | ||
|
||
/** | ||
* User Mailer | ||
* | ||
*/ | ||
class UsersMailer extends Mailer | ||
{ | ||
/** | ||
* Send the templated email to the user | ||
* | ||
* @param EntityInterface $user User entity | ||
* @param string $subject Subject, note the first_name of the user will be prepended if exist | ||
* @param string $template string, note the first_name of the user will be prepended if exists | ||
* | ||
* @return array email send result | ||
*/ | ||
protected function validation(EntityInterface $user, $subject, $template = 'CakeDC/Users.validation') | ||
{ | ||
$firstName = isset($user['first_name'])? $user['first_name'] . ', ' : ''; | ||
$this | ||
->to($user['email']) | ||
->subject($firstName . $subject) | ||
->viewVars($user->toArray()) | ||
->template($template); | ||
} | ||
|
||
/** | ||
* Send the reset password email to the user | ||
* | ||
* @param EntityInterface $user User entity | ||
* @param string $template string, note the first_name of the user will be prepended if exists | ||
* | ||
* @return array email send result | ||
*/ | ||
protected function resetPassword(EntityInterface $user, $template = 'CakeDC/Users.reset_password') | ||
{ | ||
$firstName = isset($user['first_name'])? $user['first_name'] . ', ' : ''; | ||
$subject = __d('Users', '{0}Your reset password link', $firstName); | ||
|
||
$this | ||
->to($user['email']) | ||
->subject($subject) | ||
->viewVars($user->toArray()) | ||
->template($template); | ||
} | ||
|
||
/** | ||
* Send account validation email to the user | ||
* | ||
* @param EntityInterface $user User entity | ||
* @param EntityInterface $socialAccount SocialAccount entity | ||
* | ||
* @return array email send result | ||
*/ | ||
protected function socialAccountValidation(EntityInterface $user, EntityInterface $socialAccount) | ||
{ | ||
$firstName = isset($user['first_name'])? $user['first_name'] . ', ' : ''; | ||
//note: we control the space after the username in the previous line | ||
$subject = __d('Users', '{0}Your social account validation link', $firstName); | ||
$this | ||
->to($user['email']) | ||
->subject($subject) | ||
->viewVars(compact('user', 'socialAccount')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.