Browse Source

Finished contact page implementation.

Alan Lam 8 năm trước cách đây
mục cha
commit
5b269ec6f1
4 tập tin đã thay đổi với 149 bổ sung và 4 xóa
  1. 49 0
      controllers/contact.php
  2. 86 0
      emails/contact.html
  3. 5 3
      engine/email.php
  4. 9 1
      views/contact.php

+ 49 - 0
controllers/contact.php

@@ -4,6 +4,55 @@ $page_title = "Contact";
 
 Core::addBreadcrumb("/contact", $page_title);
 
+$submit = http::value("submit");
+if($submit) {
+	try {
+		Core::validateCSRFToken($submit);
+
+		$name = http::value("name", "trim");
+		$email = http::value("email", "email");
+		$phone = http::value("phone", "trim");
+		$message = http::value("message", "trim");
+
+		$form_exception = new FormException;
+
+		if(!Form::checkRecaptcha()) $form_exception->addError("reCAPTCHA check failed.");
+		if(empty($name)) $form_exception->addError("Name is required.");
+		if(empty($email)) $form_exception->addError("Email address is invalid.");
+		if(empty($phone)) $form_exception->addError("Contact number is required.");
+		if(empty($message)) $form_exception->addError("Message is required.");
+
+		if($form_exception->hasErrors()) throw $form_exception;
+
+		$mail = new Email();
+
+		$mail->loadHtmlTemplate("contact");
+		$mail->setValues([
+			"name" => htmlspecialchars($name),
+			"email" => htmlspecialchars($email),
+			"phone" => htmlspecialchars($phone),
+			"message" => htmlspecialchars($message),
+		]);
+
+		$mail->setFrom(EMAIL_FROM, EMAIL_FROM_NAME);
+		$mail->addAddress("admin@" . DOMAIN);
+		$mail->addReplyTo($email);
+
+		$mail->setSubject("New message received");
+
+		$mail->send();
+
+		Core::addAlert("success", "Message sent successfully. We will get back to you shortly.");
+		HTTP::redirect("/contact");
+	}
+	catch(FormException $e) {
+		$e->generateAlerts();
+	}
+	catch(Exception $e) {
+		Core::addAlert("danger", $e->getMessage());
+	}
+}
+
 View::includeOnce("header", compact("page_title"));
 View::include("contact");
 View::includeOnce("footer");

+ 86 - 0
emails/contact.html

@@ -0,0 +1,86 @@
+<!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%">
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
+											<b>Hi,</b>
+										</td>
+									</tr>
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif; font-size: 24px;">
+											<b>A new message was sent from the website!</b>
+										</td>
+									</tr>
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
+											<b>Name:</b><br />
+											{name}
+										</td>
+									</tr>
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
+											<b>Email Address:</b><br />
+											{email}
+										</td>
+									</tr>
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
+											<b>Contact Number:</b><br />
+											{phone}
+										</td>
+									</tr>
+									<tr>
+										<td style="padding: 0 0 20px 0; font-family: Arial, sans-serif;">
+											<b>Message:</b><br />
+											{message}
+										</td>
+									</tr>
+									<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>
+										</td>
+									</tr>
+								</table>
+							</td>
+						</tr>
+					</table>
+				</td>
+			</tr>
+		</table>
+	</body>
+</html>

+ 5 - 3
engine/email.php

@@ -84,6 +84,10 @@ class Email {
 		return $this->instance->addAddress($email, $name);
 	}
 
+	public function addReplyTo($email, $name="") {
+		return $this->instance->addReplyTo($email, $name);
+	}
+
 	public function setSubject($subject) {
 		$this->instance->Subject = $subject;
 		return true;
@@ -109,14 +113,12 @@ class Email {
 
 		$instance->Body = $this->htmlContent;
 
-		if(PRODUCTION) {
+		if(PRODUCTION || DEBUG_MODE) {
 			return $this->instance->send();
 		}
 		else {
 			return true;
 		}
-
-
 	}
 
 }

+ 9 - 1
views/contact.php

@@ -39,7 +39,15 @@
 					<textarea class="form-control mb-3" name="message" placeholder="Enter message" rows="8" required></textarea>
 				</div>
 
-				<button class="btn btn-primary" type="submit" name="submit" value="1">Send Message</button>
+				<?php
+				if(GOOGLE_RECAPTCHA_SITE_KEY) {
+					?>
+					<div class="g-recaptcha my-3" data-sitekey="<?=GOOGLE_RECAPTCHA_SITE_KEY?>"></div>
+					<?php
+				}
+				?>
+
+				<button class="btn btn-primary" type="submit" name="submit" value="<?=$_SESSION["csrf_token"]?>">Send Message</button>
 			</form>
 		</div>
 	</div>