Passing data to queued Mail object in Laravel 4, using Iron.io - php

I am trying to set up a queued email in Laravel 4 using the Iron.io driver. I would like to pass some details to the email's Subject and From attributes but they seem to not be making it into the queue request and their presence causes the email to not be sent (not sure where to look for a log with errors). However, simply using Mail::Send() works fine.
Here is the code in question:
public function handleFeedbackForm()
{
$data = array(
'name_f' => Input::get('name_f'),
'name_l' => Input::get('name_l'),
'email' => Input::get('email'),
'commentType' => Input::get('commentType'),
'testimonialPublish_answer' => Input::get('testimonialPublish_answer'),
'comment' => Input::get('message')
);
$rules = array(
'name_f' => 'required',
'name_l' => 'required',
'email' => 'required|email',
'message' => 'required'
);
$v = Validator::make(Input::all(), $rules);
if ($v->passes())
{
$emailInfo = array('name_f' => Input::get('name_f'),
'name_l' => Input::get('name_l'),
'email' => Input::get('email'));
Mail::queue('emails.feedback', $data, function($message) use($emailInfo)
{
$recipients = array();
$form = MailType::find(1);
foreach ($form->users as $user)
{
$recipients[] = $user->email;
}
if (count($recipients) == 0)
{
// Nobody for this field, send to webmaster
$recipients[] = 'someone#somewhere.com';
}
$message->to($recipients)
->from($emailInfo['email'])
->subject('Foobar Feedback Form Message - ' . $emailInfo['name_f'] . ' ' . $emailInfo['name_l']);
});
return Redirect::to('contact')->with('feedbackSuccess', true);
}
else
{
return Redirect::to('contact')->with('feedbackError', true);
}
}
Any ideas? Thanks!

Related

method not allowed laravel validation post

I want to validate input in POST method but the result message shows that Method is Not Allowed. Here's my code for create new user in database (UserController.php)
public function userRegister(Request $request)
{
$data['error']['state'] = false;
$rules = [
'name' => 'required',
'username' => 'required|unique:username',
'nip' => 'required|unique:nip',
'email' => 'required|unique:email',
'phone' => 'required',
'avatar' => 'required',
'password' => 'required',
'faculty_id' => 'required',
'building_id' => 'required',
'room_id' => 'required'
];
$message = [
'required' => 'Fill the required field.',
'username.unique' => 'Username already taken.',
'nip.unique' => 'Staff ID already taken.',
'email.unique' => 'Email already taken.',
];
$validator = $this->validate($request,$rules,$message);
if($validator->fails()){
$data['error']['state'] = true;
$data['error']['data'] = $validator->errors()->first();
}
else{
$data['user']['name'] = $request->input('user.name');
$data['user']['surname'] = $request->input('user.surname');
$data['user']['username'] = $request->input('user.username');
$data['user']['nip'] = $request->input('user.nip');
$data['user']['email'] = $request->input('user.email');
$data['user']['password'] = Hash::make($request->input('user.password'));
$data['user']['phone'] = $request->input('user.phone');
$data['user']['level'] = $request->input('user.role');
$data['user']['username_telegram'] = $request->input('user.username_telegram');
$data['user']['user_email_action'] = $request->input('user.user_email_action');
$data['user']['user_telegram_action'] = $request->input('user.user_telegram_action');
$data['user']['faculty_id'] = $request->input('user.faculty');
$data['user']['building_id'] = $request->input('user.building');
$data['user']['room_id'] = $request->input('user.room');
$data['user']['verified'] = 0;
if(!empty($request->input('user.avatar'))){
$data['user']['avatar'] = $request->input('user.username').'-'.$request->input('user.new_avatar');
}
else{
$data['user']['avatar'] = 'default.png';
}
$user_id = DB::table('register')->insertGetId($data['user'],'id');
}
return response()->json($data);
}
Here's the message:
Error Message
Do you know how to solve it? Thank you.
Your question doesn't show the routes of your application but you'll need to make sure your form is set to post
<form action="/my/url/path" method="post">
and your route is set to 'post'
E.g.
Route::post('/my/url/path', 'MyController#userRegister');
Please note that if you're not using Laravel Collective you'll need to make sure you include the CSRF token
<form method="POST" action="/my/url/path">
#csrf (laravel 5.6)
{{ csrf_field() }} (previous versions)

Laravel 5.4 Image validation is not working

I'm kinda stuck here on images validation in laravel. I have to validation the file input must be an image and for that I used the classical way of laravel validation but I don't why it is not working any clue?
User Controller
public function createProfile(Request $request) {
$phoneNumber=$request->phoneNumber;
if (empty($request->except(['userId','token']))){
$data= array(
'nickName' => '',
'profilePic' => '',
'phoneNumber' => '',
'userHeight' => '',
'userWeight' => '',
'userVertical' => '',
'userSchool' => '',
'homeTown' => '',
);
$this->setMeta("200", "Success");
$this->setData("userDetails", $data);
return response()->json($this->setResponse());
}
if($phoneNumber) {
$validationData= array(
'phoneNumber' => $phoneNumber,
);
$validationRules = array(
'phoneNumber' => [
'regex:/^[0-9]+$/',
'min:10',
'max:15',
Rule::unique('users')->ignore($request->userId, 'userId'),
]
);
if($request->has('profilePic')){
$validationData['profilePic'] = $request->profilePic;
$validationRules['profilePic'] = 'image|mimes:jpeg,bmp,png';
}
$validator = Validator::make($validationData,$validationRules);
if ($validator->fails()) {
$errors = $validator->errors();
if ($errors->first('phoneNumber')) {
$message = $errors->first('phoneNumber');
} else if ($errors->first('profilePic')) {
$message = $errors->first('profilePic');
} else {
$message = Constant::MSG_422;
}
$this->setMeta("422", $message);
return response()->json($this->setResponse());
}
}
$homeTown = $request->homeTown;
$filename='';
$profilePic=$request->file('profilePic');
if(!empty($profilePic)) {
$destinationPath = public_path() . '/uploads/users';
$filename = "image_" . Carbon::now()->timestamp . rand(111, 999) . ".jpg";
$profilePic->move($destinationPath, $filename);
}
$user = User::where('userId',$request->userId)->first();
if($request->hasFile('profilePic')){
$user->profilePic = $filename;
}
$user->nickName=$request->nickName;
$user->phoneNumber=$request->phoneNumber;
$user->userHeight=$request->userHeight;
$user->userWeight=$request->userWeight;
$user->userVertical=$request->userVertical;
$user->userSchool=$request->userSchool;
$user->homeTown=$homeTown;
$user->save();
$this->setMeta("200", "Profile Changes have been successfully saved");
$this->setData("userDetails", $user);
return response()->json($this->setResponse());
}
I would assume the reason your validation isn't working is because you adding the rule inside:
if($request->has('profilePic')){
This needs to be $request->hasFile('profilePic').
Hope this helps!
you can use as like this i think it is better.....
if($request->hasFile('profilePic')){
$rules = array(
'profilePic' => 'required | mimes:jpeg,jpg,png',
);
}
$validator = Validator::make($request->all(), $rules);
Just use this for validation. If you have to handle condition then go through step by step debuging.
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:5120',
'description' => 'required'
]);

Send a email only if a users updates profile with new data. Laravel

My application gives employees of a company the ability to edit their data(example: address). Once they change any of it, a manager gets an e-mail with their new information. The problem is that an email is sent every time a employee clicks update. I need the application to compare the info in the database and only send an email if there's new information. What is good way to achieve this? I am sorry I forgot to mention, the email needs to contain only the new iformation. So isDirty() would not work for me.
public function editcredentials_action(Request $request)
{
$user = Auth::user();
$address = $user->address;
$this->validate($request, [
'password' => 'max:255',
'language' => 'integer',
'facebook_profile' => 'max:255',
'twitter_profile' => 'max:255',
'street' => 'max:255',
'house_number' => 'max:255',
'city' => 'max:255',
'zip_code' => 'max:255',
'country' => 'max:255',
]);
if (!empty($request->get('password')) && $request->get('password')) {
$user->password = bcrypt($request->get('password'));
}
$user->facebook_profile = $request->get('facebook_profile');
$user->twitter_profile = $request->get('twitter_profile');
$user->language_id = $request->get('language');
$user->save();
if (!$address) {
$address = new UserAddress();
$address->user_id = $user->id;
}
$address->street = $request->street;
$address->house_number = $request->house_number;
$address->city = $request->city;
$address->zip_code = $request->zip_code;
$address->country = $request->country;
$address->save();
$data = [
'email' => $user->email,
'facebook' => $user->facebook_profile,
'twitter' => $user->twitter_profile,
'name' => $user->name . ' ' . $user->lastname,
'address' => $address,
];
Mail::send('emails.user-update', $data, function ($message) use ($data) {
$message->from('no-reply#example.com', 'Profile change');
$message->to('profilechange#example.com');
$message->subject('Profile change: ' . $data['name']);
});
Session::flash('message', trans('app.global.saved'));
return back();
//return redirect()->route('profile.edit-credentials');
}
I don't think isDirty() will work here, but you can save user data in the beginning:
$params = ['language', 'facebook_profile', 'twitter_profile', 'name'];
$oldUserData = auth()->user()->only($paramsToCOmpare);
Then compare the data after using save() method:
if (count(array_diff($oldUserData, $user->only($paramsToCompare))) > 0) {
// Data was changed.
}
You can do the same for UserAddress model. For password just check if it was filled in the form (since you're not displaying it anyway):
if (!empty($request->password))

sending random ids - Laravel email

Good day
I'm using Mailgun to send emails to the users with required info from the order after submitting the form,I managed to send the subject and the email address ,but I'm having trouble sending the random number that gets assigned on the creation of the order.
here is my controller:
public function store(Request $request)
{
$order = $user->orders()->create([
'randomid' => rand(100000,999999),
'subject' => $request->get('subject'),
'email' => $request->get('email'),
]);
$data = $request->only('subject', 'email', 'randomid');
Mail::send('emails.note',
$data
, function($message) use ($data)
{
$message->subject('New Order: '.$data['subject'])
->from('myemail#myserver.com')
->to($data['email']);
});
}
I saw that you get the $data from request object
$data = $request->only('subject', 'email', 'randomid');
but randomid was generated in created method
$order = $user->orders()->create([
'randomid' => rand(100000,999999),
'subject' => $request->get('subject'),
'email' => $request->get('email'),
]);
So there is not randomid in request.
I think you should get $data from $order like the folowing:
$data = $order->toArray();
So You will have:
public function store(Request $request)
{
$order = $user->orders()->create([
'randomid' => rand(100000,999999),
'subject' => $request->get('subject'),
'email' => $request->get('email'),
]);
$data = $order->toArray();
Mail::send('emails.note',
$data
, function($message) use ($data)
{
$message->subject('New Order: '.$data['subject'])
->from('myemail#myserver.com')
->to($data['email']);
});
}
The randomid doesn't come from the request. You generate its value manually using rand(100000,999999) !
Please try this:
public function store(Request $request){
$data = [
'randomid' => rand(100000, 999999) ,
'subject' => $request->input('subject') ,
'email' => $request->input('email')
];
$order = $user->orders()->create($data);
Mail::send('emails.note', $data, function ($message) use($data) {
$message->subject('New Order: ' . $data['subject'])
->from('myemail#myserver.com')->to($data['email']);
});
}

Laravel 4 Auth::attempt() issue

I'm trying the Laravel's Auth class but the method returns false always. Here's my code:
Controller :
public function postLogin()
{
// Declare the rules for the form validation.
//
$rules = array(
'email' => 'Required|Email',
'password' => 'Required'
);
// Get all the inputs.
//
$email = Input::get('email');
$password = Input::get('password');
// Validate the inputs.
//
$validator = Validator::make(Input::all(), $rules);
// Check if the form validates with success.
//
if ($validator->passes())
{
//echo $password; displays test
// Try to log the user in.
//
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
// Redirect to the users page.
//
return Redirect::to('account')->with('success', 'You have logged in successfully');
}
else
{
// Redirect to the login page.
//
return Redirect::to('account/login')->with('error', 'Email/password invalid.');
}
}
// Something went wrong.
//
return Redirect::to('account/login')->withErrors($validator->getMessageBag());
}
Seeder.php
public function run()
{
DB::table('users')->delete();
$users = array(
array(
'email' => 'test#test.com',
'password' => Hash::make('test'),
'first_name' => 'John',
'last_name' => 'Doe',
'created_at' => new DateTime,
'updated_at' => new DateTime,
)
);
DB::table('users')->insert( $users );
}
It will be because of framework bug. So try to update it.
composer update
Or
php composer.phar update
In your config/auth.php file
try changing from 'driver' => 'eloquent' to 'driver' => 'database'.

Categories