我有一个 php 类,用于生成 Ical 数据。现在我只是生成一个 ical 文件,但我想避免创建它,而只是将我的 ical 数据字符串视为我可以发送的电子邮件附件。 PHP 有没有一种方法可以将字符串转换为文件而不创建它?只是将其视为文件?
public function sendInviteMail ($mailTutor, $mailBeneficiary, $meetingDate, $meetingEndDate, $meetingName, $meeting_location, $cancel, $UID){
$meetingStamp = strtotime($meetingDate . " UTC");
$meetingEndStamp = strtotime($meetingEndDate . " UTC");
$dtstart= gmdate("Ymd\THis\Z",$meetingStamp);
$dtend= gmdate("Ymd\THis\Z",$meetingEndStamp);
$todaystamp = gmdate("Ymd\THis\Z");
//Create unique identifier @todo Changer la méthode de creation récupérer l'uid en base ou le reconstruire si possible
$cal_uid = $UID;
$ical = "BEGIN:VCALENDAR\n".
"VERSION:2.0\n";
if($cancel){
$ical.="METHOD:CANCEL\n";
}
$ical .= "BEGIN:VEVENT\n".
"UID:".$cal_uid."\n".
"ORGANIZER;CN=Test:".$mailTutor."\n".
"DTSTART:".$dtstart."\n".
"DTEND:".$dtend."\n".
"DTSTAMP:".$todaystamp."\n".
"DESCRIPTION:".$meetingName."\n".
"SUMMARY:".$meetingName."\n".
"LOCATION:".$meeting_location."\n".
"END:VEVENT\n".
"END:VCALENDAR";
$ics_file = fopen('MYPATH/myicsfile.ics', "w+");
fwrite($ics_file, $ical);
fclose($ics_file);
$messagetobesent = Swift_Message::newInstance('Appointment Subject')
->setFrom(array('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83e2e7eeeaedc3edecf1e6f3effaade0ecee" rel="noreferrer noopener nofollow">[email protected]</a>' => 'John Doe'))
->setTo(array($mailTutor, $mailBeneficiary))
->setBody($message)
;
$swiftAttachment = Swift_Attachment::fromPath($icsfile);
$messagetobesent->attach($swiftAttachment);
$this->get('mailer')->send($messagetobesent);
}
我想摆脱 fopen fwrite fclose 部分并附加一个仅存在于内存中但不存在于硬盘上的文件。
最佳答案
这是 Swiftmailer 的标准功能 - http://swiftmailer.org/docs/messages.html#attaching-dynamic-content
请参阅下面的版本 - 不确定 MIME 类型
// Create your file contents in the normal way, but don't write them to disk
$data = 'YOUR STRING';
// Create the attachment with your data
$attachment = Swift_Attachment::newInstance($data, 'ical.ics', 'application/ics');
// Attach it to the message
$message->attach($attachment);
关于php - 将 php 字符串视为邮件 txt 附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31022730/