Skip to content

Commit

Permalink
Use PHPMailer (#2)
Browse files Browse the repository at this point in the history
Implement PHPMailer instead of using mail() directly.
  • Loading branch information
Spencer14420 authored Nov 13, 2024
1 parent dcf918e commit 4d87b64
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 15 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
}
],
"minimum-stability": "dev",
"require": {},
"prefer-stable": true,
"require": {
"phpmailer/phpmailer": "^6.9"
},
"autoload": {
"psr-4": {
"spencer14420\\PhpEmailHandler\\": "src/"
Expand Down
94 changes: 88 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 18 additions & 8 deletions src/EmailHandler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace spencer14420\PhpEmailHandler;

use PHPMailer\PHPMailer\PHPMailer;

class EmailHandler
{
private $mailboxEmail;
Expand Down Expand Up @@ -69,18 +71,26 @@ public function handleRequest()
}

// Prepare and send the main email to the mailbox
$headers = "From: {$this->siteName} <{$this->fromEmail}>\r\nReply-To: $email";
$body = "From: {$name} ({$email})\n\nMessage:\n" . wordwrap($message, 70);
$messageSent = mail($this->mailboxEmail, "Message from {$name} via {$this->siteDomain}", $body, $headers);
$inquryEmail = new PHPMailer();

$inquryEmail->setFrom($this->fromEmail, $this->siteName);
$inquryEmail->addReplyTo($email);
$inquryEmail->addAddress($this->mailboxEmail, $this->siteName);
$inquryEmail->Subject = "Message from $name via $this->siteDomain";
$inquryEmail->Body = "From: {$name} ({$email})\n\nMessage:\n" . wordwrap($message, 70);

if (!$messageSent) {
$this->jsonErrorResponse("Failed to send the message. Please try again later.", 500);
if (!$inquryEmail->send()) {
$this->jsonErrorResponse("Error: ". $inquryEmail->ErrorInfo, 500);
}

// Prepare and send the confirmation email to the sender
$headers = "From: {$this->siteName} <{$this->fromEmail}>\r\nReply-To: {$this->replyToEmail}";
$confirmationMessage = "Dear {$name} ({$email}),\n\nYour message (shown below) has been received. We will get back to you as soon as possible.\n\nSincerely,\n{$this->siteName}\n\nPlease note: This message was sent to the email address provided in our contact form. If you did not enter your email, please disregard this message.\n\nYour message:\n" . wordwrap($message, 70);
mail($email, "Your message to {$this->siteName} has been received", $confirmationMessage, $headers);
$confirmationEmail = new PHPMailer();
$confirmationEmail->setFrom($this->fromEmail, $this->siteName);
$confirmationEmail->addReplyTo($this->replyToEmail);
$confirmationEmail->addAddress($email);
$confirmationEmail->Subject = "Your message to $this->siteName has been received";
$confirmationEmail->Body = "Dear $name ($email),\n\nYour message (shown below) has been received. We will get back to you as soon as possible.\n\nSincerely,\n$this->siteName\n\nPlease note: This message was sent to the email address provided in our contact form. If you did not enter your email, please disregard this message.\n\nYour message:\n$message";
$confirmationEmail->send();

echo json_encode(['status' => 'success']);
}
Expand Down

0 comments on commit 4d87b64

Please sign in to comment.