-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpmailer_setup.php
More file actions
67 lines (62 loc) · 2.52 KB
/
phpmailer_setup.php
File metadata and controls
67 lines (62 loc) · 2.52 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
} else {
// Fallback: PHPMailer not installed, define a dummy sendEmail function
if (!function_exists('sendEmail')) {
function sendEmail($to, $subject, $body, $replyTo = null) {
error_log("PHPMailer not installed. Email to $to not sent.");
return false;
}
}
}
if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
// PHPMailer classes not loaded, define dummy sendEmail function
if (!function_exists('sendEmail')) {
function sendEmail($to, $subject, $body, $replyTo = null) {
error_log("PHPMailer classes not loaded. Email to $to not sent.");
return false;
}
}
} else {
// Import classes inside function to avoid syntax error
if (!function_exists('sendEmail')) {
function sendEmail($to, $subject, $body, $replyTo = null) {
$mail = new \PHPMailer\PHPMailer\PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Gmail SMTP server
$mail->SMTPAuth = true;
$mail->Username = 'booker.library.staff@gmail.com'; // Your Gmail address
$mail->Password = 'jbai gevm jtgl ucvx'; // Your Gmail app password
$mail->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Disable SSL certificate verification (for local development)
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
//Recipients
$mail->setFrom('your-email@gmail.com', 'Booker Library'); // Use your Gmail address
$mail->addAddress($to);
if ($replyTo) {
$mail->addReplyTo($replyTo);
}
// Content
$mail->isHTML(false);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->send();
return true;
} catch (\PHPMailer\PHPMailer\Exception $e) {
error_log("Message could not be sent. Mailer Error: {$mail->ErrorInfo}");
return $mail->ErrorInfo;
}
}
}
}
?>