summaryrefslogtreecommitdiff
path: root/http/public/do/send_email.php
blob: 51d8b1952e66f706603b44bb6c919717b53684fe (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

use JoeacNet\Http\Config;
use JoeacNet\Http\Db;
use JoeacNet\Http\Mail;

require_once __DIR__ . "/../../vendor/autoload.php";
require_once __DIR__ . "/../../php/config.php";
require_once __DIR__ . "/../../php/db.php";
require_once __DIR__ . "/../../php/mail.php";

$db = Db\connectDb();

$payload = json_decode(file_get_contents("php://input"), true);
$name = getNameFromPayloadAndValidate($payload);
$email = getEmailFromPayloadAndValidate($payload);
$message = getMessageFromPayloadAndValidate($payload);
$token = getTokenFromPayloadAndValidate($payload);
$userId = getUserIdFromPayloadAndValidate($payload);

limitMaxDailyEmails($db);
verifyToken(db: $db, userId: $userId, token: $token);
Db\deleteAllTokensForUser($db, $userId);

$messageId = Mail\sendEmail(
  subject: "joeac.net: $name left a message",
  replyToEmail: $email,
  replyToName: $name,
  toEmail: Config\contactMailbox(),
  toName: Config\contactMailboxName(),
  body: "$name <$email> sent you a message:\n\n\n$message",
);
Db\recordSentEmail($db, $messageId);
syslog(LOG_INFO, "Sent an email to Joe. Message ID: $messageId");

http_response_code(200);
echo $messageId;
exit();

/// functions ///

function verifyToken(PDO $db, string $userId, string $token)
{
  if (!Db\isTokenValid($db, $userId, $token)) {
    http_response_code(400);
    echo "token is not valid";
    exit();
  }
}

function limitMaxDailyEmails(PDO $db)
{
  $countEmailsSentLast24Hours = Db\countEmailsSentLast24Hours($db);
  if ($countEmailsSentLast24Hours > Config\maxDailyEmails()) {
    $msg =
      "$countEmailsSentLast24Hours emails have been sent in the last 24 hours, but the max daily load is " .
      Config\maxDailyEmails() .
      ".";
    syslog(LOG_WARNING, $msg);
    http_response_code(500);
    echo $msg;
    exit();
  }
}

function getNameFromPayloadAndValidate(array $payload): string
{
  $name = (string) $payload["name"];
  if (is_null($name) || $name == "") {
    http_response_code(400);
    echo "name must not be empty";
    exit();
  }
  return $name;
}

function getEmailFromPayloadAndValidate(array $payload): string
{
  $email = (string) $payload["email"];
  if (is_null($email) || $email == "") {
    http_response_code(400);
    echo "email must not be empty";
    exit();
  }
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    http_response_code(400);
    echo "<$email> is not a valid email address.";
    exit();
  }
  return $email;
}

function getMessageFromPayloadAndValidate(array $payload): string
{
  $message = (string) $payload["message"];
  if (is_null($message) || $message == "") {
    http_response_code(400);
    echo "message must not be empty";
    exit();
  }
  return $message;
}

function getTokenFromPayloadAndValidate(array $payload): string
{
  $token = (string) $payload["token"];
  if (is_null($token) || $token == "") {
    http_response_code(400);
    echo "token must not be empty";
    exit();
  }
  return $token;
}

function getUserIdFromPayloadAndValidate(array $payload): string
{
  $userId = (string) $payload["userId"];
  if (is_null($userId) || $userId == "") {
    http_response_code(400);
    echo "userId must not be empty";
    exit();
  }
  return $userId;
}