isSMTP();
$mail->Host = getenv('SMTP_HOST');
$mail->SMTPAuth = true;
$mail->Port = (int) getenv('SMTP_PORT');
$mail->Username = getenv('SMTP_USERNAME');
$mail->Password = getenv('SMTP_PASSWORD');
$mail->SMTPOptions = [
'ssl' => [
'cafile' => '/etc/ssl/certs/ca-certificates.crt',
'verify_peer' => true,
'verify_peer_name' => true,
],
];
$mail->setFrom(getenv('SMTP_FROM_EMAIL'), getenv('SMTP_FROM_NAME'));
$mail->isHTML(true);
return $mail;
}
function sendRegistrationConfirmationEmail(string $toEmail, string $username): bool
{
try {
$mail = configureAppMailer();
$mail->addAddress($toEmail, $username);
$mail->Subject = 'Welcome to OurApp!';
$safeUsername = htmlspecialchars($username, ENT_QUOTES, 'UTF-8');
$mail->Body = '
Welcome, ' . $safeUsername . '! 🐾
Your OurApp account has been created. You are ready to start creating unique, AI-generated critters — pick an adjective, animal, action, and location, and watch your imagination come to life.
Start Creating
If you didn’t create this account, you can safely ignore this email.
';
$mail->AltBody = "Welcome, {$username}!\n\nYour OurApp account has been created. Start creating unique, AI-generated critters at https://ourapp.local/\n\nIf you didn't create this account, you can safely ignore this email.";
$mail->send();
return true;
} catch (PHPMailerException $e) {
error_log('Registration confirmation email failed: ' . (isset($mail) ? $mail->ErrorInfo : $e->getMessage()));
return false;
}
}
function sendPasswordResetEmail(string $toEmail, string $username, string $resetLink): bool
{
try {
$mail = configureAppMailer();
$mail->addAddress($toEmail, $username);
$mail->Subject = 'Reset your OurApp password';
$safeUsername = htmlspecialchars($username, ENT_QUOTES, 'UTF-8');
$safeResetLink = htmlspecialchars($resetLink, ENT_QUOTES, 'UTF-8');
$mail->Body = '
Reset your password
Hi ' . $safeUsername . ', we received a request to reset your OurApp password. Click the button below to choose a new one. This link expires in 20 minutes.
Reset Password
If you didn’t request a password reset, you can safely ignore this email — your password will not be changed.
';
$mail->AltBody = "Hi {$username},\n\nWe received a request to reset your OurApp password. Visit the link below to choose a new one. This link expires in 20 minutes.\n\n{$resetLink}\n\nIf you didn't request a password reset, you can safely ignore this email.";
$mail->send();
return true;
} catch (PHPMailerException $e) {
error_log('Password reset email failed: ' . (isset($mail) ? $mail->ErrorInfo : $e->getMessage()));
return false;
}
}