如何在 Laravel 中发送带有附件 (pdf) 的电子邮件?

时间:2021-05-30 05:59:06

标签: php laravel email

我想发送一封带有来自请求的附件 (pdf) 的电子邮件。无依附。一封电子邮件已完美发送,但带有附件,却引发了错误

    Fatal error: Uncaught exception 'Swift_IoException' with message 'The path cannot be empty' 

这是我的邮件类

class MyMail extends Mailable {
use Queueable, SerializesModels;
public $details;

public function __construct($details =[]) {
    $this->details = $details;
}

public function build() {
    if ($this->details['file']) {
        return $this->subject('New applicant')
            ->from("my@email.com", "name")
            ->view('email.mail')
            ->attach($this->details['file']->getRealPath(), 
                [
                    'as'   => $this->details['file']->getClientOriginalName(),
                    'mime' => $this->details['file']->getClientMimeType(), 
                ]);
    }

}

}

这是我的控制器代码

        $file = $request->file('file ');
        if ($file) {
            $file_name = hexdec(uniqid());
            $ext       = strtolower($file->getClientOriginalExtension());

            $file_full_name = $file_name . '.' . $ext;
            $upload_path    = 'files/';
            $upload_path1   = 'backend/files/';
            $file_url       = $upload_path . $file_full_name;
            $success        = $file->move($upload_path1, $file_full_name);
        }
        $details = [
            'applicant_name'    => $request->name,
            'applicant_contact' => $request->contact,
            'applicant_email'   => $request->email,
            'file'              => $file,
        ];

        $base_email = 'example@email.com';
        Mail::to($base_email)->send(new MyMail($details));

谁能帮助我如何发送带有附件的电子邮件?

2 个答案:

答案 0 :(得分:0)

您是直接从请求中附加的。但是您应该将您的图像上传到存储或公共文件夹。然后您可以附加图像的路径

$details = [
            'applicant_name'    => $request->name,
            'applicant_contact' => $request->contact,
            'applicant_email'   => $request->email,
            'file'              => $file,
            'fileUrl' => $file_url,
        ];

 $this->subject('New applicant')
            ->from("my@email.com", "name")
            ->view('email.mail')
            ->attach(asset( $this->details['fileUrl']), 
                [
                    'as'   => $this->details['file']->getClientOriginalName(),
                    'mime' => $this->details['file']->getClientMimeType(), 
                ]);

参考:https://laravel.com/docs/8.x/mail#attachments

答案 1 :(得分:0)

在我的控制器中,我上传了文件并将其发送到邮件类,如下所示:

    if ($request->has('resume')) {
        $file = $request->file('resume');
        $file_name =  Str::random(7) . '.'.$file->getClientOriginalExtension();
        $destinationPath = "/followUp";
        $file->move(public_path($destinationPath), $file_name);
        $file = 'followUp/'.$file_name;
    }
    $subject = 'پیگیری رزومه - ایندید';

    Mail::to('visanewcom@gmail.com')->send(new sendMail($username,$mobile,$subject,$file));

然后在发送邮件类:

 return $this->view('email.followUp')

    ->with('username',$this->username)
        ->with('mobile',$this->mobile)
        ->with('file',$this->file)
        ->subject($this->subject)
        ->attach(public_path($this->file), [
            'as' => 'resume.pdf',
            'mime' => 'application/pdf',
        ])
        ;