forked from fsi-tue/eei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.php
77 lines (66 loc) · 2.33 KB
/
email.php
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
<?php
require __DIR__ . '/phpmailer/src/Exception.php';
require __DIR__ . '/phpmailer/src/PHPMailer.php';
require __DIR__ . '/phpmailer/src/SMTP.php';
// Loads the environment variables from the .env file
loadEnv('.env');
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
/**
* Sends an email to a given address.
*
* @param string $recipient
* @param string $subject
* @param string $body
* @param string $attachment
* @param string $attachment_name
* @return bool
*/
function sendMailViaPHPMailer(string $recipient, string $subject, string $body, string $attachment = '', string $attachment_name = ''): bool
{
$mail = new PHPMailer(TRUE);
try {
$mail->isSMTP();
/* https://stackoverflow.com/questions/2491475/phpmailer-character-encoding-issues */
$mail->Encoding = 'base64';
$mail->CharSet = 'UTF-8';
if (isLocalhost()) {
// If the server is localhost, use SMTP authentication
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPOptions = [
'ssl' => [
'verify_peer' => FALSE,
'verify_peer_name' => FALSE,
'allow_self_signed' => TRUE
]
];
$mail->Password = getEnvVar('SENDER_PASSWORD');
$mail->Username = getEnvVar('SENDER_USERNAME');
} else {
// Otherwise, use no authentication
$mail->SMTPAuth = FALSE;
$mail->SMTPSecure = FALSE;
$mail->SMTPAutoTLS = FALSE;
}
$mail->SMTPKeepAlive = TRUE;
$mail->Host = getEnvVar('EMAIL_HOST');
$mail->Port = getEnvVar('EMAIL_PORT');
$mail->setFrom(getEnvVar('SENDER_EMAIL'), getEnvVar('SENDER_NAME'));
$mail->addAddress($recipient);
$mail->isHTML();
$mail->Subject = $subject;
$mail->Body = $body;
// If an attachment is given, add it to the email.
if ($attachment !== '') {
$mail->addStringAttachment($attachment, $attachment_name);
}
$mail->send();
return TRUE;
} catch (Exception) {
return FALSE;
}
}