Ask Your Question
4

How can a random email be picked from an array for Laravel's mailable feature?

asked 2022-03-23 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-08-23 13:00:00 +0000

ladyg gravatar image

To pick a random email address from an array for Laravel's mailable feature, you can use the array_rand() function in PHP.

Here's an example code snippet:

// Define an array of email addresses
$emails = [
    'john@example.com',
    'jane@example.com',
    'bob@example.com',
    'alice@example.com'
];

// Pick a random email address using array_rand()
$randomEmail = $emails[array_rand($emails)];

// Use the $randomEmail variable in your mailable code
Mail::to($randomEmail)->send(new YourMailable());

In this example, the array_rand() function picks a random index from the $emails array, and returns the corresponding email address. You can then use this email address in your Mail::to() function to send your mailable to a random recipient.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-23 11:00:00 +0000

Seen: 11 times

Last updated: Aug 23 '21