How can I iterate the message of a Mail::send Laravel function? - php

I am trying to iterate and send message to mail in array of emails using Laravel Mail::send function
I searched for same problem and found the code below reference from Radmation here https://stackoverflow.com/a/39625789.
$emails = ['tester#blahdomain.com', 'anotheremail#blahdomian.com'];
Mail::send('emails.lead', ['name' => $name, 'email' => $email,
'phone' => $phone], function ($message) use ($request, $emails)
{
$message->from('no-reply#yourdomain.com', 'Joe Smoe');
//$message->to( $request->input('email') );
$message->to( $emails);
//Add a subject
$message->subject("New Email From Your site");
});
I am wondering the second paramater for iteration usage, so i can message each email with dynamic message of their name.

$emails = ['tester#blahdomain.com', 'anotheremail#blahdomian.com'];
foreach($emails as $currentRecipient){
$customtMsg = //create here a custom msg
Mail::send(['text' => 'view'], $customtMsg, function ($message) use ($request, $currentRecipient)
{
$message->from('no-reply#yourdomain.com', 'Joe Smoe');
$message->to($currentRecipient);
//Add a subject
$message->subject("New Email From Your site");
});
}
Please check usage here

You could put emails in associative array, for example:
$emails = [
'tester#blahdomain.com' => 'tester',
'anotheremail#blahdomian.com' => 'anotheremail'
];
And then iterate over key=>value pairs, like:
foreach($emails as $email=>$name){
Mail::send('emails.lead', ['name' => $name, 'email' => $email], function ($message) use ($email, $name){
$message->from('no-reply#yourdomain.com', 'Joe Smoe');
$message->to($email, $name);
$message->subject("New Email From Your site");
});
}
If you want to send same mail to multiple recipients at once, you could also pass an array of email=>name pairs to the to method:
$message->to($emails)
But I don't think it is possible to customize an email content individualy with that approach. Also in that case, all of the email addresses are visible to every recipient.

Related

Laravel assert multiple recipients of email during PHPUnit test

I want to test that an email has been sent to a number of addresses during a PHPUnit test. How can I achieve this?
Although the Laravel documentation does indicate that a hasTo() function exists within the Mail object:
// Assert a message was sent to the given users...
Mail::assertSent(OrderShipped::class, function ($mail) use ($user) {
return $mail->hasTo($user->email) &&
$mail->hasCc('...') &&
$mail->hasBcc('...');
});
It does not make clear that it is possible to assert that multiple address have been sent the mail. The hasTo function accepts the following structure as expected assertions:
[
[
'email' => 'johnny#appleseed.com',
'name' => 'Johnny Appleseed'
],
[
'email' => 'jane#appleseed.com',
'name' => 'Jane Appleseed'
],
]
As the name key is optional, the simplest way to test that specific users have received an email would look something like this:
Mail::fake();
$admins = User::where('administrator', true)->get()->map(function ($admin) {
return ['email' => $admin->email];
})->toArray();
Mail::assertSent(MyMailable::class, function ($mail) use ($admins) {
return $mail->hasTo($admins);
});
If you have used the default Laravel User model, or your user model has both name and email properties, you can pass your users in as a collection
Mail::fake();
$admins = User::where('administrator', true)->get();
Mail::assertSent(MyMailable::class, function ($mail) use ($admins) {
return $mail->hasTo($admins);
});

How to pass emails array to mail function in laravel?

$emails is an array but only shows me one email.
Also how to pass the email array into info_on_hand view?
$emails=$request['emails'][$i];
Mail::send('emails.info_on_hand', $data, function ($message) use($emails) {
$message->from('us#example.com', 'Name Company');
foreach($emails as $email):
$message->to($emails)->subject('Status: '.Input::get('desc').' With VIN# '.Input::get('vin').' is '.Input::get('status').'At Name');
endforeach;
});
So to send and pass the array of emails to the view and send it to them is like this
Guessing when you return just $emails it looks like this:
['test#example.com', 'test2#example.com','test3#example.com']
If so use this
$emails = $request['emails'];
Mail::send('emails.info_on_hand', ['emails' => $emails], function ($m) use ($emails) {
$m->from('youremail#eample.com', 'Name');
$m->to($emails);
$m->subject('Your Subject');
});
Another thing is that with the New GDPR laws don't think this is allowed?

Laravel 5.2 multiple email sending. Get email address in view

How to get the actually/current email address in the 'emails.subscribed' view?
Here is my code:
Mail::send('emails.subscribed', $data, function($message) use ($subject,$recievers,$meta) {
foreach ($recievers as $email) {
$message->bcc($email,$email);
$message->email = $email;
}
});
And the view I need to get the current email address (not the whole email array just only the current):
{{$somehow->email}}
Send emails one by one, not all in bcc, then you can easily change email view:
$recipients = [ ["name" => "John", "email" => "john#john.com"], ["name" => "Doe", "email" => "doe#doe.com"] ];
$subject = 'subject';
$meta = 'meta';
foreach($recipients as $recipient) {
// here you declare variables accesable in view file
$dataToPassToEmailView = [];
// **key** of this table is variable **name in view**
$dataToPassToEmailView['recipient'] = $recipient;
Mail::send('emails.subscribed', $dataToPassToEmailView, function($message) use ($subject, $recipient, $meta) {
$message->to($recipient['email'], $recipient['name']);
$message->subject($subject);
});
}
view emails.subscribed.blade.php:
Email: {{ $recipient['email'] }}
Name: {{ $recipient['name'] }}

how to send text mail in laravel

i want to send text not view by laravel mail , as i use simple form wth textarea to write the content of message , how to seend text or how to convert this text to view ....... thanks
public function sendmail(){
$sendmessage=Input::get('message');
//$messageView=View::make('messageview')->with('message',$sendmessage);
Mail::send($sendmessage, array('name'=>'hossam'),function($message){
$title=Input::get('title');
$mail=Input::get('mailsender');
$message->from($mail,'user');
$message->to('webdev11111#gmail.com','hossam gamal')->subject($title);
You can create an email view where the only thing it sends is the contents of a variable:
app/views/emails/nonview.blade.php
{{ $contents }}
Then, you can use this view whenever you want to send an email that doesn't have a view:
public function sendmail() {
$from = Input::get('mailsender');
$subject = Input::get('title');
// 'contents' key in array matches variable name used in view
$data = array(
'contents' => Input::get('message')
);
Mail::send('emails.nonview', $data, function($message) use ($from, $subject) {
$message->from($from, 'user');
$message->to('webdev11111#gmail.com','hossam gamal')->subject($subject);
});
}

Sent mail to multiple address using Laravel 4 queue

I'm using Laravel 4 , tying to send mail to multiple user using Queueing Mail , my code looks like -
$mailuserlist=DB::table('table')
->join('some_table')
->select('some_thing')
->where('somecondition'))->get();
Mail::queue('mail_template', $data, function($message) use ($mailuserlist)
{
$message->from('test#desto.co.in', 'Mail Notification');
foreach ($mailuserlist as $value) {
$message->to($value['email'],$value['firstname'].' '.$value['lastname']);
}
$message->subject('Testing mail');
});
..it's not at all working . How can i send ail to multiple address ??
It should be possible in two ways as we can see in the source code framework/src/Illuminate/Mail/Message.php:
Chaining
Using array
Chaining:
->to($address1, $name1)->to($address2, $name2)->to($address3, $name3)...
Using array of addresses:
->to(array($address1,$address2,$address3,...), array($name1,$name2,$name3,...))
Queueing Mail doesn't seem to support sending 1 mail to multiple user. I think you should queue 1 mail for each of your recipients:
$mailuserlist=DB::table('table')
->join('some_table')
->select('some_thing')
->where('somecondition'))->get();
foreach ($mailuserlist as $mailuser) {
Mail::queue('mail_template', $data, function($message) use ($mailuser) {
$message
->from('test#desto.co.in', 'Mail Notification')
->to($mailuser['email'],
$mailuser['firstname'].' '.$mailuser['lastname'])
->subject('Testing mail');
});
}
You can get all emails to send
$users = User::select('email')->get()->toArray();
And delete the key of array for get an array only with the emails
$emails = array_pluck($users, 'email');
And then run the Mail::queue
Mail::queue('mail.your_view', [], function($message) use ($emails) {
$message->from('test#from.com', 'Mail Notification');
$message->to($emails);
$message->subject('Your Subject');
});
**
Sending multiple users
**
$users = UsersGroup::where(['groups_id' => $group->id])->get();
if(!$users->count()) {
Mail::send('emails.groups.delete-group', [
'group' => $group->title,
], function ($message) use ($group, $users) {
$message->from(Config::get('mail.from.address'), Config::get('mail.from.name'))
->subject(Lang::get('groups.group_deleted'));
foreach ($users as $user) {
$message = $message->to($user->users->email);
}
});
}

Categories