通过Symfony2(Swift Mailer)生成并发送.ics文件

时间:2016-01-20 09:19:33

标签: php symfony icalendar swiftmailer

我正在尝试在Symfony2中生成日历事件MS Outlook / Google日历,而电子邮件是与.ics文件一起发送的,但我无法将该事件添加到日历中。当我尝试打开文件时,它说

Failed to import events: Unable to process your iCal/CSV file..

这就是我尝试生成iCal文件的方式

$message="
    BEGIN:VCALENDAR
    VERSION:2.0
    CALSCALE:GREGORIAN
    METHOD:REQUEST
    BEGIN:VEVENT
    DTSTART:".date('Ymd\THis', strtotime($meetingStartTime))."
    DTEND:".date('Ymd\THis', strtotime($meetingEndTime))."
    DTSTAMP:".date('Ymd\THis', strtotime($meetingStartTime))."
    ORGANIZER;CN=XYZ:mailto:do-not-reply@example.com
    UID:".rand(5, 1500)."
    ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP= TRUE;CN=Sample:emailaddress@testemail.com
    DESCRIPTION:".$this->getUser()->getName()." requested Phone/Video Meeting Request
    LOCATION: Phone/Video
    SEQUENCE:0
    STATUS:CONFIRMED
    SUMMARY:Meeting has been scheduled by ".$this->getUser()->getName()."
    TRANSP:OPAQUE
    END:VEVENT
    END:VCALENDAR";

$messageObject = \Swift_Message::newInstance();
$messageObject->setContentType("text/calendar");
$messageObject->setSubject("Your meeting has been booked")
              ->setFrom($this->container->getParameter('mailer_user'), "From Name")
              ->setTo($this->getUser()->getEmail())
              ->setBody(trim($message));
$this->get('mailer')->send($messageObject);

我真的很感激,如果我可以帮助我做错了导致导入事件失败的错误:无法处理您的iCal / CSV文件

2 个答案:

答案 0 :(得分:2)

您应该在服务器上编写iCal文件(使用Filesystem component或使用file_put_contents等本机PHP函数),然后将其作为附件发送:

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

$fs = new Filesystem();

//temporary folder, it has to be writable
$tmpFolder = '/tmp/';

//the name of your file to attach
$fileName = 'meeting.ics';

$icsContent = "
    BEGIN:VCALENDAR
    VERSION:2.0
    CALSCALE:GREGORIAN
    METHOD:REQUEST
    BEGIN:VEVENT
    DTSTART:".date('Ymd\THis', strtotime($meetingStartTime))."
    DTEND:".date('Ymd\THis', strtotime($meetingEndTime))."
    DTSTAMP:".date('Ymd\THis', strtotime($meetingStartTime))."
    ORGANIZER;CN=XYZ:mailto:do-not-reply@example.com
    UID:".rand(5, 1500)."
    ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP= TRUE;CN=Sample:emailaddress@testemail.com
    DESCRIPTION:".$this->getUser()->getName()." requested Phone/Video Meeting Request
    LOCATION: Phone/Video
    SEQUENCE:0
    STATUS:CONFIRMED
    SUMMARY:Meeting has been scheduled by ".$this->getUser()->getName()."
    TRANSP:OPAQUE
    END:VEVENT
    END:VCALENDAR"
;

//creation of the file on the server
$icfFile = $fs->dumpFile($tmpFolder.$fileName, $icsContent);

//message to include as body to your mail
$body = 'Hello...';

$messageObject = \Swift_Message::newInstance();
$messageObject->setSubject("Your meeting has been booked")
              ->setFrom($this->container->getParameter('mailer_user'), "From Name")
              ->setTo($this->getUser()->getEmail())
              ->setBody($body)
              ->attach(Swift_Attachment::fromPath($tmpFolder.$fileName))
;
$this->get('mailer')->send($messageObject);

//remove the created file
$fs->remove(array('file', $tmpFolder, $fileName));

答案 1 :(得分:1)

If you want an OOP creation, you can use the package : https://github.com/markuspoerschke/iCal

Installation can be done using composer and there's no need for adding a bundle in appKernel for SF users.