What are the potential security implications of passing username and password directly in PHP code for SMTP authentication with PHPMailer?
Storing usernames and passwords directly in PHP code for SMTP authentication with PHPMailer can pose a security risk as the credentials can be easily exposed if the code is accessed by unauthorized users. To mitigate this risk, it is recommended to store sensitive information like usernames and passwords in environment variables or configuration files outside of the codebase.
// Load sensitive information from environment variables
$smtpUsername = getenv('SMTP_USERNAME');
$smtpPassword = getenv('SMTP_PASSWORD');
// Initialize PHPMailer with SMTP authentication
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;