为什么我看不到已发送电子邮件中上传的附件?

发布于 2025-01-20 07:55:05 字数 1712 浏览 1 评论 0原文

我不知道为什么在发送成功的电子邮件中看不到上传的附件。相反,我看到的是 file: /tump/php/Du460K 而不是实际的附件。

我以为 Mail:: 中的 foreach() 会成功,但事实并非如此。

我做错了什么?

这是MailController.php

public function support(Request $request) {
    $name        = $request->get('name');
    $email       = $request->get('email');
    $file        = $request->file('file');
    $messageText = $request->get('message');

    $data = [
        'name' => $name,
        'email' => $email,
        'file' => $file,
        'message' => $messageText
    ];

    Mail::send('email.support.support', $data, function($message) use($file) {
        $fileName = $file->getClientOriginalName();
        $mimeType = $file->getMimeType();

        $message->to('[email protected]');

        $data = [
            'as' => $fileName,
            'mime' => $mimeType
        ];

        if(!empty($file)) {
            foreach($file as $userFile) {
                $message->attach($userFile->getRealPath(), $data);
            }
        }

    });

    view('email.support.support', compact('name', 'email', 'file', 'messageText'));

    return Redirect::back()->with('success', 'Successfully submitted!');
}

这是support.blade.php

<div class="container">
    <p>Name: {{ $fullName ?? "" }}</p>
    <p>Email: {{ $email ?? "" }}</p>
    <p>Message: {{ $messageText ?? "" }}</p>
    <p>file: {{ $file ?? "" }}</p>
</div>

I'm not sure why I can't see the uploaded attachment in the email that's being sent successfully. Instead, I'm seeing file: /tump/php/Du460K instead of the actual attachment.

I thought my foreach() inside Mail:: would've done the trick but it hasn't.

What am I doing wrong?

Here's MailController.php

public function support(Request $request) {
    $name        = $request->get('name');
    $email       = $request->get('email');
    $file        = $request->file('file');
    $messageText = $request->get('message');

    $data = [
        'name' => $name,
        'email' => $email,
        'file' => $file,
        'message' => $messageText
    ];

    Mail::send('email.support.support', $data, function($message) use($file) {
        $fileName = $file->getClientOriginalName();
        $mimeType = $file->getMimeType();

        $message->to('[email protected]');

        $data = [
            'as' => $fileName,
            'mime' => $mimeType
        ];

        if(!empty($file)) {
            foreach($file as $userFile) {
                $message->attach($userFile->getRealPath(), $data);
            }
        }

    });

    view('email.support.support', compact('name', 'email', 'file', 'messageText'));

    return Redirect::back()->with('success', 'Successfully submitted!');
}

Here's support.blade.php

<div class="container">
    <p>Name: {{ $fullName ?? "" }}</p>
    <p>Email: {{ $email ?? "" }}</p>
    <p>Message: {{ $messageText ?? "" }}</p>
    <p>file: {{ $file ?? "" }}</p>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心旳酸楚 2025-01-27 07:55:05

您应该将文件存储在特定磁盘上,然后将URL返回actact()方法

检查此

https://laravel.com/docs/9.x/filesystem#file-uploads

我希望这很有帮助

you should store the file on a specific disk and return the URL to attach() method

check this

https://laravel.com/docs/9.x/filesystem#file-uploads

I hope it's helpful

残龙傲雪 2025-01-27 07:55:05

首先存储图像,然后使用存储的图像路径。

$filePath = $file->store('imagePathDirectory');
 if(!empty($file)) {
     foreach($file as $userFile) 
         $message->attach($userFile->$filePath, $data);
     }
 }

first store the image and then use the stored image path.

$filePath = $file->store('imagePathDirectory');
 if(!empty($file)) {
     foreach($file as $userFile) 
         $message->attach($userFile->$filePath, $data);
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文