laravel - 邮件动态创建的 .ics 文件附件无法打开文件进行阅读

标签 laravel email laravel-5 icalendar swiftmailer

环境:
- Laravel 5.3

案例:
有关“事件”的邮件,包括带有 iCalendar 文件 (.ics) 的附件。正在使用 PHP 生成 ics 文件。

问题:
使用 ics 文件发送邮件时,Laravel 返回以下错误:

Swift_IoException in FileByteStream.php line 144: Unable to open file for reading [/storage/app/public/events/eventname.ics]

生成的文件在没有邮件的情况下使用时看起来很好。该事件将添加到日历中,因此文件不会损坏。调试时(路径上的dd)文件被返回,因此文件夹/访问权限似乎也很好。

代码:

// Build mail
public function build()
{
    $event = $this->createEvent($this->data[2]->planned_at, $this->data[2]->subject, $this->data[2]->content);

    return $this->view('emails.template')
        ->from('info@stackoverflow.nl', 'Stackoverflow')->subject($this->data[2]->subject)
        ->with([
            'token' => $this->data[0],
            'email' => $this->data[1],
            'mail' => $this->data[2],
            'sponsors' => $this->data[3],
            'name' => $this->data[4],

        ])
        ->attach(Storage::url('app/public/events/' . $this->data[2]->subject . '.ics'), [
            'as' => $this->data[2]->subject . '.ics',
            'mime' => 'calendar/event',
        ]);
}

// Build ics file
static function createEvent($date, $subject, $content)
{
    $dateTimeArr = explode(' ', $date);
    $newDate = $dateTimeArr[0];
    $dateArr = explode('-', $newDate);
    $newDate = $dateArr[0] . $dateArr[1] . $dateArr[2] . '-' . $dateTimeArr[1];
    $date = substr($newDate, 0, -9);

    $startTime = str_replace(":", "", substr($newDate, 9, -3));

    $strip = str_replace("\r", "", strip_tags($content));
    $desc = str_replace("\n", "", $strip);

    // ICS
    $mail[0] = "BEGIN:VCALENDAR";
    $mail[1] = "-//WEBSITE v1.0//NL";
    $mail[2] = "VERSION:2.0";
    $mail[3] = "METHOD:PUBLISH";
    $mail[4] = "BEGIN:VCALENDAR";
    $mail[5] = "DTSTART:" . $date . "T" . $startTime . "00Z";
    $mail[6] = "DTEND:" . $date . "T" . $startTime . "00Z";
    $mail[7] = "DTSTAMP:" . gmdate('Ymd') . 'T' . gmdate('His') . "Z";
    $mail[8] = "UID:" . md5(uniqid(mt_rand(), true));
    $mail[9] = "ORGANIZER;" . "Stackoverflow";
    $mail[10] = "CREATED:" . $date . "T" . $startTime . "00Z";
    $mail[11] = "DESCRIPTION:" . $desc;
    $mail[12] = "LAST-MODIFIED:" . $date . "T" . $startTime . "00Z";
    $mail[13] = "LOCATION:" . "";
    $mail[14] = "SEQUENCE:0";
    $mail[15] = "STATUS:CONFIRMED";
    $mail[16] = "SUMMARY:" . "";
    $mail[17] = "TRANSP:OPAQUE";
    $mail[18] = "END:VEVENT";
    $mail[19] = "END:VCALENDAR";

    //set correct content-type-header
    $filename = $subject . '.ics';
    $mail = implode("\r\n", $mail);

    header("text/calendar");

    Storage::put('public/events/' . $filename , $mail);
}

最佳答案

许多类似的票证建议您需要将真实路径传递给 swiftmailer。

类似这样的事情:

public function build()
{
$event = $this->createEvent($this->data[2]->planned_at, $this->data[2]->subject, $this->data[2]->content);
$path=realpath('app/public/events/' . $this->data[2]->subject . '.ics');
return $this->view('emails.template')
    ->from('info@stackoverflow.nl', 'Stackoverflow')->subject($this->data[2]->subject)
    ->with([
        'token' => $this->data[0],
        'email' => $this->data[1],
        'mail' => $this->data[2],
        'sponsors' => $this->data[3],
        'name' => $this->data[4],

    ])
    ->attach($path), [
        'as' => $this->data[2]->subject . '.ics',
        'mime' => 'calendar/event',
    ]);
}

https://laracasts.com/discuss/channels/laravel/unable-to-open-file-for-reading-swift-ioexception

PHP, Swift-mailer problem

Retrieving a file name to attach to an email with SwiftMailer and PHP

关于laravel - 邮件动态创建的 .ics 文件附件无法打开文件进行阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40759243/

相关文章:

regex - Laravel 验证小数 0-99.99

php - 隐式路由中的产品 slug 和 id

php - Laravel/Blade 表单添加 'required' 来选择元素

c# - 使用 Monotouch 发送多条短信和电子邮件

php - 为什么 Mandrill 电子邮件不从开发服务器(本地主机)发送?

laravel-5 - 基于时区的碳对象到 Unix 时间戳

mysql - 想要找到两个请求值

laravel - Illuminate\Database\QueryException 消息 'SQLSTATE[42S02]: Base table or view not found: 1146 Table ' test.people' doesn't exist

ruby-on-rails - 如何将电子邮件地址放入 Rails 的 url 中

php - 用户取消时 Laravel 社交名流错误