从视图内的公共文件夹访问资产(Laravel)

问题描述 投票:0回答:3

我无法在我的视图中访问资产。

这是我的代码。

 $mail->addAttachment(asset($file), 'receiptFile', 'base64', 'application/octet-stream');

浏览器显示以下错误:

无法访问文件:http://localhost/receiptGenerator2/public/5a46187d954de.png无法发送消息.Mailer错误:无法访问文件:http://localhost/receiptGenerator2/public/5a46187d954de.png ------

如您所见,文件的路径已成功找到。

如果Laravel可以找到该文件,为什么它会显示错误。

php laravel phpmailer
3个回答
1
投票

请尝试这种方式使用资产并提供图像名称,如果文件是任何文件夹而不是{{asset('folder_name / image_name')}}

{{ asset('5a46187d954de.png') }}

它产生http://localhost/receiptGenerator2/public/5a46187d954de.png


0
投票

试试这个:

->attach(
   public_path('receiptGenerator2/public/5a46187d954de.png'), [
   'as' =>"attachment.png" ,
   'mime' => 'application/octet-stream',
]);

资料来源:https://laravel.com/docs/5.5/mail#attachments

编辑:既然你正在使用phpmail,这应该可以解决问题

 $mail->addAttachment(public_path($file), 'receiptFile', 'base64','application/octet-stream');

邮件需要完整路径,是的你可以从浏览器打开该URL的文件,但phpmailer试图访问你的磁盘中的http://localhost/receiptGenerator2/public/5a46187d954de.png,所以它试图从/ var / www / html / project / http://localhost/receiptGenerator2/public/5a46187d954de.png获取文件有效。


0
投票

正如the docs for addAttachment()所说,第一个参数必须是本地文件系统路径;如果你给它一个URL它将无法工作。 PHPMailer不是HTTP客户端 - 还有许多其他功能和库可以帮助解决这个问题,因此如果您需要从远程URL获取某些内容以用作附件,请使用它们。您不应该使用本地资源的localhost URL,因为它不必要地低效。

© www.soinside.com 2019 - 2024. All rights reserved.