aboutsummaryrefslogtreecommitdiff
path: root/app/model/Form.php
blob: 4e6d3f39238e83903e37ef23707ddcc5f115bb42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php

// PHP mailer namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class Form
{
    public $name;
    public $email;
    public $message;

    public function __construct($name, $email, $message)
    {
        $this->name = $name;
        $this->email = $email;
        $this->message = $message;

        $this->isSpam();
        $this->isEmpty();
    }

    public function isSpam()
    {
        $spam = false;

        if (isset($_POST['contact'])) {
            $spam = $_POST['contact'];
        }

        if ((bool) $spam == true) {
            http_response_code(403);
            error_log('Contact Form Spam: Error 403');
            exit;
        }
    }

    public function isEmpty()
    {
        if ((bool) empty($this->name) == true
            || (bool) empty($this->email) == true
            || (bool) empty($this->message) == true
        ) {
            header('Location: /contact');
            exit;
        }
    }

    public function isSubmit()
    {
        // Include mail config
        $config = include '../AppConfig.php';

        $mail = new PHPMailer(true);

        try {
            //Server settings
            //$mail->SMTPDebug = 2;                             // Enable verbose debug output
            $mail->isSMTP();                                    // Set mailer to use SMTP
            $mail->Host = $config['mail']['host'];              // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                             // Enable SMTP authentication
            $mail->Username = $config['mail']['username'];      // SMTP username
            $mail->Password = $config['mail']['password'];      // SMTP password
            $mail->SMTPSecure = 'ssl';                          // Enable TLS encryption, `ssl` also accepted
            $mail->Port = $config['mail']['port'];              // TCP port to connect to

            //Recipients
            $mail->setFrom('thedroneely@gmail.com', 'Thedro Neely');
            $mail->addAddress('thedroneely@gmail.com', 'Thedro Neely');
            $mail->addReplyTo($this->email, $this->name);

            //Content
            $mail->isHTML(true);
            $mail->Subject = 'New message from ' . $this->name;
            $mail->Body    = $this->message;
            $mail->AltBody = $this->message;

            //Send Mail
            $mail->send();

        } catch (Exception $e) {
            include '../app/views/mail-error.view.php';
        }
    }
}