像处理邮件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.

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);

If you want to use fopen use the php://memory wrapper instead of writing a file.

But I recommend to use the solution of @edmondscommerce