2016-11-23 37 views
4

環境:
- Laravel 5.3郵件動態創建的.ics文件附件無法打開文件進行讀取

案例:
郵件大約一個 '事件',包括與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('[email protected]', '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); 
} 
+0

可能是一個權限問題,這臺服務器處理用戶可以訪問的/存儲/程序/公共/事件/文件夾? –

+0

雖然「dd'ing是被退回該文件的存儲路徑,因此該文件被正確 – Liam

+0

訪問如果我的回答解決你的問題,請標記爲接受的真實路徑 –

回答

1

許多類似的門票已經建議你需要傳遞給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('[email protected]', '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

+0

總是返回false,嘗試了多種不同的路徑作爲參數 – Liam

+0

這是假設您的文件系統已正確設置https://laravel.com/docs/5.3/filesystem'請記住,如果您使用的是本地驅動程序,那麼應該公開訪問的所有文件都應該放在storage/app/public目錄中。此外,您應該在公共/存儲中創建一個指向存儲/應用程序/公共目錄的符號鏈接。' –

+0

我知道,並且找到了該文件。但。實時路徑每次都返回false。 – Liam