Skip to content

Commit

Permalink
update Contact controller and view: add contact_us_successful flag, c…
Browse files Browse the repository at this point in the history
…hange email message generation, and add success dialog to view
  • Loading branch information
Divyeshhhh committed Jun 2, 2024
1 parent 6ee328a commit 84bfda6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/controllers/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function __construct()
$this->view_data['defaultEmail'] = "";
$this->view_data['defaultMessage'] = "";
$this->view_data['errors'] = [];
$this->view_data['contact_us_successful'] = false;

}

/**
Expand Down Expand Up @@ -84,7 +86,7 @@ private function handleFormSubmission(): void
if (empty($this->view_data['errors'])) {
$success = $this->mailBusinessInbox($form_data);
if ($success) {
Utility::redirect('home');
$this->view_data['contact_us_successful'] = true;
} else {
(new Error())->handleMailingError();
}
Expand All @@ -100,18 +102,18 @@ private function handleFormSubmission(): void
*/
private function mailBusinessInbox($form_data): bool
{
// Concatenate form data into the email message
$htmlMessage = "You have received a new message from: <br>";
$htmlMessage .= "Name: " . $form_data['first_name'] . " " . $form_data['last_name'] . "<br>";
$htmlMessage .= "Email: " . $form_data['email'] . "<br>";
$htmlMessage .= "Message: " . $form_data['message'] . "<br>";
// Load the email template
ob_start();
extract($form_data);
include __DIR__ . '/../views/mails/Contact.php';
$htmlMessage = ob_get_clean();

$plainMessage = "You have received a new message from:\n";
$plainMessage .= "Name: " . $form_data['first_name'] . " " . $form_data['last_name'] . "\n";
$plainMessage .= "Email: " . $form_data['email'] . "\n";
$plainMessage .= "Message: " . $form_data['message'] . "\n";

//Implement logic to send email to admin using Mailer class
// Implement logic to send email to admin using Mailer class
$mailer = new Mailer();
$subject = "New Contact Message from " . $form_data['first_name'] . " " . $form_data['last_name'];

Expand Down
12 changes: 11 additions & 1 deletion src/views/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,14 @@
<input type="submit" name="form_submit" value="Submit">
</form>
</div>
</main>
</main>

<dialog <?= $contact_us_successful ? "open" : "" ?>>
<article>
<h3>Thank You for Contacting Us! 🔎</h3>
<p>Your message has been successfully sent. Our team will review your inquiry and get back to you shortly. We appreciate your interest in our services.</p>
<footer>
<a href="/home" role="button" data-target="my-modal">Return to Home</a>
</footer>
</article>
</dialog>
51 changes: 51 additions & 0 deletions src/views/mails/Contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>New Contact Message</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
background-color: #f4f4f4;
padding: 20px;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h2 {
color: #555;
}
p {
margin: 0 0 10px;
}
.footer {
margin-top: 20px;
font-size: 0.9em;
color: #888;
}
</style>
</head>
<body>
<div class="container">
<h2>New Contact Message</h2>
<p><strong>Name:</strong> <?= htmlspecialchars($first_name) ?> <?= htmlspecialchars($last_name) ?></p>
<p><strong>Email:</strong> <?= htmlspecialchars($email) ?></p>
<p><strong>Message:</strong></p>
<p><?= nl2br(htmlspecialchars($message)) ?></p>
<div class="footer">
<p>This message was sent via the contact form on your website.</p>
</div>
</div>
</body>
</html>

0 comments on commit 84bfda6

Please sign in to comment.