What are some recommended PHP libraries for sending emails from a contact form?
Sending emails from a contact form in PHP can be achieved using various libraries that simplify the process of sending emails. Some recommended PHP libraries for sending emails from a contact form include PHPMailer, Swift Mailer, and Zend Mail. These libraries provide easy-to-use functions for sending emails with attachments, HTML content, and handling SMTP authentication.
// Using PHPMailer library to send emails from a contact form
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 email subject and body
$mail->Subject = 'Subject of the email';
$mail->Body = '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';
}
Keywords
Related Questions
- What are the advantages of using arrays when processing file data in PHP?
- How can PHP developers avoid the error message "supplied argument is not a valid MySQL result resource" when using mysql_fetch_array()?
- How can the choice of text editor impact the handling of character encoding in PHP scripts and data files?