1
0
Эх сурвалжийг харах

Added email builder. Converted email calls to use builder instead of templates.

Alan Lam 8 жил өмнө
parent
commit
c7739b8c40

+ 1 - 0
.gitignore

@@ -5,3 +5,4 @@ vendor/
 reference/
 logs/
 tasks/PraesepeVenues.csv
+tasks/email_test.php

+ 17 - 9
controllers/contact.php

@@ -24,20 +24,28 @@ if($submit) {
 
 		if($form_exception->hasErrors()) throw $form_exception;
 
-		$mail = new Email();
+		// Content
+		$name = htmlspecialchars($name);
+		$email = htmlspecialchars($email);
+		$phone = htmlspecialchars($phone);
+		$message = htmlspecialchars($message);
 
-		$mail->loadHtmlTemplate("contact");
-		$mail->setValues([
-			"name" => htmlspecialchars($name),
-			"email" => htmlspecialchars($email),
-			"phone" => htmlspecialchars($phone),
-			"message" => htmlspecialchars($message),
-		]);
+		// Build
+		$builder = new EmailBuilder;
+		$builder->addParagraph("<b>Hi,</b>");
+		$builder->addLeadParagraph("A new message was sent from the website!");
+		$builder->addParagraph("<b>Name:</b><br />$name");
+		$builder->addParagraph("<b>Email:</b><br />$email");
+		$builder->addParagraph("<b>Contact Number:</b><br />$phone");
+		$builder->addParagraph("<b>Message:</b><br />$message");
+		$content = $builder->build();
 
+		// Send
+		$mail = new Email;
+		$mail->setHtmlContent($content);
 		$mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
 		$mail->addAddress("admin@" . DOMAIN);
 		$mail->addReplyTo($email);
-
 		$mail->setSubject("New message received");
 
 		$mail->send();

+ 24 - 18
engine/email.php

@@ -48,24 +48,6 @@ class Email {
 
 		$content = file_get_contents($path);
 
-		// Embed images
-		$match_count = preg_match_all("/src\=\"(\.\.\/public\/img\/[^\"]*)\"/i", $content, $all_matches);
-		if($match_count > 0) {
-
-			$paths = $all_matches[1];
-
-			foreach($paths as $path) {
-				$filename = basename($path);
-				$cid = str_replace(".", "_", $filename);
-
-				$path = ltrim($path, "./");
-
-				$this->instance->addEmbeddedImage($path, $cid, $filename);
-				$content = str_replace("../$path", "cid:$cid", $content);
-
-			}
-		}
-
 		$this->htmlContent = $content;
 	}
 
@@ -93,6 +75,12 @@ class Email {
 		return true;
 	}
 
+	public function setHtmlContent($string) {
+		$this->instance->IsHTML(true);
+
+		$this->htmlContent = $string;
+	}
+
 	public function send() {
 		// Does our sanity checks and then send email
 		$instance = $this->instance;
@@ -102,6 +90,24 @@ class Email {
 		if(empty($instance->Subject)) throw new Exception("No subject is set.");
 		if(empty($this->htmlContent)) throw new Exception("No content is set.");
 
+		// Embed images
+		$match_count = preg_match_all("/src\=\"(\.\.\/public\/img\/[^\"]*)\"/i", $this->htmlContent, $all_matches);
+		if($match_count > 0) {
+
+			$paths = $all_matches[1];
+
+			foreach($paths as $path) {
+				$filename = basename($path);
+				$cid = str_replace(".", "_", $filename);
+
+				$path = ltrim($path, "./");
+
+				$this->instance->addEmbeddedImage($path, $cid, $filename);
+				$this->htmlContent = str_replace("../$path", "cid:$cid", $this->htmlContent);
+
+			}
+		}
+
 		$count = preg_match_all("/\{([a-z\-]+)\}/", $this->htmlContent, $matches);
 		if($count > 0) {
 			$missing_keys = [];

+ 120 - 0
engine/email_builder.php

@@ -0,0 +1,120 @@
+<?php
+
+class EmailBuilder {
+
+	private $parts = [];
+
+	public function __construct() {
+		$this->parts[] = <<<EOD
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+	<head>
+		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+		<title>Check Policy</title>
+		<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+	</head>
+	<body>
+		<table align="center" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse;">
+			<tr>
+				<td>
+					<table border="0" cellpadding="0" cellspacing="0" width="600" style="border: 1px solid #cccccc;">
+						<tr>
+							<td align="center" style="padding: 40px 0 40px 0; border-bottom: 1px solid #cccccc;">
+								<a href="">
+									<img src="../public/img/logo.png" width="386" height="80" style="display: block;" alt="Check Policy logo" />
+								</a>
+							</td>
+						</tr>
+						<tr>
+							<td style="padding: 40px 30px 40px 30px;">
+								<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
+EOD;
+	}
+
+	public function addLeadParagraph($html) {
+		$this->parts[] = <<<EOD
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif; font-size: 24px;">
+											<b>$html</b>
+										</td>
+									</tr>
+EOD;
+
+	}
+
+	public function addParagraph($html) {
+		$this->parts[] = <<<EOD
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
+											$html
+										</td>
+									</tr>
+EOD;
+	}
+
+	public function addAcceptDeclineButtons($base_url) {
+		$this->parts[] = <<<EOD
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;" align="center">
+
+											<table cellpadding="0" cellspacing="0" width="100%">
+												<tr>
+													<td width="260" valign="top" align="center">
+														<a href="$base_url/accept" style="display: block; background: #4CAF50; padding: 10px; color: white; text-decoration: none;">
+															Accept
+														</a>
+													</td>
+													<td width="20" style="font-size: 0; line-height: 0;">
+													</td>
+													<td width="260" valign="top" align="center">
+														<a href="$base_url/decline" style="display: block; background: #F44336; padding: 10px; color: white; text-decoration: none;">
+															Decline
+														</a>
+													</td>
+												</tr>
+											</table>
+
+										</td>
+									</tr>
+EOD;
+	}
+
+	public function build() {
+
+		$this->parts[] = <<<EOD
+									<tr>
+										<td style="font-family: Arial, sans-serif;">
+											Check Policy Team
+										</td>
+									</tr>
+								</table>
+							</td>
+						</tr>
+						<tr>
+							<td bgcolor="#4CAF50" style="padding: 40px 30px 40px 30px;">
+								<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
+									<tr>
+										<td style="font-family: Arial, sans-serif;">
+											<b><font color="#ffffff">&copy; Check Policy 2018</font></b>
+										</td>
+										<td align="right" style="font-family: Arial, sans-serif;">
+											<a href="https://www.facebook.com/checkpolicy" style="color: #ffffff;"><font color="#ffffff">Facebook</font></a><br />
+											<a href="https://api.whatsapp.com/send?phone=447841353775" style="color: #ffffff;"><font color="#ffffff">WhatsApp</font></a>
+										</td>
+									</tr>
+								</table>
+							</td>
+						</tr>
+					</table>
+				</td>
+			</tr>
+		</table>
+	</body>
+</html>
+EOD;
+
+		return implode("\n", $this->parts);
+
+	}
+
+}

+ 27 - 20
engine/test.php

@@ -48,31 +48,38 @@ class Test extends Model {
 		$tester = $this->getTester();
 		$location = $this->getLocation();
 
-		$tester_message = "";
-		if($message) {
-			$tester_message = Email::getTextCellHtml($message);
-		}
-
+		// Content
+		$name = htmlspecialchars($tester->first_name);
+		$message = htmlspecialchars($message);
+		$base_url = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id);
+		$address = htmlspecialchars($location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]));
 		$date_due = date("j M Y", strtotime($this->dts_due));
+		$admin_first_name = htmlspecialchars(Core::$user->first_name);
+		$admin_phone = htmlspecialchars(Core::$user->phone);
 
-		$mail = new Email();
-
-		$mail->loadHtmlTemplate("test_assign");
-		$mail->setValues([
-			"name" => $tester->first_name,
-			"tester-message" => $tester_message,
-			"test-url" => PROTOCOL . "://" . DOMAIN . "/account/tests/" . $this->id,
-			"address" => $location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]),
-			"date-due" => $date_due,
-			"admin-first-name" => Core::$user->first_name,
-			"admin-phone" => Core::$user->phone
-		]);
-
+		// Build
+		$builder = new EmailBuilder;
+		$builder->addParagraph("<b>Hi $name,</b>");
+		$builder->addLeadParagraph("Congrats, you have been allocated a test!");
+		if($message) {
+			$builder->addParagraph($message);
+		}
+		$builder->addParagraph("We need you to confirm if you are able to do the test. Please check the details below and then confirm by pressing the <b>Accept</b> or <b>Decline</b> buttons and once you tap <b>Accept</b>, the test will be allocated to you.");
+		$builder->addAcceptDeclineButtons($base_url);
+		$builder->addParagraph("The site address is: <b>$address</b>");
+		$builder->addParagraph("The date that need to complete and submit the results by is: <font color=\"red\"><b>$date_due</b></font>");
+		$builder->addParagraph("For methodology and further notes please check the <b>Useful Documents</b> section on the <b>Account</b> page.");
+		$builder->addParagraph("If you agree to conduct the test, log in and then go to <b>Tests</b> where you will be able to see the tests available to you. Please tap the <b>Begin</b> button and follow the instructions that is shown.");
+		$builder->addParagraph("If you have any questions please send them to <a href=\"mailto:tests@check-policy.com\">tests@check-policy.com</a> or call <b>$admin_first_name</b> on <b>$admin_phone</b>.");
+		$builder->addParagraph("If you are unable to submit the results via the web-based app please contact us and we can send you an offline form to complete. Please accept the test by tapping the <b>Accept</b> button and email us back saying you would like an offline form.");
+		$content = $builder->build();
+
+		// Send
+		$mail = new Email;
+		$mail->setHtmlContent($content);
 		$mail->setFrom(EMAIL_CONTACT, EMAIL_FROM_NAME);
 		$mail->addAddress($tester->email, $tester->getDisplayName());
-
 		$mail->setSubject("You have been allocated a test");
-
 		$mail->send();
 	}
 

+ 45 - 24
engine/user.php

@@ -58,14 +58,21 @@ class User extends Model {
 		$this->verify_token = bin2hex(random_bytes(16));
 		$this->commit();
 
-		$mail = new Email();
-
-		$mail->loadHtmlTemplate("registration");
-		$mail->setValues([
-			"name" => $this->first_name,
-			"link" => PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token
-		]);
-
+		// Content
+		$name = htmlspecialchars($this->first_name);
+		$link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/verify-email/" . $this->verify_token);
+
+		// Build
+		$builder = new EmailBuilder;
+		$builder->addParagraph("<b>Hi $name</b>");
+		$builder->addLeadParagraph("Thanks for registering!");
+		$builder->addParagraph("Welcome to Check Policy! Before you can log in, you will need to verify your email address. You can do so by tapping the following link:");
+		$builder->addParagraph("<a href=\"$link\">$link</a>");
+		$content = $builder->build();
+
+		// Send
+		$mail = new Email;
+		$mail->setHtmlContent($content);
 		$mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
 		$mail->addAddress($this->email, $this->getDisplayName());
 
@@ -78,19 +85,24 @@ class User extends Model {
 
 	public function sendWelcomeEmail() {
 
-		$mail = new Email();
-
-		$mail->loadHtmlTemplate("welcome");
-		$mail->setValues([
-			"name" => $this->first_name,
-			"link" => PROTOCOL . "://" . DOMAIN . "/account/submit-id"
-		]);
-
+		// Content
+		$name = htmlspecialchars($this->first_name);
+		$link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/account/submit-id");
+
+		// Build
+		$builder = new EmailBuilder;
+		$builder->addParagraph("<b>Hi $name</b>");
+		$builder->addLeadParagraph("Thanks for verifying your email address!");
+		$builder->addParagraph("Welcome to Check Policy! Before you can take part in tests, you will need to provide us with some ID. You can do so by tapping the following link:");
+		$builder->addParagraph("<a href=\"$link\">$link</a>");
+		$content = $builder->build();
+
+		// Send
+		$mail = new Email;
+		$mail->setHtmlContent($content);
 		$mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
 		$mail->addAddress($this->email, $this->getDisplayName());
-
 		$mail->setSubject("Welcome to Check Policy");
-
 		$mail->send();
 
 		return true;
@@ -102,14 +114,23 @@ class User extends Model {
 		$this->reset_token = bin2hex(random_bytes(16));
 		$this->commit();
 
-		$mail = new Email();
+		// Content
+		$name = htmlspecialchars($this->first_name);
+		$link = htmlspecialchars(PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token);
 
-		$mail->loadHtmlTemplate("password_reset");
-		$mail->setValues([
-			"name" => $this->first_name,
-			"link" => PROTOCOL . "://" . DOMAIN . "/reset-password/" . $this->reset_token
-		]);
+		// Build
+		$builder = new EmailBuilder;
+		$builder->addParagraph("<b>Hi $name</b>");
+		$builder->addLeadParagraph("A reset password request has been received.");
+		$builder->addParagraph("To reset your password, please tap on the following link:");
+		$builder->addParagraph("<a href=\"$link\">$link</a>");
+		$builder->addParagraph("If you <strong>did not</strong> request for your password to be reset, please ignore this email.");
+		$content = $builder->build();
+
+		// Send
 
+		$mail = new Email;
+		$mail->setHtmlContent($content);
 		$mail->setFrom(EMAIL_NO_REPLY, EMAIL_FROM_NAME);
 		$mail->addAddress($this->email, $this->getDisplayName());
 

+ 0 - 25
tasks/email_test.php

@@ -1,25 +0,0 @@
-<?php
-
-require "../initialise.php";
-
-try {
-	$mail = new Email();
-
-	$mail->loadHtmlTemplate("registration");
-	$mail->setValues([
-		"name" => "Alan",
-		"link" => "http://local.check-policy.co.uk/verify"
-	]);
-
-	$mail->setFrom("admin@check-policy.co.uk", "Check Policy Admin");
-	$mail->addAddress("alanhmlam@gmail.com", "Alan Lam");
-
-	$mail->setSubject("Check Policy Registration Email Test");
-
-	$mail->send();
-
-	echo "Message sent.";
-}
-catch(Exception $e) {
-	echo $e->getMessage();
-}