laravel5.6 使用mail发送附件邮件

模型:

?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    private $data;
    public function __construct($data)
    {
        $this-data = $data;
    }


    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this-view('emails.common')
                    -subject('测试邮件')
                    -attach(storage_path($this-data['file_path']), ['as' = $this-data['file_name']]);
    }
}

控制器:

public function sendMail()
   {
        $to = '15033245576@139.com';

        $file_name = '2019语言学资料.pdf';
        $file_name = "=?UTF-8?B?".base64_encode($file_name)."?=";

        $info['file_path'] = 'test.pdf';
        $info['file_name'] = $file_name;

        $mail = (new SendMail($info))-onQueue('mail');

        Mail::to($to)-queue($mail);

        if(count(Mail::failures())  1){
            return '发送邮件成功,请查收!';
        }else{
            return '发送邮件失败,请重试!';
        }

    }

注意:改代码后需要执行一下命令,否则有可能发送邮件失败

!!!发邮件前需要缓存清理(进入虚拟机,执行一下操作):

重启队列
php artisan queue:restart

清楚缓存
php artisan config:cache
php artisan cache:clear
php artisan route:clear

最新回复(0)
/jishum24cTCmMaGMqcUWMASR6wCddewRG0T9oBeMVGufUAgY_3D4858546
8 简首页