Last active
August 4, 2017 08:30
-
-
Save ptasker/7680134 to your computer and use it in GitHub Desktop.
Swiftmailer create attachment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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( '[email protected] ' => '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( | |
'[email protected] ' => '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