簡體   English   中英

如何通過自動設置日期和時間將 email 發送給 PHP 中的多個用戶 email

[英]How to send email to multiple user email in PHP by set date and hour automatically

I want to send email to multiple user email in PHP automatically by setting a date by the user I mean the user choose a date and hour from input date and I save it on a database then I want to send an email on that date is there PHP 的任何方式都會自動完成這項工作,因為我自己手動完成這項工作,用戶數量正在增加,也許我們必須在一天中的任何幾秒鍾內發送電子郵件,當我成功時,我想在 Laravel 5.8 框架中完成這項工作在上面。

我在 PHP 文件中使用的代碼並像這樣手動運行:

!!! (我通過選擇日期和時間從數據庫中獲取$UsersEmail數組,它就像我在代碼中編寫的數組)!!!

$UsersEmail = array(
    0 => 'user1@example.com',
    1 => 'user2@site.com',
    2 => 'user3@test.com',
    3 => 'user4@somesite.com',
    4 => 'user5@anohtersite.com',
);

$AllEmail = '';

foreach($UsersEmail as $user){
    $AllEmail .= $user.', ';
}

$to = $AllEmail;

$subject = 'Send Email';

$message = 'This is text message';

$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=utf-8';

$headers[] = 'From: My site <info@mysite.com>';

mail($to, $subject, $message, implode("\r\n", $headers));

我想將來在 Laravel5.8 框架中完成這項工作

我的 PHP 版本是 7.2.7 而我以后要使用的 Laravel 版本是 5.8

請幫助我,我真的很困惑

如果我的問題有任何問題,請在評論中告訴我,我會編輯它,請不要投反對票

您可以安排一個可能每分鍾或每小時運行一次的命令,以檢查是否有任何郵件在適當的時間發送。 在您的表格中,您將有一個標志,表明是否發送郵件是為了不向用戶發送垃圾郵件或您想要的任何邏輯。 獲得用戶后,派發工作。

發送用戶郵件命令

public function handle()
{
    $users = User::where('mail_sent', false)->whereDate('mail_send_date', '<=', Carbon::now())->get();
    ProcessUserMails::dispatch($users);
}

Kernel你必須注冊命令

protected function schedule(Schedule $schedule)
{
    $schedule->command('send-user-mail')->hourly();
}

ProcessUserMails 作業

public function handle()
{
    foreach ($this->users as $user) {
        $user->notify(new SendMailNotification($user));
        $user->update(['mail_sent', true);
    }
}

發送郵件通知

public function toMail($notifiable)
{
    return (new UserMail($this->user))->to($notifiable->email);
}

用戶郵箱

public function build()
{
    return $this->subject('User Custom Email')
        ->markdown('emails.user.custom_mail');
}

這可以成為您的起點。 但是,請務必查看有關創建命令和通知的 Laravel 文檔,因為我只包含了代碼片段。

暫無
暫無

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

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