-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_mail.php
More file actions
executable file
·46 lines (38 loc) · 1.49 KB
/
Copy pathlib_mail.php
File metadata and controls
executable file
·46 lines (38 loc) · 1.49 KB
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
<?php
// lib_mail.php — Mailtrap SMTP via PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once __DIR__ . '/vendor/autoload.php';
function send_mail(string $to, string $subject, string $html): bool
{
$mail = new PHPMailer(true);
try {
// Mailtrap SMTP
$mail->isSMTP();
$mail->Host = defined('MAIL_HOST') ? MAIL_HOST : 'sandbox.smtp.mailtrap.io';
$mail->SMTPAuth = true;
$mail->Username = defined('MAIL_USER') ? MAIL_USER : '';
$mail->Password = defined('MAIL_PASS') ? MAIL_PASS : '';
$mail->Port = defined('MAIL_PORT') ? MAIL_PORT : 2525;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->CharSet = 'UTF-8';
// Sender
$from_addr = defined('MAIL_FROM_ADDR') ? MAIL_FROM_ADDR : 'no-reply@yourhotel.test';
$from_name = defined('MAIL_FROM_NAME') ? MAIL_FROM_NAME : 'The Riverside Reservations';
$mail->setFrom($from_addr, $from_name);
// Recipient (Mailtrap will catch everything in the inbox)
$mail->addAddress($to);
// Content
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $html;
$mail->AltBody = strip_tags(
preg_replace('/<br\s*\/?>/i', "\n", preg_replace('/<\/p>/i', "\n\n", $html))
);
$mail->send();
return true;
} catch (Exception $e) {
error_log("Mailer Error: {$mail->ErrorInfo}");
return false;
}
}