How can PHPMailer be utilized for handling email submissions in PHP, particularly in the context of form submissions?
When handling email submissions in PHP, PHPMailer can be utilized to send emails securely and reliably. To integrate PHPMailer with form submissions, you need to include the PHPMailer library in your project and configure it to send email notifications when a form is submitted.
<?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 = 'your_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Recipient
$mail->setFrom('your@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
// Email content
$mail->isHTML(true);
$mail->Subject = 'Subject of the Email';
$mail->Body = 'Body of the Email';
// Send email
$mail->send();
echo 'Email sent successfully';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
Related Questions
- Is it advisable to start a session for every user in PHP, or are there specific use cases where this approach should be avoided?
- What are some alternative methods, besides pathinfo(), that can be used to determine the current path of a website in PHP?
- How can the use of single quotes versus double quotes impact the return values in PHP functions like the one discussed in the forum thread?