Dynamically created mail attachment of .ics file cannot open file for reading

Environment:
 - Laravel 5.3

Case:
Send an event message, including an attachment with an iCalendar ( .ics) file . The file icsis created using PHP.

Problem:
When sending mail with a file, icsLaravel returns the following error:

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

The generated file seems good when used without mail. The event is added to the calendar, so the file is not corrupted. During debugging ( ddon the way), the file is returned, so the permissions to access the folder / access also seem fine.

The code:

// 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);
}
+4
source share
1 answer

Several similar tickets suggested that you need to pass the real path to the swiftmailer.

Something like that:

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, problem with Swift-mailer

Retrieving the file name for attaching to email using SwiftMailer and PHP

+1
source

Source: https://habr.com/ru/post/1661683/


All Articles