1. 定义不同的发送邮箱config,如果想安全可以放到env

    <?php
    /**
     * Created by PhpStorm.
     * User: Lenovo
     * Date: 23/3/2021
     * Time: 下午4:50
     */
    return [
         //张亚
         '18'=> [
              'host' => 'smtp.qq.com',
              'port' => 465,
              'username' => '2850281407@qq.com',
              'password' => 'mykkpbdqhsuzddic',
              'from' => [
                   'address' => '2850281407@qq.com',
                   'name' => '张亚',
              ],
         ],
         //张正山
         '23'=> [
              'host' => 'smtp.qq.com',
              'port' => 465,
              'username' => '2850281398@qq.com',
              'password' => 'bhzwnbbdmbuvqdcgd',
              'from' => [
                   'address' => '2850281398@qq.com',
                   'name' => '张正山',
              ],
         ],
         //卢非
         '24'=> [
              'host' => 'smtp.qq.com',
              'port' => 465,
              'username' => '2850281398@qq.com',
              'password' => 'bhzwnprhyuvqdcgd',
              'from' => [
                   'address' => '2850281398@qq.com',
                   'name' => '卢非',
              ],
         ],
         //胡婕
         '25'=> [
              'host' => 'smtp.qq.com',
              'port' => 465,
              'username' => '2850281423@qq.com',
              'password' => 'ucawqrtyboxidhaa',
              'from' => [
                   'address' => '2850281423@qq.com',
                   'name' => '胡婕',
              ],
         ],
         //张云
         '26'=> [
              'host' => 'smtp.qq.com',
              'port' => 465,
              'username' => '2850281420@qq.com',
              'password' => 'jjwfryuimmjmdfdc',
              'from' => [
                   'address' => '2850281420@qq.com',
                   'name' => '张云',
              ],
         ],
         //王娟
         '28'=> [
              'host' => 'smtp.qq.com',
              'port' => 465,
              'username' => '2850281406@qq.com',
              'password' => 'xgxhertcmcypdfdc',
              'from' => [
                   'address' => '2850281406@qq.com',
                   'name' => '王娟',
              ],
         ],
         //章美
         '27'=> [
              'host' => 'smtp.qq.com',
              'port' => 465,
              'username' => '2881759484@qq.com',
              'password' => 'hcwbuiopkfgtsdfbh',
              'from' => [
                   'address' => '2881759484@qq.com',
                   'name' => '章美',
              ],
         ],
    ];

2.生成可邮寄类

    php artisan make:mail SendPrice

所有的可邮寄类配置都在 build 方法中完成,在这个方法中,你可以调用多个方法,例如 from,subject, view, 和 attach 来配置邮件的内容和发送。

3.配置可邮寄类 SendPrice

    <?php    
    namespace App\Mail;
    use Illuminate\Bus\Queueable;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Support\Facades\Log;
    class SendPrice extends Mailable
    {
        use Queueable, SerializesModels;
        /**
         * Create a new message instance.
         *
         * @return void
         */
        protected $data;
        public function __construct($data)
        {
            //
            $this->data = $data;
        }
        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            Log::info("start",['newSendPrice starting']);
            //return $this->view('view.name');
            //$from_address = $this->data['from']['address'];
            //$from_name = $this->data['from']['name'];
            $subject = $this->data['subject'];
            $member_info = $this->data['member_info'];
            $item = $this->data['item'];
            return $this->subject($subject)->view('newest_home.email.email_quote')->with([
                'member_info'=> $member_info,
                'item'=> $item
            ]);
            /*return $this->from($from_address, $from_name)->subject($subject)->view('newest_home.email.email_quote')->with([
                 'member_info'=> $member_info,
                 'item'=> $item
            ]);*/
        }
    }

4.配置邮件模板email_quote

    <html>    
    <head>
        <title>**网报价提醒信息</title>
    </head>
    <body>
    <?php $delivery = array(1=>'快递到门',2=>'物流自提',3=>'送货上门');?>
    <p>尊敬的{{$member_info['user_name']}}:</p>
    <p>感谢你的来访,您所询产品cas:<strong>{{$member_info['cas']}}</strong>  名称:<strong>{{$member_info['prod_name']}}</strong> 报价如下:</p>
    <table  style="width: 98%;">
        <tbody>
        @foreach($item as $k1=>$v1)
            @foreach($v1['list'] as $k=>$v)
        <tr>
            @if($v['customization_type'] == '定制')
                <td style="border: 1px solid #dddddd;">¥{{$v['price']}}/{{$v['package']}}{{$v['package_unit']}}/{{$v['purity']}}/{{$v['customization_type']}}{{$v['etd']}}天/{{$delivery[$v['ship_method']]}}/{{$member_info['invoice']}};</td>
            @else
                <td style="border: 1px solid #dddddd;">¥{{$v['price']}}/{{$v['package']}}{{$v['package_unit']}}/{{$v['purity']}}/{{$v['customization_type']}}/{{$delivery[$v['ship_method']]}}/{{$member_info['invoice']}};</td>
            @endif
        </tr>
            @endforeach
        @if($v1['note'])
            <tr>
                <td>备注:<span style="color: red;">{{$v1['note']}}</span></td>
            </tr>
        @endif
        @endforeach
        </tbody>
    </table>
    <p>如要下单,请登陆平台下单即可,或者联系对应撮合。</p>
    <p>**网祝您工作愉快,谢谢!</p>
    </body>
    </html>

5.队列发送邮件业务逻辑实现方法

业务逻辑发送邮件方法

    $member_info = array(
      'cas'=>$inquiry->cas,
      'prod_name'=>$inquiry->prod_name,
      'email'=>$inquiry->user->email,
      'can_send_email'=>$inquiry->user->can_send_email,
      'user_name'=>$inquiry->user->name,
      'manage_id'=>$inquiry->user->user_id, //撮合员
      'invoice'=>$inquiry->invoices->erp_name,
      'openid'=>$openid,
      'create_time'=>date('Y-m-d H:i:s',time())
    );
    
    $sendPriceArr = array(
      'member'=>$member_info,
      'item'=>$list,
    );
    
    //塞入发送邮件SendPriceEmail队列里
    $this->dispatch((new SendPriceEmail($sendPriceArr))->onQueue("SendPriceEmail"));

6. 新建 SendPriceEmail 队列

    php artisan make:queue SendPriceEmail

7. SendPriceEmail 队列实现

    <?php    
    namespace App\Jobs;
    use App\Library\Common;
    use App\Mail\SendPrice;
    use Illuminate\Bus\Queueable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Support\Facades\Log;
    use Illuminate\Support\Facades\Mail;
    class SendPriceEmail implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
        /**
         * Create a new job instance.
         *
         * @return void
         */
        protected $sendInfo;
        public function __construct($sendInfo)
        {
            //
            $this->sendInfo = $sendInfo;
        }
        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle()
        {
            
            Log::info("start", $this->sendInfo);
            
            $member_info = $this->sendInfo['member'];
            $item = $this->sendInfo['item'];
            
            if(!filter_var($member_info['email'], FILTER_VALIDATE_EMAIL)){
                return;
            }
            
            //不能发送的邮箱
            if($member_info['can_send_email'] == 1){
                Log::info("unable send email", [$member_info['email']]);
                return;
            }
            
            //找对应的发送邮件发送
            $subject = $member_info['cas']."/".$member_info['prod_name']."/"."已报价";
            $data = array(
                'member_info'=>$member_info,
                'item'=> $item,
                'subject'=> $subject
            );
            
            //设置对应的撮合员邮件发送
            Common::setUserSendEmailConfig($member_info['manage_id']);
            
            //发送邮件
            Mail::to($member_info['email'])->bcc('zzs911119@163.com')
                 ->send(new SendPrice($data));
                 
            //早前唯一邮箱发送办法
            /*Mail::send('newest_home.email.email_quote',['member_info'=>$member_info,'item'=>$item],function($message) use ($member_info, $subject){
                $message ->to($member_info['email'])->subject($subject);
            });*/
        }
    }

8.Common里两个方法,获取 设置给定的不同人员的邮箱Config 

         /**    
          * 获取撮合员邮箱设置
          * @param $user_id
          * @return mixed
          */
         public static function getUserSendEmailConfig($user_id){
              //撮合ids
              $user_ids = [18,23, 24, 25, 26, 28];
              if(!in_array($user_id, $user_ids)) return config('bs_email.27');
              return config('bs_email.'.$user_id);
         }
         /**
          * 不同用户对应自己撮合员邮箱发送
          * @param $user_id 撮合员id
          */
         public static function setUserSendEmailConfig($user_id){
              $userEmailConfig = self::getUserSendEmailConfig($user_id);
              $emailConfig = config('mail');
              $emailConfig['host'] = $userEmailConfig['host'];
              $emailConfig['port'] = $userEmailConfig['port'];
              $emailConfig['username'] = $userEmailConfig['username'];
              $emailConfig['password'] = $userEmailConfig['password'];
              $emailConfig['from']['address'] = $userEmailConfig['from']['address'];
              $emailConfig['from']['name'] = $userEmailConfig['from']['name'];
              config(['mail' => $emailConfig]);
         }