【问题标题】:Sending attachment in laravel mail在 laravel 邮件中发送附件
【发布时间】:2018-03-11 11:08:18
【问题描述】:

每个人。我正在尝试发送带有附件的电子邮件。我所做的是:

   controller code snippet:

     try{
           Mail::send(['name' => 'By System'],array(), function($msg) use ($email,$name,$message,$attachment,$ext,$display) {
                        $msg->from('ricket999@gmail.com', 'Markle Admin');
                        $msg->to($email)->subject('Payment Done')
                            ->setBody('This is to notify you that the employee named '.$name.' has been paid for the monthly payment. Thank You.','text/html')
                            ->attach(public_path().'/'.$attachment, ['as' => $display.'.'.$ext, 'mime' => 'application/pdf']);  });
        }
    catch(Exception $e){
             // if any error mark the mail sent status 0
             $result = DB::table('message')->where('name',$name)->where('email',$email)->update(['sent'=>'0']); 
            }

但它显示此错误:

    Type error: Argument 1 passed to Swift_Mime_SimpleMessage::attach() must implement interface Swift_Mime_MimeEntity, string given, called in E:\xampp\htdocs\email-send\app\Http\Controllers\HomeController.php on line 133

我错过了什么。我看到了thisthis,但它和我的一样,他们说它可以工作,但为什么我会收到这个错误。任何形式的帮助表示赞赏。谢谢。

【问题讨论】:

  • 您已将其标记为 laravel-5.4,并且您指的是 laravel-5.1 的文档。除了在我看来文件已损坏或路径不正确的形式。
  • @ChrysovalantisKoutsoumpos 好的,我已经更正了链接,这就是传递给邮件的内容:Swift_Mime_SimpleMessage->attach('E:\\xampp\\htdocs\\email-send\\public/files /1520764563.zip', array('as' => 'somename.zip', 'mime' => 'application/pdf'))。我认为路径和文件是正确的,因为它是从运行打开时使用上述文件路径打开的

标签: email laravel-5.4 email-attachments


【解决方案1】:

你有两个问题:

  1. 当您将 mime 设置为 application/pdf

  2. 时,您正尝试附加一个 zip 存档
  3. 存档路径错误。 代替 E:\\xampp\\htdocs\\email-send\\public\\files\\1520764563.zipE:\\xampp\\htdocs\\email-send\\public/files/1520764563.zip

关于Argument 1 passed to Swift_Mime_SimpleMessage::attach() must implement interface Swift_Mime_MimeEntity, string given 错误,我想这可以解决您的问题:

    $swiftAttachment = Swift_Attachment::fromPath($correctPath); 
    $this->email->attach($swiftAttachment); // now attach the correct type

【讨论】:

  • 好的,我在 Swift_Mime_SimpleMessage->attach('E:\\xampp\\htdocs\\email-send\\public\\files\\1520768296.pdf', array(' as' => 'somename.pdf', 'mime' => 'application/pdf'))。但仍然是同样的错误。
  • 它是说“传递给 Swift_Mime_SimpleMessage::attach() 的参数 1 必须实现接口 Swift_Mime_MimeEntity,给定字符串”。它将文件路径作为字符串。
猜你喜欢
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
相关资源
最近更新 更多