简体   繁体   English

如何在 laravel 中发送带有附件 (pdf) 的 email?

[英]How to send email with attachment (pdf) in laravel?

I want to send an email with an attachment (pdf) that comes from a request.我想发送一个带有来自请求的附件 (pdf) 的 email。 Without attachment.无执着。 an email has sent perfectly but with attachment, it threw an error一个 email 已完美发送,但带有附件,它抛出了一个错误

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

here is my mail class这是我的邮件 class

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(), 
                ]);
    }

}

} }

Here is my controller code这是我的 controller 代码

        $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));

Can anyone help me with how can I send an email with an attachment?谁能帮助我如何发送带有附件的 email?

You are attaching directly from request.But you should upload your iamge to storage or public folder.Then you can attach path of image您是直接从请求中附加的。但是您应该将您的 iamge 上传到存储或公共文件夹。然后您可以附加图像的路径

$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(), 
                ]);

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

in my controller i uploaded the file and send it to mail class like this:在我的 controller 中,我上传了文件并将其发送到邮件 class,如下所示:

    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));

and then in send mail class:然后在发送邮件 class 中:

 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',
        ])
        ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM