Accessing email from MySQL db to be use for sending email - php

I have a problem with my message notification using email.Why is that it generates an error inside the foreach.
The error is this part "$all_ngo" Undefined variable: all_ngo.
$pend = AddRequest::where('ship_id','=',$ship_id)->get();
$all_ngo = [];
foreach ($pend as $id) {
array_push($all_ngo, $id->ngo_id);
}
$orga_email = Auth::User()->orgainfo->orga_email;
$staffName = Auth::User()->orgainfo->inchargelname.' '. Auth::User()->orgainfo->inchargefname;
$name = $scholars->scholar_fname.' '.$scholars->scholar_mname.' '.$scholars->scholar_lname;
$input = array(
'name' => $staffName,
'email' => $orga_email,
'msgs' => 'asd' .' '. $name.'. '.'Hoping for your favorable response. Thank you!'
);
Mail::send('emails.mailMessage', $input, function($message){
$message->from('Somename#gmail.com');
foreach ($all_ngo as $id3) {<-------Undefined variable: all_ngo
$user = User::find($id3);
$ngo_email2 = $user->ngo_email;
$message->to($ngo_email2)->subject('Request For Sponsorship');
}
});

Because $all_ngo is out of scope. You can solve this by adding a global $all_ngo; into the function:
Mail::send('emails.mailMessage', $input, function($message) {
global $all_ngo;
$message->from('Somename#gmail.com');
foreach ($all_ngo as $id3) {<-------Undefined variable: all_ngo
$user = User::find($id3);
$ngo_email2 = $user->ngo_email;
$message->to($ngo_email2)->subject('Request For Sponsorship');
}
});
or allow the anonymous function to access the variable:
Mail::send('emails.mailMessage', $input, function($message) use ($all_ngo) {
$message->from('Somename#gmail.com');
foreach ($all_ngo as $id3) {<-------Undefined variable: all_ngo
$user = User::find($id3);
$ngo_email2 = $user->ngo_email;
$message->to($ngo_email2)->subject('Request For Sponsorship');
}
});

Related

Laravel Error: Only variable references should be returned by reference

I am working on a web app with php laravel framework. I'm also using framwork eloquent.
When i create a new "user" and i send it to my database the error with
$myValue->save();
This is the error:
"at HandleExceptions->handleError(8, 'Only variable references should be returned by reference',
'C:\\wamp64\\www\\project\\Fiq-spsslsj\\app\\Http\\Controllers\\DossierController.php', 97, array('request' => object(Request), 'employe' => object(employe)))
in DossierController.php line 97"
I've seen some similar problem but they are not quite really usefull (the problem in the other topics talk about code ingniter and common.php).
public function ajouter(Request $request){
$employe = new employe;
$employe->No_Employe = $request->input('No_Employe');
$employe->Nom = $request->input('Nom');
$employe->Prenom = $request->input('Prenom');
$employe->Email = $request->input('Email');
$employe->Adresse = $request->input('Adresse');
$employe->Date_Naissance = $request->input('Date_Naissance');
$employe->Titre_Emploi = $request->input('Titre_Emploi');
$employe->Telephone = $request->input('Telephone');
$employe->Annee_Embauche = $request->input('Annee_Embauche');
$employe->Present_Travail = $request->input('optTrav');
$employe->Sexe = $request->input('optSexe');
$employe->Fumeur = $request->input('optFum');
$employe->Langue = $request->input('Langue');
if (DB::table('liste_ville')->where('Nom_Ville','=',$request->input('Ville'))->get() != null)
{
$employe->Fk_Id_Ville = DB::table('liste_ville')->where('Nom_Ville','=',$request->input('Ville'))->value('Id_Ville');
}
else
{
$newVille = ucfirst($request->input('Ville'));
DB::table('liste_ville')->insertGetId(['Nom_Ville' => $newVille]);
$MoreVille = DB::table('liste_ville')->get();
foreach ($MoreVille as $key => $v)
{
if(strtolower($request->input('Ville')) === strtolower($v->Nom_Ville)){
$employe->Fk_Id_Ville = $v->Id_Ville;
break;
}
}
}
$employe->Code_Postal = $request->input('Code_Postal');
$employe->Actif = $request->input('optActif');
if (DB::table('provinces')->where('Nom_Province','=',$request->input('Province'))->get() != null) {
$employe->Fk_Id_Province =DB::table('provinces')->where('Nom_Province','=',$request->input('Province'))->value('Id_Province');
}
else
{
$newProv = ucfirst($request->input('Province'));
DB::table('provinces')->insertGetId(['Nom_Province' => $newProv]);
foreach ($provinces as $key => $p)
{
if(strtolower($request->input('Province')) === strtolower($p->Nom_Province)){
$employe->Fk_Id_Province = $p->Id_Province;
break;
}
}
}
$employe->save();
return redirect('/dossiers');
}
The code above is the code in my controller.
I hope you'll be able to help me.
Try this in your controller
return redirect()->to('/dossiers');

Allow user to send multiple's user to email via one function laravel

function sendEmail($user_id){
$ids = Input::get('system_id');
DB::setFetchMode(PDO::FETCH_ASSOC);
$send = DB::table('users')
->whereIn('user_id', $ids)->get();
$mainUser = User::where('user_id', $user_id)->first();
foreach($send as $value) {
$data = [
'master_name' => $mainUser['name'],
'master_id' => $mainUser->user_id,
'name' => $value->name,
'email' => $value['email'],
'link_confirmation_code' => $value['link_confirmation_code']
];
}
Mail::later(5, 'emails.send.confirm-user', $data, function($message) use ($emails, $names)
{
$message->from('no-reply#myemail.com', 'Myemail');
$message->to($emails, $names)->subject('Yes - connect us');
});
}
In blade
Hi {{$name}},
<td>{{$master_name}} has requested you to connect with us!</td>
Connect
I expect each user should get email along with main_user and each user have confirmation_link that allow him/her to verifying the same. Applied code but does work only for single/first user not for others. please suggest
I assume that you just trying send the mail using dynamic looping without delay operation. If this is correct, follow below code:
In controller:
function sendEmail($user_id){
$ids = Input::get('system_id');
DB::setFetchMode(PDO::FETCH_ASSOC);
$send = DB::table('users')
->whereIn('user_id', $ids)->get();
$mainUser = User::where('user_id', $user_id)->first();
foreach($send as $value) {
//Variable initializaation and setup operation...
$toEmail = $value->email;
$toName = $value->name;
$link_confirmation_code = url('v1/register/', [$link_confirmation_code]);
$data = [
'master_name' => $mainUser['name'],
'master_id' => $mainUser->user_id,
'name' => toName,
'email' => $toEmail,
'link_confirmation_code' => $link_confirmation_code
];
//Mail sendting operation...
Mail::send('emails.send.confirm-user', $data, function($message) use($toEmail, $toName) {
$message->from('no-reply#myemail.com', 'Myemail');
$message->to($toEmail, $toName)->subject('Yes - connect us');
});
}
}
In Blade:
Hi {{$name}},
<td>{{$master_name}} has requested you to connect with us!</td>
Connect
Just check that $toEmail and $toName, as there is no definition given so place it as assumption.

Getting Undefined offset: 2 error on foreach loop

Here is the code :
foreach ($test_scores as $score) {
switch ($score['name']) {
case 'LE-TOD':
$le_tod = $score['banding'];
$le_tod_desc = $score["result_desc"];
if($score['std_score'] == 0) {
$le_tod_small = $scoring['tod']['no_pref_bottom'];
}
else {
$le_tod_small = $scoring['tod']['pref_bottom'].$score['banding'];
}
break;
This is where I get the error message :
public static function generateLSPReportFile($dataInput, $auto_download = true)
{
$file_type = $dataInput['file_type'];
$test_type_id = $dataInput['test_type_id'];
$he_she = array('male' => 'He', 'female' => 'She');
$his_her = array('male' => 'his', 'female' => 'her');
$candidate = \Candidate::where('id',$dataInput['candidate_id'])->first();
if(!$candidate)
return;
$candidate = $candidate->toArray();
$candidate_name = $candidate['first_name'].' '.$candidate['last_name'];
$order = \Order::with('client')->find($dataInput['order_id']);
if(!$order)
return;
$client = $order->client;
$order = $order->toArray();
//for test dates
$order_candidate = \OrderCandidate::candidateId($dataInput['candidate_id'])->orderId($dataInput['order_id'])->first()->toArray();
$order_tests = \OrderCandidateTest::orderCandidateId($order_candidate['id'])
->status(\Config::get('kcg.candidate_test_status_taken'))
->testId($dataInput['test_id'])
->with(array('test' => function($query){
$query->select(['id', 'name', 'description', 'abbreviation']);
}));
$tests_scores = \OrderCandidateTestScore::orderId($dataInput['order_id'])
->candidateId($dataInput['candidate_id'])
->testId($dataInput['test_id'])
->with(array('test' => function($query){
$query->select(['id', 'name', 'description', 'test_type_id']);
}));
$test_type = TestType::find($test_type_id);
$test_scores = $tests_scores->orderBy('sequence_no')->get()->toArray();
$order_tests = $order_tests->get()->toArray();
if(!$order_tests)
return;
if(!$tests_scores)
return;
It posted an error : ErrorException Undefined offset: 2. I'm really not sure what's causing this.
What am I doing wrong here? Please help me. Thank you
Typically, the Undefined offset message would be triggered by trying to access an array key which doesn't exist.
It seems that your code example might not actually be showing the part that triggered the error... In your case, I would expect the code to show something like $var[2], where the array does not actually contain an index of 2.
See this other question for a clearer representation of what that error means.

Call to undefined method Illuminate\Support\Collection

I'm having a problem while running this code:
//DashboardController
public function getStream()
{
$user = Sentry::getUser();
$userid = $user->id;
$convs = TBMsg::getUserConversations($userid);
$getNumOfParticipants = $convs->getNumOfParticipants();
$participants = $convs->getAllParticipants();
$lastMessage = $convs->getLastMessage();
$senderId = $lastMessage->getSender();
$content = $lastMessage->getContent();
$status = $lastMessage->getStatus();
$posts = Post::whereIn('user_id', function($query) {
$query->select('follow_id')
->from('user_follows')
->where('user_id', '1');
})->orWhere('user_id', '1')->get();
return View::make('stream', array('getNumOfParticipants' => $getNumOfParticipants,
'participants' => $participants,
'lastMessage' => $lastMessage,
'senderId' => $senderId,
'content' => $content,
'status' => $status
))->with('posts', $posts)->with('convs', $convs);
}
}
I got this error: Call to undefined method Illuminate\Support\Collection::getNumOfParticipants()
http://i.stack.imgur.com/9N3xU.png
Replace ->get() with ->first() as right now you're basically returning an collection of arrays but you need that.
$query->select('follow_id')
->from('user_follows')
->where('user_id', '1');
})->orWhere('user_id', '1') // ->get() is removed
->first();
$convs is a collection so you can't call a method on it that only exists on a single model. Like the tutorial of the package suggests you have to iterate over the collection to use that function.
From the how to:
foreach ( $convs as $conv ) {
$getNumOfParticipants = $conv->getNumOfParticipants();
$participants = $conv->getAllParticipants();
/* $lastMessage Tzookb\TBMsg\Entities\Message */
$lastMessage = $conv->getLastMessage();
$senderId = $lastMessage->getSender();
$content = $lastMessage->getContent();
$status = $lastMessage->getStatus();
}

Undefined variable Array Slim Application

I am getting this error that runs fine on one of my servers with php 5.4 I transfered the code to a new server with php 5.5.9 and now I am getting this error:
Details
Type: ErrorException
Code: 8
Message: Undefined variable: propertylist
File: /var/www/subdomains/api/index.php
Line: 57
Trace
The code:
$app->get("/propertylist/", function () use ($app, $db) {
$app->response()->header("Content-Type", "application/json");
ob_start('ob_gzhandler');
$req = $app->request();
$bed = $req->get('bed');
$bath = $req->get('bath');
$city = $req->get('city');
$zip = $req->get('zip');
if($bed ==''){$bed=0;}
if($bath ==''){$bath=0;}
if($zip ==''){
$properties = $db->rets_property_listing_mrmls_resi->limit(2500,0)->where("Bedrooms >= ?", $bed)->where("City LIKE ?", "%$city%")->where("BathsTotal >= ?", $bath);
}else{
$properties = $db->rets_property_listing_mrmls_resi->limit(2500,0)->where("Bedrooms >= ?", $bed)->where("ZipCode LIKE ?", "%$zip%")->where("BathsTotal >= ?", $bath);
}
foreach ($properties as $property) {
$propertylist[] = array(
"MLSnumber" => $property["MLnumber"],
"ListPrice" => number_format($property["ListPrice"]),
"StreetNumber" => $property["StreetNumber"],
"StreetName" => $property["StreetName"],
"SqFt" => $property["SquareFootageStructure"],
"PropertyDescription" => summaryMode($property["PropertyDescription"],15),
"Bedrooms" => $property["Bedrooms"],
"BathsTotal" => $property["BathsTotal"],
"LO_Name" => $property["LO_Name"]
);
}
echo json_encode($propertylist);
});
You need create/define variable before use:
$app->get("/propertylist/", function () use ($app, $db) {
$app->response()->header("Content-Type", "application/json");
ob_start('ob_gzhandler');
$req = $app->request();
$bed = $req->get('bed');
$bath = $req->get('bath');
$city = $req->get('city');
$zip = $req->get('zip');
if($bed ==''){$bed=0;}
if($bath ==''){$bath=0;}
if($zip ==''){
$properties = $db->rets_property_listing_mrmls_resi->limit(2500,0)->where("Bedrooms >= ?", $bed)->where("City LIKE ?", "%$city%")->where("BathsTotal >= ?", $bath);
}else{
$properties = $db->rets_property_listing_mrmls_resi->limit(2500,0)->where("Bedrooms >= ?", $bed)->where("ZipCode LIKE ?", "%$zip%")->where("BathsTotal >= ?", $bath);
}
$propertylist = array(); //Create variable type array
foreach ($properties as $property) {
$propertylist[] = array(
"MLSnumber" => $property["MLnumber"],
"ListPrice" => number_format($property["ListPrice"]),
"StreetNumber" => $property["StreetNumber"],
"StreetName" => $property["StreetName"],
"SqFt" => $property["SquareFootageStructure"],
"PropertyDescription" => summaryMode($property["PropertyDescription"],15),
"Bedrooms" => $property["Bedrooms"],
"BathsTotal" => $property["BathsTotal"],
"LO_Name" => $property["LO_Name"]
);
}
echo json_encode($propertylist);
});
I think that version of PHP just changed the variables' scope, now it's more like you expect in other languages. If you define/create a variable in a block, it's not visible in the outer scope.
In your case, $propertylist is defined directly in the foreach block scope, hence it isn't visible to the rest of the code, causing the error.
As #Guilherme Nascimento suggested, you have to define it outside the loop.
That's said, in PHP is perfectly fine (but not recommended) to use a variable without instantiate it:
$var[] = 1;
// => array(1)

Categories