How to pass emails array to mail function in laravel? - php

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

Related

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

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.

Laravel: access to variables inside facades

The code below is the method for sending the feedback message by Laravel. I want to create the variables which values are html fields names. However, the $email_fieldName is not accessibleinside the Mail::send .../
public function sendFeedback(Request $request) {
$email_fieldName = 'email';
// ...
if ($request ->isMethod('post')) {
// ...
$inputedData = $request->all();
// $email_fieldName is accessible here
$result = Mail::send('email', ['inputedData' => $inputedData], function($message) use ($inputedData) {
// $email_fieldName is not accessible here
$message->from($inputedData[$email_fieldName], $inputedData[$name_fieldName]);
// ...
});
}
}
I tried below simulation fiddle and it works. What wrong with the variable access in the code above?
$nameKey = "name";
$testBool = true;
if ($testBool) {
$array= array("name"=>"Alex", "age"=>22, "student" => true);
echo($array[$nameKey]);
}
The Mail function can't find the $email_fieldName as you haven't mention it inside use()
Change
$result = Mail::send('email', ['inputedData' => $inputedData], function($message) use ($inputedData) {
with.
$result = Mail::send('email', ['inputedData' => $inputedData], function($message) use ($inputedData, $email_fieldName) {
That's because you are in the anonymous callback of the Mail::send method. $email_fielName is not in that scope.
The same way you have use ($inputedData), you should should add $email_fieldName :
Mail::send('email', ['inputedData' => $inputedData], function($message) use ($inputedData, $email_fieldName) {
(And probably $name_fieldName too given your code)

Mail in laravel , i want to send email with multiple recepients

I have a problem here.. i try to send email to multiple recepients. The recepients are from my database which name of table is subscribes the message error is like this
ErrorException in SimpleMessage.php line 297:
Illegal offset type
public function store_job(Request $request)
{
$this->validate($request, ['posisi' => 'required','persyaratan' => 'required','tanggung_jawab' => 'required']);
$tambah = new jobs(); //kita buat objek yang terhubung ke table JOBS
$tambah->posisi = $request['posisi'];
$tambah->persyaratan = $request['persyaratan'];
$tambah->tanggung_jawab = $request['tanggung_jawab'];
$tambah->kategori = $request['kategori'];
$tambah->save();
$anu = DB::table('subscribes')->select('email');
$data = array ('email'=>$anu);
Mail::send('emails.news', $data, function ($message) use ($request, $data) {
$message->from('stevanajja#gmail.com',$request->email);
$message->to($data['email'])->subject($request->posisi);;
});
return redirect()->to('/panel_admin/opportune');
}
please help as fast as possibble.. because i am a student, this is my homework for examination.
Here what I'am doing is declaring a new array $emails to store all email from database. By iterating the retrieved object anu, I am pushing the email to $emails and passing it to the to property of the mail.
$anu = DB::table('subscribes')->select('email')->get();
$emails=[];
foreach($anu as $a){
$emails[]=$a->email;
}
Mail::send('emails.news', $emails, function ($message) use ($request, $emails) {
$message->from('stevanajja#gmail.com',$request->email);
$message->to($emails)->subject($request->posisi);;
});
Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key.
This question should about match your effort.

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