我试图在Laravel中使用Mailable,我遇到了一个我以前没遇到过的问题,目前似乎没有什么可以提供帮助。
在开发一个新的Mailable时,除了将一个EXISTING文件附加到mailable之外,我还能正常工作。
错误返回:
"message": "Unable to open file for reading [/public/storage/shipments/CJ2K4u6S6uluEGd8spOdYgwNkg8NgLFoC6cF6fm5.pdf]",
"exception": "Swift_IoException",
"file": "E:\\webserver\\htdocs\\truckin\\vendor\\swiftmailer\\swiftmailer\\lib\\classes\\Swift\\ByteStream\\FileByteStream.php",
"line": 131,
但是如果你浏览文件夹和文件,实际上有一个文件,我可以打开它,我甚至可以通过ajax弹出窗口打开它来查看详细信息。
这是我的mailable:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Shipment;
use App\Shipment_Attachment;
class shipmentAttachments extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public $shipment, $attachment, $storagePath;
public function __construct($shipment, $attachment, $storagePath)
{
$this->shipment = $shipment;
$this->attachment = $attachment;
$this->attachmentFile = '/public'.$storagePath;
$this->proNumber = $shipment->pro_number;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('billing@cmxtrucking.com')
->cc('billing@cmxtrucking.com')
->subject('New Attachment(s) - '. $this->proNumber)
->view('emails.shipments.shipmentAttachments',['shipment'=> $this->shipment])
->attach($this->attachmentFile);
}
}
这是我的控制器导致可邮寄:
public function attachmentsEmail(Request $request){
$shipment = Shipment::findOrFail($request->shipmentID);
$attachment = Shipment_Attachment::findOrFail($request->attachmentID);
$storagePath = Storage::url($attachment->attachmentPath);
$email = $request->email;
Mail::to($email)->send(new shipmentAttachments($shipment, $attachment, $storagePath)); //maybe try to use queue instead of send...
return back();
}
所以我不确定这可能来自哪里。
答案 0 :(得分:13)
尝试使用public_path()laravel helper函数而不是&#39; / public&#39;。
$this->attachmentFile = public_path() . '/' . $storagePath;
也许您需要在public / index.php中更改此变量。我正好在require bootstrap下面:
$app->bind('path.public', function() {
return __DIR__;
});
进行一些测试。
dd(public_path());
dd(public_path() . '/' . $storagePath);
或者可以验证FileSystem类是否存在文件。
希望这对你有帮助!
答案 1 :(得分:1)
我正在为此做很多事情,当你尝试在dompdf上构建PDF时,情况也是如此,完全一样,你可以写下这个:
(&#39; / image /&#39;。$ file)并且不起作用,所以你可以解决它在路径后面添加一个点&#34;。&#34;,就像这样:
(&#39; ./图像/&#39; $文件)
当您要在邮件发送中添加附件或想要制作包含图像的PDF时,它可以正常工作。
答案 2 :(得分:0)
如果您使用存储,并尝试使用Laravel通知导出xlsx文件:
在您的通知课程中:
公共函数toMail($ notifiable){
$path = Storage::disk('export')->getAdapter()->getPathPrefix();
return (new MailMessage)
->greeting(language_data('Your file is ready', $this->user->language_id).$this->user->name)
->line(language_data('Please, check your Email attachments.', $this->user->language_id))
->subject(language_data('Export Contacts', $this->user->language_id))
->attach($path.$notifiable->filename, [ 'as' => $notifiable->filename, 'mime' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', ])
->line(language_data('If you did not request this file, please contact us.', $this->user->language_id));
}
对我来说很好。