enabling email for registration.

main
Steffen Pohle 2 weeks ago
parent 116373cb36
commit 7a40c6f603

@ -388,6 +388,24 @@ function generateRegistrationCode() {
return str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT); return str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
} }
function sendRegistrationVerificationEmail($email, $code) {
$email = trim((string) $email);
if ($email === '') {
return false;
}
$subject = 'Your KaI registration verification code';
$message = "Hello,\n\n"
. "You requested to create a KaI account.\n"
. "Your verification code is: {$code}\n\n"
. "Please enter this code on the registration page to complete your account setup.";
$headers = "From: " . MAIL_FROM_NAME . " <" . MAIL_FROM . ">\r\n"
. "Reply-To: " . MAIL_FROM . "\r\n"
. "X-Mailer: PHP/" . phpversion();
return mail($email, $subject, $message, $headers);
}
function savePendingRegistration($username, $email, $password, $code) { function savePendingRegistration($username, $email, $password, $code) {
if (session_status() === PHP_SESSION_NONE) { if (session_status() === PHP_SESSION_NONE) {
session_start(); session_start();

@ -52,11 +52,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$errors[] = 'That username or email is already in use.'; $errors[] = 'That username or email is already in use.';
} else { } else {
$code = generateRegistrationCode(); $code = generateRegistrationCode();
if (savePendingRegistration($username, $email, $password, $code)) { if (savePendingRegistration($username, $email, $password, $code) && sendRegistrationVerificationEmail($email, $code)) {
$message = 'A verification code has been sent to ' . htmlspecialchars($email) . '. Please enter it below.'; $message = 'A verification code has been sent to ' . htmlspecialchars($email) . '. Please enter it below.';
$message .= ' (For demo purposes, the code is: ' . htmlspecialchars($code) . ')';
} else { } else {
$errors[] = 'Unable to start registration.'; $errors[] = 'Unable to start registration. Please check that your email address is valid and the mail server is configured.';
} }
} }
} }

Loading…
Cancel
Save