2016-01-20 26 views
0

我想在Symfony2中生成日曆事件MS Outlook/Google日曆,而電子郵件是使用.ics文件發送的,但我無法添加事件到日曆。當我嘗試打開該文件,它說通過Symfony2生成併發送.ics文件(Swift Mailer)

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:[email protected] 
    UID:".rand(5, 1500)." 
    ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP= TRUE;CN=Sample:[email protected] 
    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文件

+0

你爲什麼不在你的服務器上寫ics文件,然後把它作爲附件發送? – Veve

+0

@Veve你能告訴我一個例子嗎?因爲它確實會有很大的幫助 – Saadia

回答

2

而應該寫你的服務器上您的iCal文件(使用象的Filesystem component或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:[email protected] 
    UID:".rand(5, 1500)." 
    ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP= TRUE;CN=Sample:[email protected] 
    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)); 
+0

有沒有人知道這樣的OOP創建ICS文件的lib? – mblaettermann

+0

@mblaettermann看看http://kigkonsult.se/iCalcreator/ – Veve

+0

你是否贊同這個包?看起來,它沒有作曲家的支持。不管怎麼說,還是要謝謝你。不想再劫持這個線程。對不起,關閉OffTopic。 – mblaettermann