summaryrefslogtreecommitdiff
path: root/cgi/contact.php
blob: c6d18d9e5c685377505860e5cd9231fb13e15f7c (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
<?php

use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;

session_start();

function missing_field(string $name): never {
    http_response_code(400);
    echo "Missing or malformed field $name\n";
    exit;
}

function getenv_or_fail(string $key): ?string {
    $value = getenv($key);
    if ($value !== false)
        return $value;
    http_response_code(500);
    echo "Missing environment variable $key\n";
    exit;
}

if ($_SERVER["REQUEST_METHOD"] !== "POST") {
    http_response_code(405);
    echo "Invalid request method!\n";
    exit;
}

$name = $_POST["name"] ?? missing_field("name");
if (empty($name) || strlen($name) > 128)
    missing_field("name");

$email = $_POST["email"] ?? missing_field("email");
if (empty($email) || strlen($email) > 128 || !str_contains($email, "@"))
    missing_field("email");

$subject = $_POST["subject"] ?? missing_field("subject");
if (empty($email) || strlen($subject) > 256)
    missing_field("subject");

$message = $_POST["message"] ?? missing_field("message");
if (empty($email) || strlen($message) > 16384)
    missing_field("message");

$captcha = $_POST["captcha"] ?? missing_field("captcha");
if (empty($email) || strlen($captcha) > 7)
    missing_field("captcha");

$expected_captcha = $_SESSION["captcha_phrase"] ?? null;

if ($captcha !== $expected_captcha) {
    http_response_code(400);
    echo "Ung&uuml;ltiges CAPTCHA!";
    exit;
}

require_once __DIR__ . "/../vendor/autoload.php";

$mailer_dsn = getenv_or_fail("MAILER_DSN");

$transport = Transport::fromDsn($mailer_dsn);

$transport->send((new Email())
    ->from(new Address("mystic-contactform+no-reply@jonaskohl.de", $name))
    ->to("mystic@jonaskohl.de")
    ->replyTo(new Address($email, $name))
    ->subject("[Mystic House Corner] $subject")
    ->text($message)
);

header("Location: /pages/sent.html");