aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/Mail.php
blob: 650c5b36ae6f4ace4025dbde47cb1606f97d0c34 (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
<?php

declare(strict_types=1);
// Sends emails using PHP's built-in mail() function.
// In Docker, msmtp is configured as the sendmail transport.

namespace App;

class Mail
{
    public static function send(string $to, string $subject, string $body): bool
    {
        $from = getenv('MAIL_FROM') ?: 'noreply@camagru.local';
        $headers = "From: $from\r\n";
        $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

        return mail($to, $subject, $body, $headers);
    }

    public static function sendVerification(string $to, string $token): bool
    {
        $url = getenv('APP_URL') . '/verify?token=' . urlencode($token);
        $subject = 'Camagru — Verify your email';
        $body = '<p>Click the link below to verify your email address:</p>'
              . '<p><a href="' . htmlspecialchars($url) . '">' . htmlspecialchars($url) . '</a></p>'
              . '<p>If you did not create an account, ignore this email.</p>';

        return self::send($to, $subject, $body);
    }

    public static function sendCommentNotification(string $to, string $commenterUsername, int $postId): bool
    {
        $url = getenv('APP_URL') . '/gallery#post-' . $postId;
        $subject = 'Camagru — New comment on your post';
        $body = '<p><strong>' . htmlspecialchars($commenterUsername) . '</strong> commented on your post.</p>'
              . '<p><a href="' . htmlspecialchars($url) . '">View the comment</a></p>';

        return self::send($to, $subject, $body);
    }

    public static function sendPasswordReset(string $to, string $token): bool
    {
        $url = getenv('APP_URL') . '/reset-password?token=' . urlencode($token);
        $subject = 'Camagru — Reset your password';
        $body = '<p>Click the link below to reset your password:</p>'
              . '<p><a href="' . htmlspecialchars($url) . '">' . htmlspecialchars($url) . '</a></p>'
              . '<p>This link expires in 1 hour.</p>'
              . '<p>If you did not request a password reset, ignore this email.</p>';

        return self::send($to, $subject, $body);
    }
}