How can using a form mailer like PHPMailer improve the functionality of a contact form compared to the current setup?
Currently, the contact form may be limited in functionality and security, as it may not properly sanitize inputs or handle email sending efficiently. By using a form mailer like PHPMailer, we can improve the functionality of the contact form by ensuring proper input validation, sanitization, and secure email sending capabilities.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
// Initialize PHPMailer
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@example.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('recipient@example.com', 'Recipient');
// Content
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Keywords
Related Questions
- Is there a way to enable full boolean evaluation in PHP, similar to other languages with compiler switches?
- What are the advantages and disadvantages of using Subversion versus Git for version control in a PHP project?
- What are some best practices for handling button actions in PHP to ensure smooth functionality?