56 lines
2.1 KiB
PHP
56 lines
2.1 KiB
PHP
|
<?php
|
||
|
//Import PHPMailer classes into the global namespace
|
||
|
use PHPMailer\PHPMailer\PHPMailer;
|
||
|
use PHPMailer\PHPMailer\SMTP;
|
||
|
use PHPMailer\PHPMailer\Exception;
|
||
|
|
||
|
require_once("./assets/php/phpmailer/Exception.php");
|
||
|
require_once("./assets/php/phpmailer/PHPMailer.php");
|
||
|
require_once("./assets/php/phpmailer/SMTP.php");
|
||
|
|
||
|
function send_email($to, $subject, $body, $alt_body, $to_first_name, $to_last_name)
|
||
|
{
|
||
|
$config_array = json_decode(file_get_contents("config.json", true));
|
||
|
|
||
|
if (!isset($alt_body)) {
|
||
|
$alt_body = "";
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
//Create a new PHPMailer instance
|
||
|
$mail = new PHPMailer;
|
||
|
//Tell PHPMailer to use SMTP
|
||
|
$mail->isSMTP();
|
||
|
//Enable SMTP debugging
|
||
|
// SMTP::DEBUG_OFF = off (for production use)
|
||
|
// SMTP::DEBUG_CLIENT = client messages
|
||
|
// SMTP::DEBUG_SERVER = client and server messages
|
||
|
$mail->SMTPDebug = SMTP::DEBUG_OFF;
|
||
|
//Set the hostname of the mail server
|
||
|
$mail->Host = 'smtp.gmail.com';
|
||
|
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
|
||
|
$mail->Port = 587;
|
||
|
//Set the encryption mechanism to use - STARTTLS or SMTPS
|
||
|
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||
|
//Whether to use SMTP authentication
|
||
|
$mail->SMTPAuth = true;
|
||
|
//Username to use for SMTP authentication - use full email address for gmail
|
||
|
$mail->Username = $config_array->{"gmail_username"};
|
||
|
//Password to use for SMTP authentication
|
||
|
$mail->Password = $config_array->{"gmail_password"};
|
||
|
//Set who the message is to be sent from
|
||
|
$mail->setFrom($config_array->{"gmail_username"}, 'Food Inventory');
|
||
|
//Set an alternative reply-to address
|
||
|
$mail->addAddress($to, $to_first_name . " " . $to_last_name);
|
||
|
//Set the subject line
|
||
|
$mail->Subject = $subject;
|
||
|
$mail->Body = $body;
|
||
|
//Replace the plain text body with one created manually
|
||
|
$mail->AltBody = $alt_body;
|
||
|
//send the message, check for errors
|
||
|
return ($mail->send());
|
||
|
} catch (Exception $e) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|