Are there any recommended resources or libraries for sending emails in PHP?
Sending emails in PHP can be achieved using the built-in `mail()` function or by using third-party libraries such as PHPMailer or Swift Mailer. These libraries provide more advanced features and better support for sending emails securely. Using a library like PHPMailer or Swift Mailer can simplify the process of sending emails and help prevent common issues such as emails being marked as spam.
// Using PHPMailer library to send an email
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; // Include the PHPMailer autoloader
// Create a new PHPMailer instance
$mail = new PHPMailer();
// Set up the email details
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@example.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email sent using PHPMailer';
// Send the email
if ($mail->send()) {
echo 'Email sent successfully';
} else {
echo 'Error sending email: ' . $mail->ErrorInfo;
}