Created
December 1, 2016 18:02
-
-
Save JamesTheHacker/e36fb7221e533056a51609fa3f424909 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
namespace StudentApp\Services; | |
class EmailService extends Service | |
{ | |
public function sendPasswordResetToken($code, $email, $firstname, $lastname) | |
{ | |
/* | |
* Load the HTML from templates file | |
*/ | |
$htmlTemplate = file_get_contents($this->ci->get('settings')['passwordReset']['htmlTemplate']); | |
/* | |
* Replace variables in HTML template | |
*/ | |
$htmlTemplate = str_replace('{firstname}', $firstname, $htmlTemplate); | |
$htmlTemplate = str_replace('{code}', $code, $htmlTemplate); | |
/* | |
* Email Subject | |
*/ | |
$subject = 'Password reset'; | |
return $this->send($email, $firstname, $lastname, $subject, $htmlTemplate); | |
} | |
public function sendEmailVerification($code, $email, $firstname, $lastname) | |
{ | |
/* | |
* Load the HTML from templates file | |
*/ | |
$htmlTemplate = file_get_contents($this->ci->get('settings')['emailVerification']['htmlTemplate']); | |
/* | |
* Replace variables in HTML template | |
*/ | |
$verifyURL = "{$this->ci->get('settings')['domain']}/verify-email/{$code}"; | |
$htmlTemplate = str_replace('{firstname}', $firstname, $htmlTemplate); | |
$htmlTemplate = str_replace('{url}', $verifyURL, $htmlTemplate); | |
/* | |
* Email Subject | |
*/ | |
$subject = 'Verify your account'; | |
return $this->send($email, $firstname, $lastname, $subject, $htmlTemplate); | |
} | |
public function send($email, $firstname, $lastname, $subject, $htmlTemplate) | |
{ | |
/* | |
* Prepare the email to send | |
*/ | |
$message = [ | |
'html' => $htmlTemplate, | |
'subject' => $subject, | |
'from_email' => $this->ci->get('settings')['mandrill']['fromEmail'], | |
'from_name' => $this->ci->get('settings')['mandrill']['fromName'], | |
'to' => [ | |
[ | |
'email' => $email, | |
'name' => "{$firstname} {$lastname}", | |
'type' => 'to' | |
] | |
], | |
'important' => true, | |
]; | |
/* | |
* Attempt to send verification email to user | |
*/ | |
try | |
{ | |
$result = $this->ci->get('Mandrill')->messages->send($message)[0]; | |
} | |
catch(\Mandrill_Error $e) | |
{ | |
$this->ci->get('Logger')->addCritical('Error sending email verification: ' . $e->getMessage()); | |
return false; | |
} | |
catch(\Exception $e) | |
{ | |
$this->ci->get('Logger')->addCritical('Uncaught exception sending email verification: ' . $e->getMessage()); | |
return false; | |
} | |
/* | |
* Ensure 'status' index is set, and is set to 'send' | |
*/ | |
if(empty($result['status']) || $result['status'] != 'sent') | |
{ | |
$this->ci->get('Logger')->addCritical('No data in Mandrill response'); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment