Fix SMTP authentication and mail body. Notify if there was a problem sending the email.

This commit is contained in:
Brian Lycett 2020-11-30 16:14:53 +00:00
parent 8dacee9c4e
commit 07cfb50e16
5 changed files with 37 additions and 15 deletions

View File

@ -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. * `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 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. 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. 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 Username format
--- ---

View File

@ -76,10 +76,7 @@ if (isset($_POST['create_account'])) {
if (isset($send_user_email) and $send_user_email == TRUE) { if (isset($send_user_email) and $send_user_email == TRUE) {
if ($NO_HTTPS == TRUE) { $protocol = 'http://'; } else { $protocol = 'https://'; } $mail_subject = "Your $ORGANISATION_NAME account has been created.";
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_body = <<<EoT $mail_body = <<<EoT
You've been set up with an account for $ORGANISATION_NAME. Your credentials are: You've been set up with an account for $ORGANISATION_NAME. Your credentials are:
@ -87,13 +84,19 @@ You've been set up with an account for $ORGANISATION_NAME. Your credentials are
Username: $username Username: $username
Password: $password Password: $password
You should change your password as soon as possible. Log into the account manager at ${protocol}${SITE_URL}/log_in using your credentials. You should change your password as soon as possible. Log into the account manager at ${SITE_PROTOCOL}${SERVER_HOSTNAME}/log_in using your credentials.
Once logged in you can change your password at ${protocol}${SITE_URL}/change_password/ Once logged in you can change your password at ${SITE_PROTOCOL}${SERVER_HOSTNAME}/change_password/
EoT; EoT;
include_once "mail_functions.inc.php"; include_once "mail_functions.inc.php";
send_email($email,"$first_name $last_name",$mail_subject,$mail_body); $sent_email = send_email($email,"$first_name $last_name",$mail_subject,$mail_body);
$creation_message = "The account was created and an email sent to $email."; $creation_message = "The account was created";
if ($sent_email) {
$creation_message .= " and an email sent to $email.";
}
else {
$creation_message .= " but unfortunately the email wasn't sent.<br>More information will be available in the logs.";
}
} }
if ($admin_setup == TRUE) { if ($admin_setup == TRUE) {

View File

@ -37,6 +37,7 @@
$ORGANISATION_NAME = (getenv('ORGANISATION_NAME') ? getenv('ORGANISATION_NAME') : 'LDAP'); $ORGANISATION_NAME = (getenv('ORGANISATION_NAME') ? getenv('ORGANISATION_NAME') : 'LDAP');
$SITE_NAME = (getenv('SITE_NAME') ? getenv('SITE_NAME') : "$ORGANISATION_NAME user manager"); $SITE_NAME = (getenv('SITE_NAME') ? getenv('SITE_NAME') : "$ORGANISATION_NAME user manager");
$SERVER_HOSTNAME = (getenv('SERVER_HOSTNAME') ? getenv('SERVER_HOSTNAME') : "ldapusermanager.org");
$USERNAME_FORMAT = (getenv('USERNAME_FORMAT') ? getenv('USERNAME_FORMAT') : '{first_name}-{last_name}'); $USERNAME_FORMAT = (getenv('USERNAME_FORMAT') ? getenv('USERNAME_FORMAT') : '{first_name}-{last_name}');
$USERNAME_REGEX = (getenv('USERNAME_REGEX') ? getenv('USERNAME_REGEX') : '^[a-z][a-zA-Z0-9\._-]{3,32}$'); $USERNAME_REGEX = (getenv('USERNAME_REGEX') ? getenv('USERNAME_REGEX') : '^[a-z][a-zA-Z0-9\._-]{3,32}$');
@ -66,7 +67,7 @@
$SMTP['tls'] = ((strcasecmp(getenv('SMTP_USE_TLS'),'TRUE') == 0) ? TRUE : FALSE); $SMTP['tls'] = ((strcasecmp(getenv('SMTP_USE_TLS'),'TRUE') == 0) ? TRUE : FALSE);
$SMTP['debug_level'] = getenv('SMTP_LOG_LEVEL'); $SMTP['debug_level'] = getenv('SMTP_LOG_LEVEL');
if (!is_numeric($SMTP['debug_level']) or $SMTP['debug_level'] >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); $EMAIL_DOMAIN = (getenv('EMAIL_DOMAIN') ? getenv('EMAIL_DOMAIN') : Null);

View File

@ -6,7 +6,7 @@ require_once "/opt/PHPMailer/src/Exception.php";
function send_email($recipient_email,$recipient_name,$subject,$body) { 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 = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP(); $mail->isSMTP();
@ -17,7 +17,7 @@ function send_email($recipient_email,$recipient_name,$subject,$body) {
$mail->Host = $SMTP['host']; $mail->Host = $SMTP['host'];
$mail->Port = $SMTP['port']; $mail->Port = $SMTP['port'];
if (isset($MAIL['username'])) { if (isset($SMTP['user'])) {
$mail->SMTPAuth = true; $mail->SMTPAuth = true;
$mail->Username = $SMTP['user']; $mail->Username = $SMTP['user'];
$mail->Password = $SMTP['pass']; $mail->Password = $SMTP['pass'];
@ -29,7 +29,15 @@ function send_email($recipient_email,$recipient_name,$subject,$body) {
$mail->addAddress($recipient_email, $recipient_name); $mail->addAddress($recipient_email, $recipient_name);
$mail->Subject = $subject; $mail->Subject = $subject;
$mail->Body = $body; $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;
}
} }

View File

@ -17,6 +17,16 @@ $GOOD_ICON = "&#9745;";
$WARN_ICON = "&#9888;"; $WARN_ICON = "&#9888;";
$FAIL_ICON = "&#9940;"; $FAIL_ICON = "&#9940;";
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 ("modules.inc.php"); # module definitions
include ("config.inc.php"); # get local settings include ("config.inc.php"); # get local settings