Skip to content

Commit

Permalink
Merge pull request #131 from multnomah-county-it/dev
Browse files Browse the repository at this point in the history
PHPMailer integration debugging
  • Loading branch information
john-c-houser authored Jan 31, 2024
2 parents ed51566 + f71dfdf commit 1fe675a
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 14 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"php": ">=5.6.0",
"curl/curl": ">=2.3",
"symfony/yaml": "~4||~5.4||~6",
"twig/twig": "^3.0"
"twig/twig": "^3.0",
"phpmailer/phpmailer": "^6.9"
},
"require-dev": {
"symfony/var-dumper": "^6.0"
Expand Down
83 changes: 82 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions libilsws.yaml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ ilsws:
timeout: 60
max_retries: 3
max_search_count: 20
# Configuration for PHPMailer
smtp:
smtp_host: 'smtp.host.domain'
smtp_port: 587
smtp_protocol: 'tls'
smtp_autotls: true
smtp_timeout: 30
smtp_username: USERNAME
smtp_password: PASSWORD
smtp_from: '[email protected]'
smtp_fromname: 'SENDER NAME'
smtp_replyto: '[email protected]'
smtp_allowhtml: true
# SirsiDynix Symphony parameters. For the most part, these must match values you have configured in
# your Symphony system.
symphony:
Expand All @@ -65,8 +78,6 @@ symphony:
13-120: '0_MULT'
# If you set this value, it will be used to set the days before expiration of online accounts
online_account_expiration: 90
# The from_email address will be as the from address for any emails sent by the code library
from_email: '[email protected]'
# Template path (www user must have read access)
# template_path: '~/web/themes/custom/multcolibtheme/templates/content'
template_path: '/Users/johnh3/Documents/php/ilsws/templates'
Expand Down
77 changes: 67 additions & 10 deletions src/Libilsws.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use DateTime;
use \Exception;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception as MailerException;

/**
* Custom API exception.
*
Expand Down Expand Up @@ -2300,6 +2304,7 @@ public function register_patron ($patron, $token = null, $addr_num = null, $temp

// Assign the patron_key from the initial registration to the update array
$patron_key = $response['key'];
$patron['barcode'] = $patron_key;

// Create a record structure with the update fields that aren't part of the initial registration
if ( ! $this->change_barcode($token, $patron_key, $patron_key) ) {
Expand All @@ -2316,7 +2321,7 @@ public function register_patron ($patron, $token = null, $addr_num = null, $temp

if ( $template && $this->validate('EMAIL', $patron['EMAIL'], 'e') ) {
$subject = 'Welcome to Multnomah County Library';
if ( ! $this->email_template($patron, $this->config['symphony']['from_email'], $patron['EMAIL'], $subject, $template) ) {
if ( ! $this->email_template($patron, $this->config['smtp']['smtp_from'], $patron['EMAIL'], $subject, $template) ) {
throw new Exception('Email to patron failed');
exit();
}
Expand Down Expand Up @@ -2619,21 +2624,73 @@ private function gen_temp_barcode ($last_name, $first_name, $street)
* @return string $message Result string
*/

public function email_template ($patron, $to, $from, $subject, $template)
public function email_template ($patron, $from, $to, $subject, $template)
{
print_r($patron);

$result = 0;

// Fill template
$loader = new \Twig\Loader\FilesystemLoader($this->config['symphony']['template_path']);
$twig = new \Twig\Environment($loader, ['cache' => $this->config['symphony']['template_cache']]);
$text = $twig->render($template, ['patron' => $patron]);
$body = chunk_split(base64_encode($text));
$body = $twig->render($template, ['patron' => $patron]);

// Initialize mailer
$mail = new PHPMailer(true);

try {
// Server settings
if ( $this->config['debug']['smtp'] ) {
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
}

$mail->isSMTP(); // Send using SMTP
$mail->CharSet = 'UTF-8'; // Use unicode
$mail->Encoding = 'base64'; // Encode test in base64

$mail->Host = $this->config['smtp']['smtp_host']; // Set the SMTP server to send through

// Headers
$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
$headers .= "From:" . $from . "\r\n"; // Sender Email
$headers = "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n\r\n";
if ( $this->config['smtp']['smtp_username'] && $this->config['smtp']['smtp_password'] ) {
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $this->config['smtp']['smtp_username']; // SMTP username
$mail->Password = $this->config['smtp']['smtp_password']; // SMTP password
} else {
$mail->SMTPAuth = false;
}

return mail($to, $subject, $body, $headers);
if ( $this->config['smtp']['smtp_protocol'] === 'tls' ) {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable implicit TLS encryption
}

// TCP port to connect to. Use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->Port = $this->config['smtp']['smtp_port'];

//Recipients
$mail->setFrom($from, $this->config['smtp']['smtp_fromname']);
$mail->addAddress($to); //Name is optional

// Reply-to
if ( $this->config['smtp']['smtp_replyto'] ) {
$mail->addReplyTo($this->config['smtp']['smtp_replyto']);
}

//Content
if ( $this->config['smtp']['smtp_allowhtml'] === 'true' ) {
$mail->isHTML(true); //Set email format to HTML
}

$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = 'Welcome to Multnomah County Library. Your barcode is ' . $patron['barcode'];

$mail->send();
$result = 1;

} catch (MailerException $e) {
error_log("Message could not be sent. Mailer Error: {$mail->ErrorInfo}");
}

return $result;
}

// End of class
Expand Down

0 comments on commit 1fe675a

Please sign in to comment.