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_Messageobject which will be used to send the email. -
$attachment1 = Swift_Attachment::fromPath('/path/to/file1.pdf');- This creates a newSwift_Attachmentobject 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 use SMTP with Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to set timeout with Swiftmailer?
- How to disable SSL verification in Swiftmailer?
- How to send emails to multiple recipients with Swiftmailer?
- How to configure Swiftmailer for SMTP without authentication?
- How to set the reply to address in Swiftmailer?
- How to get the message ID in SwiftMailer?
- How to use Swiftmailer with SendGrid?
See more codes...