Browse Source

Add support for email notification for contact form messages, close #29

Skylar Ittner 7 months ago
parent
commit
9887a96c74
6 changed files with 134 additions and 5 deletions
  1. 2
    1
      composer.json
  2. 68
    2
      composer.lock
  3. 2
    0
      lang/en_us.php
  4. 14
    0
      pages/sitesettings.php
  5. 40
    2
      public/contact.php
  6. 8
    0
      settings.template.php

+ 2
- 1
composer.json View File

@@ -6,7 +6,8 @@
6 6
         "catfan/medoo": "^1.5",
7 7
         "guzzlehttp/guzzle": "^6.2",
8 8
         "geoip2/geoip2": "~2.0",
9
-        "unsplash/unsplash": "^2.4"
9
+        "unsplash/unsplash": "^2.4",
10
+        "phpmailer/phpmailer": "^6.0"
10 11
     },
11 12
     "license": "MPL-2.0",
12 13
     "authors": [

+ 68
- 2
composer.lock View File

@@ -4,8 +4,8 @@
4 4
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5 5
         "This file is @generated automatically"
6 6
     ],
7
-    "hash": "14eefa1d98fa62ca2f2fe52de606868c",
8
-    "content-hash": "e9f318b9d8bd1dfa6c7fb56c820d1910",
7
+    "hash": "f30e715ebe71e016a347feec9c9dc5bf",
8
+    "content-hash": "9efd5e7ff4f253d9ef07ae1535880ffb",
9 9
     "packages": [
10 10
         {
11 11
             "name": "catfan/medoo",
@@ -624,6 +624,72 @@
624 624
             ],
625 625
             "time": "2018-04-04 21:24:14"
626 626
         },
627
+        {
628
+            "name": "phpmailer/phpmailer",
629
+            "version": "v6.0.5",
630
+            "source": {
631
+                "type": "git",
632
+                "url": "https://github.com/PHPMailer/PHPMailer.git",
633
+                "reference": "cb3ea134d4d3729e7857737d5f320cce9caf4d32"
634
+            },
635
+            "dist": {
636
+                "type": "zip",
637
+                "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/cb3ea134d4d3729e7857737d5f320cce9caf4d32",
638
+                "reference": "cb3ea134d4d3729e7857737d5f320cce9caf4d32",
639
+                "shasum": ""
640
+            },
641
+            "require": {
642
+                "ext-ctype": "*",
643
+                "ext-filter": "*",
644
+                "php": ">=5.5.0"
645
+            },
646
+            "require-dev": {
647
+                "doctrine/annotations": "1.2.*",
648
+                "friendsofphp/php-cs-fixer": "^2.2",
649
+                "phpdocumentor/phpdocumentor": "2.*",
650
+                "phpunit/phpunit": "^4.8 || ^5.7",
651
+                "zendframework/zend-eventmanager": "3.0.*",
652
+                "zendframework/zend-i18n": "2.7.3",
653
+                "zendframework/zend-serializer": "2.7.*"
654
+            },
655
+            "suggest": {
656
+                "ext-mbstring": "Needed to send email in multibyte encoding charset",
657
+                "hayageek/oauth2-yahoo": "Needed for Yahoo XOAUTH2 authentication",
658
+                "league/oauth2-google": "Needed for Google XOAUTH2 authentication",
659
+                "psr/log": "For optional PSR-3 debug logging",
660
+                "stevenmaguire/oauth2-microsoft": "Needed for Microsoft XOAUTH2 authentication",
661
+                "symfony/polyfill-mbstring": "To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2)"
662
+            },
663
+            "type": "library",
664
+            "autoload": {
665
+                "psr-4": {
666
+                    "PHPMailer\\PHPMailer\\": "src/"
667
+                }
668
+            },
669
+            "notification-url": "https://packagist.org/downloads/",
670
+            "license": [
671
+                "LGPL-2.1"
672
+            ],
673
+            "authors": [
674
+                {
675
+                    "name": "Jim Jagielski",
676
+                    "email": "jimjag@gmail.com"
677
+                },
678
+                {
679
+                    "name": "Marcus Bointon",
680
+                    "email": "phpmailer@synchromedia.co.uk"
681
+                },
682
+                {
683
+                    "name": "Andy Prevost",
684
+                    "email": "codeworxtech@users.sourceforge.net"
685
+                },
686
+                {
687
+                    "name": "Brent R. Matzelle"
688
+                }
689
+            ],
690
+            "description": "PHPMailer is a full-featured email creation and transfer class for PHP",
691
+            "time": "2018-03-27 13:49:45"
692
+        },
627 693
         {
628 694
             "name": "psr/http-message",
629 695
             "version": "1.0.1",

+ 2
- 0
lang/en_us.php View File

@@ -141,4 +141,6 @@ define("STRINGS", [
141 141
     "new" => "New",
142 142
     "search" => "Search",
143 143
     "no results" => "No results.",
144
+    "contact form" => "Contact Form",
145
+    "contact form messages will be forwarded to this email address" => "Contact form messages will be forwarded to this email address, if it is set.",
144 146
 ]);

+ 14
- 0
pages/sitesettings.php View File

@@ -245,6 +245,20 @@ function getsetting($name) {
245 245
                         </div>
246 246
                     </div>
247 247
 
248
+                    <!-- Contact Form -->
249
+                    <div class="card mt-4">
250
+                        <div class="card-body">
251
+                            <h5 class="card-title"><i class="fas fa-comments"></i> <?php lang("contact form"); ?></h5>
252
+                            <div class="input-group">
253
+                                <div class="input-group-prepend">
254
+                                    <span class="input-group-text"><label for="contactemail"><i class="fas fa-envelope"></i> Forward to:</label></span>
255
+                                </div>
256
+                                <input type="email" class="form-control" name="settings[contactemail]" id="contactemail" value="<?php echo getsetting("contactemail"); ?>" />
257
+                            </div>
258
+                            <small class="form-text"><?php lang("contact form messages will be forwarded to this email address"); ?></small>
259
+                        </div>
260
+                    </div>
261
+
248 262
                     <!-- Extra code header snippets -->
249 263
                     <div class="card mt-4 mb-4">
250 264
                         <div class="card-body">

+ 40
- 2
public/contact.php View File

@@ -4,9 +4,13 @@
4 4
  * License, v. 2.0. If a copy of the MPL was not distributed with this
5 5
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 6
  */
7
+ignore_user_abort(true);
7 8
 
8 9
 require __DIR__ . "/../lib/requiredpublic.php";
9 10
 
11
+use PHPMailer\PHPMailer\PHPMailer;
12
+use PHPMailer\PHPMailer\Exception;
13
+
10 14
 function output_card($content) {
11 15
     ?>
12 16
     <!DOCTYPE html>
@@ -33,8 +37,10 @@ END;
33 37
     die();
34 38
 }
35 39
 
40
+$siteid = getsiteid();
41
+
36 42
 $database->insert("messages", [
37
-    "siteid" => getsiteid(),
43
+    "siteid" => $siteid,
38 44
     "name" => htmlspecialchars($_POST['name']),
39 45
     "email" => htmlspecialchars($_POST['email']),
40 46
     "message" => htmlspecialchars($_POST['message']),
@@ -47,4 +53,36 @@ $content = <<<END
47 53
 <a href="./" class="btn btn-success">Continue</a>
48 54
 END;
49 55
 
50
-output_card($content);
56
+output_card($content);
57
+ob_flush();
58
+flush();
59
+
60
+
61
+if ($database->has('settings', ["AND" => ['siteid' => $siteid, 'key' => 'contactemail']])) {
62
+    $emailto = $database->get('settings', "value", ["AND" => ['siteid' => $siteid, 'key' => 'contactemail']]);
63
+    // Setup mailer
64
+    $mail = new PHPMailer(true);
65
+    $mail->isSMTP();
66
+    $mail->Host = SMTP_HOST;
67
+    $mail->SMTPAuth = SMTP_AUTH;
68
+    if (SMTP_AUTH) {
69
+        $mail->Username = SMTP_USERNAME;
70
+        $mail->Password = SMTP_PASSWORD;
71
+    }
72
+    if (SMTP_SECURITY != "none") {
73
+        $mail->SMTPSecure = SMTP_SECURITY;
74
+    }
75
+    $mail->Port = SMTP_PORT;
76
+    $mail->isHTML(true);
77
+    $mail->setFrom(SMTP_FROMADDRESS, SMTP_FROMNAME);
78
+
79
+    $mail->addAddress($emailto);
80
+    $mail->addReplyTo($_POST['email'], $_POST['name']);
81
+
82
+    $mail->Subject = 'Website Contact Form Message';
83
+    $mail->Body = '<p><b>From:</b> ' . htmlspecialchars($_POST['name']) . ' &lt;<a href="mailto:' . htmlspecialchars($_POST['email']) . '">' . $_POST['email'] . '</a>&gt;</p>'
84
+            . '<p><b>Message:</b> <br>' . htmlspecialchars($_POST['message']) . '</p>';
85
+    $mail->AltBody = "From: $_POST[name] <$_POST[email]>\r\n\r\nMessage: \r\n$_POST[message]";
86
+
87
+    $mail->send();
88
+}

+ 8
- 0
settings.template.php View File

@@ -20,6 +20,14 @@ define("DB_CHARSET", "utf8");
20 20
 // Name of the app.
21 21
 define("SITE_TITLE", "SiteWriter");
22 22
 
23
+define("SMTP_HOST", "");
24
+define("SMTP_AUTH", true);
25
+define("SMTP_SECURITY", "tls"); // tls, ssl, or none
26
+define("SMTP_PORT", 25);
27
+define("SMTP_USERNAME", "");
28
+define("SMTP_PASSWORD", "");
29
+define("SMTP_FROMADDRESS", "sitewriter@example.com");
30
+define("SMTP_FROMNAME", "SiteWriter");
23 31
 
24 32
 // URL of the AccountHub API endpoint
25 33
 define("PORTAL_API", "http://localhost/accounthub/api.php");

Loading…
Cancel
Save