Are there best practices for handling SMTP with authentication in PHP on a Windows server?
When sending emails via SMTP with authentication in PHP on a Windows server, it is important to use a secure connection (TLS/SSL) and provide the correct credentials for authentication. It is recommended to use a PHP library like PHPMailer to handle SMTP with authentication securely and efficiently.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_smtp_username';
$mail->Password = 'your_smtp_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'Email body';
$mail->send();
echo 'Email sent successfully';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Keywords
Related Questions
- What are the best practices for organizing PHP code to handle navigation and content switching effectively?
- What are the potential pitfalls of using the deprecated mysql_* functions in PHP?
- Are there any best practices for handling long-running processes in PHP to prevent script timeouts or memory limit issues?