Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ptasker
Last active August 4, 2017 08:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ptasker/7680134 to your computer and use it in GitHub Desktop.
Save ptasker/7680134 to your computer and use it in GitHub Desktop.
Swiftmailer create attachment
<?php
//Using composer to get the Sabre lib
require dirname ( __FILE__ ) . '/../vendor/autoload.php';
use Sabre\VObject\Component\VCalendar;
// Create a message
$message = Swift_Message::newInstance ( 'Message Title' )
->setFrom ( array( 'address@domain.com ' => 'John Doe' ) )
->setTo ( array( $email ) )
->setSubject ( "Cool message subject" )
->setBody ( $body )
->setContentType ( 'text/html' );
//Set the 'from' to something a bit nicer
$from = $message->getHeaders ()->get ( 'From' );
$from->setNameAddresses ( array(
'address@domain.com ' => 'John Doe',
) );
$headers = $message->getHeaders ();
//Add text header for Mailgun campaign tracking
$headers->addTextHeader ( 'X-Mailgun-Campaign-Id', '[mailgun-campaign-id]]' );
//Create calendar invite with Sabre VOject lib
$cal = new VCalendar();
//Create a meeting invite that lasts 2 hours on the same day
$vevent = $cal->add ( 'VEVENT', array(
'summary' => $subject,
'location' => 'Meeting room 1',
'dtstart' => new DateTime( $mtg_date . ' 09:00:00', new \DateTimeZone( 'America/Toronto' ) ),
'dtend' => new DateTime( $mtg_date . ' 11:00:00', new \DateTimeZone( 'America/Toronto' ) ),
'method' => 'REQUEST'
) );
//Make ical
$data = $cal->serialize ();
$filename = "invite.ics";
//Attach the calendar invite to the mail
$attachment = Swift_Attachment::newInstance ()
->setFilename ( $filename )
->setContentType ( 'text/calendar' )
->setBody ( $data );
$message->attach ( $attachment );
$result = $mailer->send ( $message );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment