将文件附加到 Laravel 5 中的电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30079809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:30:24  来源:igfitidea点击:

Attach file to email in laravel 5

phpemaillaravelmailgun

提问by Neil Aldred

Im trying to attach a file to an email in laravel but i cant get it to work no matter what i do. It sends the email but with no attachment. doesn't give any errors neither.

我试图将文件附加到 laravel 中的电子邮件,但无论我做什么,我都无法让它工作。它发送电子邮件,但没有附件。也不会给任何错误。

public function sendCv($job)
{
    $cv = \App\File::where('id', Auth::user()->details->cv_id)->first();

    $cv_path = storage_path(). '/uploads/files/' . Auth::user()->id . "/" . $cv->identifier . "/" . $cv->name;

    $data = [];

    Mail::send('email.apply', $data, function ($message) use ($job, $cv_path) {
        $message->to('[email protected]')->from(Auth::user()->email)->subject('Application');
        $message->attach($cv_path);
    });

}

Its also setting the 'to' part of my email to 'Holidays in United Kingdom' in mac mail and i cant work out why?

它还将我的电子邮件的“收件人”部分设置为 Mac 邮件中的“英国假期”,但我不知道为什么?

enter image description here

在此处输入图片说明

Can anyone help and advise why this isn't working

任何人都可以帮助并建议为什么这不起作用

EDIT:

编辑:

Adding a second argument in the ->to method sorted out the holiday in the uk bit but with regards to troubleshooting i have already done.

在 ->to 方法中添加第二个参数整理了英国位的假期,但关于故障排除我已经完成了。

I Dump and died the path to make sure it was correct and it is. I checked the file exists and it does. I made the path incorrect which then give me an error (which suggests its working with the correct path)

我转储并死了路径以确保它是正确的。我检查了该文件是否存在并且确实存在。我使路径不正确,然后给了我一个错误(这表明它使用正确的路径)

Set up a file downloading working with the same path and it worked fine

设置使用相同路径的文件下载并且它工作正常

    $cv_path = storage_path(). '/uploads/files/' . Auth::user()->id . "/" . $cv->identifier . "/" . $cv->name;

    $headers = array(
        'Content-Type: application/pdf',
        'Content-Type: application/octet-stream',
    );

    return Response::download($cv_path, $cv->name, $headers);

this works fine and the file downloads.

这工作正常并且文件下载。

回答by Hamid Naghipour

if you want attach multi file :

如果你想附加多个文件:

 $path =[];
$path[] = '../path/your/number/one/file';
$path[] = '../path/your/number/two/file';

$data_mail = Mail::send($tmp, array('msg'=>$msg), function($message) use ($path) {
     $message->from('[email protected]', $_POST['subj'] );
     $message->to($_POST['to'])->subject($_POST['subj'] );
     $size = sizeOf($path); //get the count of number of attachments 

        for( $i=0; $i<$size; $i++ ){ 
         $message->attach($path[$i]); // change i to $i

        }
  },true);