[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())
    ->setBody($body, 'text/html');

// Attach the file to the email message

 More on Stackoverflow  Random snippet