简体   繁体   中英

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 any way to PHP do this job automatically because I do this job manually by myself and the number of users is going up and maybe we must send emails in any seconds of a day and I want to do this job in Laravel 5.8 framework when I success on it.

The code I use in a PHP file and run this manually like this:

!!! (I get $UsersEmail array from a database by selecting the date and hour and it's like the array I write in codes) !!!

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

and I want to do this job in Laravel5.8 framework in the future

My PHP version is 7.2.7 and Laravel version I want to use in the future is 5.8

Please help me and I'm really confused

If any problem in my question please tell me on the comment and I will edit it, please don't vote it down

You can schedule a command that runs maybe every minute or once every hour, which checks whether any mails are at an appropriate time to be sent. In your table, you would have a flag that tells whether the mail was sent in order not to spam the user or whatever logic you want here. Once you get users, dispatch a job.

SendUserMail Command

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

In Kernel you have to register the command

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

ProcessUserMails Job

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

SendMailNotification

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

UserMail

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

This can be a starting point for you. However, be sure to check Laravel docs about creating commands and notifications as I have only included code snippets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM