What is the correct way to send multiple attachments in PHP?
When sending multiple attachments in PHP, you can use the PHPMailer library to easily attach multiple files to an email. You can create an instance of PHPMailer, add each attachment using the `addAttachment()` method, and then send the email using the `send()` method.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php'; // Include PHPMailer autoload file
// Create a new PHPMailer instance
$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', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
// Attach multiple files
$mail->addAttachment('path/to/file1.pdf');
$mail->addAttachment('path/to/file2.jpg');
// Content
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = 'Email body content';
// Send email
$mail->send();
echo 'Email sent successfully';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;
}
Keywords
Related Questions
- How can PHP developers efficiently handle multiple links from a database and store them in an array for display on a webpage?
- How can the PHP documentation be utilized effectively to understand and implement trigonometric functions accurately?
- How can regular expressions be used in PHP to address the issue of removing repeated occurrences of a character in a string?