代码之家  ›  专栏  ›  技术社区  ›  Tekraj Shrestha

在laravel邮件中发送附件

  •  0
  • Tekraj Shrestha  · 技术社区  · 8 年前

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

       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
    

    我错过了什么。我看到了 this this 但它和我的一样,他们说它有效,但为什么我会出现这个错误。感谢您的任何帮助。非常感谢。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Chrysovalantis Koutsoumpos    8 年前

    您有两个问题:

    1. 您正在尝试附加zip存档,同时将mime设置为 application/pdf

    2. 存档的路径错误。 而不是 E:\\xampp\\htdocs\\email-send\\public\\files\\1520764563.zip 是的 E:\\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
    
    推荐文章