Are there recommended PHP libraries or classes for sending emails from a form?
To send emails from a form in PHP, you can use the built-in PHP `mail()` function or you can use a more robust library like PHPMailer. PHPMailer provides a more flexible and secure way to send emails with attachments, HTML content, and SMTP authentication.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
// Create a new PHPMailer instance
$mail = new PHPMailer();
// Set up the SMTP settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
// Set the sender and recipient
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('to@example.com', 'Recipient Name');
// Set the email subject and body
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email from PHPMailer';
// Send the email
if($mail->send()) {
echo 'Email sent successfully';
} else {
echo 'Email could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>