From 4d87b642c0afeef3b353a38e40843cf9f9fa66f3 Mon Sep 17 00:00:00 2001 From: Spencer14420 <67592325+Spencer14420@users.noreply.github.com> Date: Wed, 13 Nov 2024 11:13:52 -0500 Subject: [PATCH] Use PHPMailer (#2) Implement PHPMailer instead of using mail() directly. --- composer.json | 5 ++- composer.lock | 94 +++++++++++++++++++++++++++++++++++++++++--- src/EmailHandler.php | 26 ++++++++---- 3 files changed, 110 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 45b55ae..fb54a6a 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,10 @@ } ], "minimum-stability": "dev", - "require": {}, + "prefer-stable": true, + "require": { + "phpmailer/phpmailer": "^6.9" + }, "autoload": { "psr-4": { "spencer14420\\PhpEmailHandler\\": "src/" diff --git a/composer.lock b/composer.lock index 4e29fa8..d4eef03 100644 --- a/composer.lock +++ b/composer.lock @@ -4,15 +4,97 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c0e392d32a00439a2226f95df0ad0722", - "packages": [], + "content-hash": "84b33ef1e0e22e297859977fc006ac94", + "packages": [ + { + "name": "phpmailer/phpmailer", + "version": "v6.9.2", + "source": { + "type": "git", + "url": "https://github.com/PHPMailer/PHPMailer.git", + "reference": "a7b17b42fa4887c92146243f3d2f4ccb962af17c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/a7b17b42fa4887c92146243f3d2f4ccb962af17c", + "reference": "a7b17b42fa4887c92146243f3d2f4ccb962af17c", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "ext-filter": "*", + "ext-hash": "*", + "php": ">=5.5.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.0", + "doctrine/annotations": "^1.2.6 || ^1.13.3", + "php-parallel-lint/php-console-highlighter": "^1.0.0", + "php-parallel-lint/php-parallel-lint": "^1.3.2", + "phpcompatibility/php-compatibility": "^9.3.5", + "roave/security-advisories": "dev-latest", + "squizlabs/php_codesniffer": "^3.7.2", + "yoast/phpunit-polyfills": "^1.0.4" + }, + "suggest": { + "decomplexity/SendOauth2": "Adapter for using XOAUTH2 authentication", + "ext-mbstring": "Needed to send email in multibyte encoding charset or decode encoded addresses", + "ext-openssl": "Needed for secure SMTP sending and DKIM signing", + "greew/oauth2-azure-provider": "Needed for Microsoft Azure XOAUTH2 authentication", + "hayageek/oauth2-yahoo": "Needed for Yahoo XOAUTH2 authentication", + "league/oauth2-google": "Needed for Google XOAUTH2 authentication", + "psr/log": "For optional PSR-3 debug logging", + "symfony/polyfill-mbstring": "To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2)", + "thenetworg/oauth2-azure": "Needed for Microsoft XOAUTH2 authentication" + }, + "type": "library", + "autoload": { + "psr-4": { + "PHPMailer\\PHPMailer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-2.1-only" + ], + "authors": [ + { + "name": "Marcus Bointon", + "email": "phpmailer@synchromedia.co.uk" + }, + { + "name": "Jim Jagielski", + "email": "jimjag@gmail.com" + }, + { + "name": "Andy Prevost", + "email": "codeworxtech@users.sourceforge.net" + }, + { + "name": "Brent R. Matzelle" + } + ], + "description": "PHPMailer is a full-featured email creation and transfer class for PHP", + "support": { + "issues": "https://github.com/PHPMailer/PHPMailer/issues", + "source": "https://github.com/PHPMailer/PHPMailer/tree/v6.9.2" + }, + "funding": [ + { + "url": "https://github.com/Synchro", + "type": "github" + } + ], + "time": "2024-10-09T10:07:50+00:00" + } + ], "packages-dev": [], "aliases": [], "minimum-stability": "dev", - "stability-flags": {}, - "prefer-stable": false, + "stability-flags": [], + "prefer-stable": true, "prefer-lowest": false, - "platform": {}, - "platform-dev": {}, + "platform": [], + "platform-dev": [], "plugin-api-version": "2.6.0" } diff --git a/src/EmailHandler.php b/src/EmailHandler.php index 0c678e7..66f2c33 100644 --- a/src/EmailHandler.php +++ b/src/EmailHandler.php @@ -1,6 +1,8 @@ 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']); }