Are there specific PHP functions or libraries that can simplify the process of sending email notifications from a contact form?
Sending email notifications from a contact form in PHP can be simplified by using the built-in `mail()` function or by utilizing libraries like PHPMailer or Swift Mailer. These libraries provide a more robust and flexible way to send emails with attachments, HTML content, and SMTP authentication.
// Using PHPMailer library to send email notifications
require 'vendor/autoload.php'; // Include PHPMailer library
// Create a new PHPMailer instance
$mail = new PHPMailer\PHPMailer\PHPMailer();
// Set up SMTP configuration
$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 email content
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('to@example.com', 'Recipient Name');
$mail->Subject = 'Subject of the email';
$mail->Body = 'This is the body of the email';
// Send the email
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}