php - 将 php 字符串视为邮件 txt 附件

标签 php email symfony

我有一个 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/

相关文章:

php - 使用 Symfony 清理 POST 和 GET 变量

php - 使用 PHP 邮件功能发送邮件 - 本地邮件服务器?

html - Outlook 不遵守父表的宽度 ="600"

php - FILTER_VALIDATE_EMAIL 是否使字符串可以安全地插入数据库?

java - 当有人回复同一邮件时,可以在发送电子邮件时将标签附加到电子邮件上

php - 选择字段的 Symfony2 DataTransformer

php - 数据库未出现在 MYSQL 控制台中

使用 cPanel 运行 CentOS 的 VPS 服务器上的 PHP CLI "Out of memory"错误

php - 从 MySQL 数据库读取数据到 HTML 表?

mysql - 为一个实体提供许多图像和(缩略图?)的最佳方式是什么?