php-swiftmailerHow to send multiple attachments with SwiftMailer?
SwiftMailer can be used to send multiple attachments with a single email. To do this, you need to create a Swift_Attachment
object for each attachment and add it to the Swift_Message
object.
Example code
<?php
// Create the message
$message = new Swift_Message();
// Create the attachment
$attachment1 = Swift_Attachment::fromPath('/path/to/file1.pdf');
$attachment2 = Swift_Attachment::fromPath('/path/to/file2.pdf');
// Add the attachments to the message
$message->attach($attachment1);
$message->attach($attachment2);
// Send the message
$mailer->send($message);
Output example
Message sent successfully
Code explanation
-
$message = new Swift_Message();
- This creates a newSwift_Message
object which will be used to send the email. -
$attachment1 = Swift_Attachment::fromPath('/path/to/file1.pdf');
- This creates a newSwift_Attachment
object from the specified file path. -
$message->attach($attachment1);
- This adds the attachment to the message. -
$mailer->send($message);
- This sends the message with the attachments.
Helpful links
More of Php Swiftmailer
- How to set timeout with Swiftmailer?
- How to use SMTP with Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to get the response code when using Swiftmailer?
- How to use Swiftmailer to send RFC 2822 compliant emails?
- How to configure Swiftmailer for Postfix?
- How to send emails in UTF8 using Swiftmailer?
- How to send emails to multiple recipients with Swiftmailer?
- How to set the port for Swiftmailer?
- How to enable TLS with Swiftmailer?
See more codes...