From 07cfb50e1634513ae8c53aa52d9eb7af34a2dd3e Mon Sep 17 00:00:00 2001 From: Brian Lycett Date: Mon, 30 Nov 2020 16:14:53 +0000 Subject: [PATCH] Fix SMTP authentication and mail body. Notify if there was a problem sending the email. --- README.md | 4 ++-- www/account_manager/new_user.php | 19 +++++++++++-------- www/includes/config.inc.php | 3 ++- www/includes/mail_functions.inc.php | 16 ++++++++++++---- www/includes/web_functions.inc.php | 10 ++++++++++ 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2bbab30..121b533 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,7 @@ Optional: * `SESSION_DEBUG` (default: *FALSE*): Set to TRUE to increase the logging level for sessions and user authorisation. This will output cookie passkeys to the error log - don't enable this in a production environment. -* `SMTP_LOG_LEVEL` (default: *1*): Set to between 1-4 to get SMTP logging information (0 disables SMTP logs). See https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging for details of the levels. +* `SMTP_LOG_LEVEL` (default: *0*): Set to between 1-4 to get SMTP logging information (0 disables SMTP debugging logs though it will still display errors). See https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging for details of the levels. Webserver SSL setup --- @@ -213,7 +213,7 @@ When you create an account you'll have an option to send an email to the person Emails are sent via SMTP, so you'll need to be able to connect to an SMTP server and pass in the settings for that server via environmental variables - see **Email settings** above. If you haven't passed in those settings or if the account you've created has no (valid) email address then the option to send an email will be disabled. -Note that failures to deliver are silent. If people aren't receiving the emails then check the logs to see why not. You can increase the log level (`SMTP_LOG_LEVEL`) for more detailed logs. +When the account is created you'll be told if the email was sent or not but be aware that just because your SMTP server accepted the email it doesn't mean that it was able to deliver it. If you get a message saying the email wasn't sent then check the logs for the error. You can increase the log level (`SMTP_LOG_LEVEL`) to above 0 in order to see SMTP debug logs. Username format --- diff --git a/www/account_manager/new_user.php b/www/account_manager/new_user.php index d48df85..8b40a07 100644 --- a/www/account_manager/new_user.php +++ b/www/account_manager/new_user.php @@ -76,10 +76,7 @@ if (isset($_POST['create_account'])) { if (isset($send_user_email) and $send_user_email == TRUE) { - if ($NO_HTTPS == TRUE) { $protocol = 'http://'; } else { $protocol = 'https://'; } - - if (in_array(strtolower($ORGANISATION_NAME[0]),array('a','e','i','o','u'))) { $org_prefix = "An "; } else { $org_prefix = "A "; } - $mail_subject = "$org_prefix $ORGANISATION_NAME account has been created for you."; + $mail_subject = "Your $ORGANISATION_NAME account has been created."; $mail_body = <<4 or $SMTP['debug_level'] <0) { $SMTP['debug_level'] = 1; } + if (!is_numeric($SMTP['debug_level']) or $SMTP['debug_level'] >4 or $SMTP['debug_level'] <0) { $SMTP['debug_level'] = 0; } $EMAIL_DOMAIN = (getenv('EMAIL_DOMAIN') ? getenv('EMAIL_DOMAIN') : Null); diff --git a/www/includes/mail_functions.inc.php b/www/includes/mail_functions.inc.php index 3a30788..ac74561 100644 --- a/www/includes/mail_functions.inc.php +++ b/www/includes/mail_functions.inc.php @@ -6,7 +6,7 @@ require_once "/opt/PHPMailer/src/Exception.php"; function send_email($recipient_email,$recipient_name,$subject,$body) { - global $EMAIL, $SMTP, $SITE_URL, $log_prefix; + global $EMAIL, $SMTP, $log_prefix; $mail = new PHPMailer\PHPMailer\PHPMailer(); $mail->isSMTP(); @@ -16,8 +16,8 @@ function send_email($recipient_email,$recipient_name,$subject,$body) { $mail->Host = $SMTP['host']; $mail->Port = $SMTP['port']; - - if (isset($MAIL['username'])) { + + if (isset($SMTP['user'])) { $mail->SMTPAuth = true; $mail->Username = $SMTP['user']; $mail->Password = $SMTP['pass']; @@ -29,7 +29,15 @@ function send_email($recipient_email,$recipient_name,$subject,$body) { $mail->addAddress($recipient_email, $recipient_name); $mail->Subject = $subject; $mail->Body = $body; - $mail->send(); + + if (!$mail->Send()) { + error_log("$log_prefix SMTP: Unable to send email: " . $mail->ErrorInfo); + return FALSE; + } + else { + error_log("$log_prefix New user: sent a new account email to $recipient_email ($recipient_name)"); + return TRUE; + } } diff --git a/www/includes/web_functions.inc.php b/www/includes/web_functions.inc.php index 0bc0771..d859cf8 100644 --- a/www/includes/web_functions.inc.php +++ b/www/includes/web_functions.inc.php @@ -17,6 +17,16 @@ $GOOD_ICON = "☑"; $WARN_ICON = "⚠"; $FAIL_ICON = "⛔"; +if (isset($_SERVER['HTTPS']) and + ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) or + isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and + $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { + $SITE_PROTOCOL = 'https://'; +} +else { + $SITE_PROTOCOL = 'http://'; +} + include ("modules.inc.php"); # module definitions include ("config.inc.php"); # get local settings