将邮件发送到队列中的未注册用户(电子邮件列表)

时间:2020-06-02 22:56:04

标签: php laravel laravel-7

我正在尝试使用队列对Laravel邮件列表进行编程尝试向非用户发送电子邮件,我在注册用户方面做得很好,这是我的代码:

        foreach ($emails as  $i=>$item){
            $user = User::where('email',$item)->first();
            if ($i % $mailsCount == 0)$count++;
            if ($user){
                $thisBody = ElzahabyDynamicLaravelString($user,$body,$this->sign);
                $thisSubject = ElzahabyDynamicLaravelString($user,$subject,$this->sign);
                $when = Carbon::now()->addMinutes($every*$count);
                Mail::to($user)
                    ->later($when,new EmailForQueuing($user,$thisBody,$thisSubject,$request->emailTemplate));
            }
        }

您可以通知 later()方法,该方法负责队列。但这仅适用于Mail::to方法,该方法仅适用于注册用户。

因此,任何想法如何将带有queue的邮件列表发送到未注册的电子邮件。

谢谢。

编辑1

EmailForQueuing()方法是一个简单的Mailable

2 个答案:

答案 0 :(得分:1)

您可以使用fill方法在数据库中创建一个没有saving的User实例,例如;

$mailUser = (new User())->fill(['email' => 'something@foo.bar']);

您还可以传递其他字段,例如['name' => 'some name', 'age' => 23]

另一个不用填充的选项是

$mailUser = (new User(['email' => 'something@foo.bar']))

您的代码将是这样;

$mails = ['email1@com', 'email2@com', 'email3@com', 'etc.com'];
$users = [];

foreach ($mails as $mail) {
    $users[] = (new User(['email' => $mail]));
}

return $users;

答案 1 :(得分:1)

Mail::to()方法中的问题在于它需要nameemail。 但是我只是发送电子邮件,没有名字!

所以 我所做的是:

使对象包含[emails(required),name(required),any other thing(optional)]

$user = (object)[
    'email' => $email,
    'name' => substr($email, 0, strpos($email, '@')), // here we take the name form email (string before "@")
];

然后正常发送:

$when = Carbon::now()->addMinutes($every*$count);
Mail::to($user)->later($when,new EmailForQueuing($user,$thisBody,$thisSubject,$request->emailTemplate));