【问题标题】:Send attachment with Laravel SMTP and save to local storage使用 Laravel SMTP 发送附件并保存到本地存储
【发布时间】:2020-09-06 21:24:25
【问题描述】:

我在论坛中阅读了一个类似的主题解决了,但它在我身上没有成功。我在哪里做错了?

查看

<input type="file" class="" name="document">

发送

Mail::send([], [], function ($message) use ($request) {
$message->to($request->to);
$message->subject($request->subject);
$message->setBody($request->message);
$data = $request->document;
$message->attach($data['document']->getRealPath(), array(
'as' => $data['document']->getClientOriginalName(),
'mime' => $data['document']->getMimeType()));

我想以这种方式发送附件,但它没有。需要先上传再发送吗?

【问题讨论】:

  • 能否请您发布您遇到的错误?我认为通过仔细阅读,您实际上可能会想出部分解决方案;)
  • 我从输入部分获取它作为请求-> 文档。 $ 数据 = 请求-> 文档。
  • 在 null getRealPath () 和 getClientOriginalName () 上调用成员函数 getRealPath () 找不到它们。我认为 $data 数组是空的。

标签: laravel email smtp attachment


【解决方案1】:

在 laravel 的官方文档中有一整节关于请求中的文件。

https://laravel.com/docs/7.x/requests#files

https://laravel.com/api/7.x/Illuminate/Http/UploadedFile.html

因此,根据上述文档,您上传的文件在处理请求时(临时)保存 - 您不需要自己保存。

$document_path = $request->document->path();   

$message->attach($document_path, array(
    'as' => $request->document->getClientOriginalName(),
    'mime' => $request->document->getMimeType())
);

【讨论】:

  • dd ($ request-> document);在城市这样的空旷道路()上找不到它的输出。 Symfony \ 组件 \ HttpFoundation \ FileBag {# 55 ▼ #parameters: [] }
  • 您确定在浏览器端正确发送文件吗?
  • 是的,我用post方法的形式发送。主题、对象等正在工作,附件听起来是空的。
  • 使用 Illuminate \ Support \ Facades \ File;它没有用,因为我没有使用它。错误的原因是导入。但我仍然无法发送多播。这是我的 foreach 循环。 $ files = $ request-> file ('附件'); foreach ($files as $file) { $Message->attach ($file->getrealpath() ['as' => $file->getClientOriginalName()]); }
猜你喜欢
  • 2023-03-29
  • 2012-02-02
  • 2021-12-06
  • 2018-07-24
  • 1970-01-01
  • 2018-11-17
  • 2019-03-31
  • 1970-01-01
  • 2016-04-29
相关资源
最近更新 更多