[PHP] Add an attachment on the fly to a Swift email
Published on 2018-10-16 • Modified on 2018-10-16
In this snippet, we create a mail attachment with the help of the Swift_Attachment
object then we "attach" it to the Swift_Message
instance.
// Create attachment on the fly
$swiftAttachment = new \Swift_Attachment('ics data', 'mycalendar.ics', 'text/calendar');
$message = (new \Swift_Message())
->setSubject('Subject')
->setFrom('from@email.com')
->setTo('to@email.com')
->setCc('cc@email.com')
->setBcc('bcc@email.com')
->setReplyTo('replyto@email.com')
->setBody($body, 'text/html');
// Attach the file to the email message
$message->attach($swiftAttachment);
More on Stackoverflow Random snippet
Call to action
Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and repost!
- Follow me on Bluesky 🦋
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win "Announcer" badges 🏅.
Thank you for reading! And see you soon on Strangebuzz! 😉
