簡體   English   中英

向隊列中的非注冊用戶(電子郵件列表)發送郵件

[英]send mail to non-registered user (email list) in a queue

我正在嘗試編寫帶有隊列的 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 class

您可以使用fill方法創建 User 的實例,而無需將其saving到數據庫中,例如;

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

問題出在Mail::to()方法中,它需要nameemail 但我只是發送 email,而且我沒有任何名字!

所以我所做的是:

制作 object 包括[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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM