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

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.

Related

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: 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)

Getting Error as htmlentities() expects parameter 1 to be string, array given

$email_ids= DB::table('users')->pluck('email_id');
$name = DB::table('users')-> pluck('name');
foreach ($email_ids as $email_id ) {
Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
$message->from('abc#xyz.com', 'ABC');
$message->to($email_id)->subject("Welcome");
});
}
I want to send a mail to respective users with their names in email. I get $email_ids and $name in array. but when I run this code , I get error as
htmlentities() expects parameter 1 to be string, array given
When I replace 'user' => $name with 'user' => $email_id . I runs successfully.
When I send mail it should be like
Hello , $name(name of user)
Thank You
Your code should be:-
$users = DB::table('users')->get(); // get all users
foreach ($users as $user ) {
$name = $user->name; // get user's name
$email_id = $user->email_id; // get user's email
Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
$message->from('abc#xyz.com', 'ABC');
$message->to($email_id)->subject("Welcome");
});
}
Note:- You can get specific columns from DB table via below query,
$users = DB::table('users')->select('name', 'email_id')->get();
Laravel shows htmlentities() error when there is an error in view file.
In your case you are getting name and email in different-different arrays but you only foreach $email_ids not $name In this line
Mail::send('mail', ['user' => $name ] , function ($message) use ($email_id) {
in this $name is array of names return by query and you are printing it as string that's why it is giving that error.
To achieve what you want change your code like this
$users = DB::table('users')->select('name')->addSelect('email_id')->get()->toArray();
foreach ($users _ids as $user ) {
Mail::send('mail', ['user' => $user->name ] , function ($message) use ($user->email_id) {
$message->from('abc#xyz.com', 'ABC');
$message->to($user->email_id)->subject("Welcome");
});
}

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

Return a JSON Array to a View in Laravel

I am using Laravel 4to build an applicaiton.
This is my code:
public function handleCreateCandle(){
$candle = new Candle;
$candle->name = Input::get('name');
$candle->body = Input::get('body');
$candle->user_id = Sentry::getUser()->id;
$candle->save();
return Redirect::action('User_BaseController#getPrayerSpace')->with('message', 'Thank you for lighting candle');
}
The issue is rather than returning a message I would like to return a json array to the view with a success code and the name and body of $candle. Can anyone advise me on how to do this?
Instead of using save() function you can use create() function to create a new row.
Create() function returns you the last inserted row, which you can use to show it in your message.
Example:
$user = User::create(array('name' => 'John'));
Please refer:
http://laravel.com/docs/eloquent

Categories