dtmu88600 2015-06-24 09:25
浏览 39
已采纳

像处理邮件txt附件一样处理php字符串

I have a php class I use to generate Ical data. For now I just generate an ical file but I'd like to avoid creating it and just treat my ical data string to use it as an email attachment I could send. Is there a way in PHP to turn a string into a file without creating it? just treating it like a file?

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
".
                "VERSION:2.0
";
    if($cancel){
         $ical.="METHOD:CANCEL
";
    }
    $ical .=    "BEGIN:VEVENT
".
                "UID:".$cal_uid."
".
                "ORGANIZER;CN=Test:".$mailTutor."
".
                "DTSTART:".$dtstart."
".
                "DTEND:".$dtend."
".
                "DTSTAMP:".$todaystamp."
".
                "DESCRIPTION:".$meetingName."
".
                "SUMMARY:".$meetingName."
".
                "LOCATION:".$meeting_location."
".
                "END:VEVENT
".
                "END:VCALENDAR";

    $ics_file = fopen('MYPATH/myicsfile.ics', "w+");
    fwrite($ics_file, $ical);
    fclose($ics_file);

    $messagetobesent = Swift_Message::newInstance('Appointment Subject')
        ->setFrom(array('admin@noreply.com' => 'John Doe'))
        ->setTo(array($mailTutor, $mailBeneficiary))
        ->setBody($message)
    ;
    $swiftAttachment = Swift_Attachment::fromPath($icsfile);
    $messagetobesent->attach($swiftAttachment);
    $this->get('mailer')->send($messagetobesent);
}

I would like to get rid of the fopen fwrite fclose part and attach a file that only exists in memory but not on the hard drive.

  • 写回答

2条回答 默认 最新

  • douban2014 2015-06-24 09:58
    关注

    It's a standard feature of Swiftmailer - http://swiftmailer.org/docs/messages.html#attaching-dynamic-content

    See version below -not sure about MIME type

    // 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料