通过 Symfony2(Swift 邮件程序)生成并发送 .ics 文件
Generate and Send .ics file via Symfony2 (Swift Mailer)
我正在尝试在 Symfony2 中生成日历事件 MS Outlook/Google Calendar,而电子邮件是使用 .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 文件
您应该将 iCal 文件写入您的服务器(使用 Filesystem component or with native PHP functions like file_put_contents),然后将其作为附件发送:
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));
如果您想要创建 OOP,可以使用包:https://github.com/markuspoerschke/iCal
可以使用 composer 完成安装,SF 用户无需在 appKernel 中添加包。
我正在尝试在 Symfony2 中生成日历事件 MS Outlook/Google Calendar,而电子邮件是使用 .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 文件
您应该将 iCal 文件写入您的服务器(使用 Filesystem component or with native PHP functions like file_put_contents),然后将其作为附件发送:
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));
如果您想要创建 OOP,可以使用包:https://github.com/markuspoerschke/iCal
可以使用 composer 完成安装,SF 用户无需在 appKernel 中添加包。