I'm trying to send an email through Laravel from a form that has two different file inputs with different names, cv and cover_letter.
The build function works just fine but it only attaches one file when it sends the email, I'd like it to send both.
I've tried this solution here on SO but no luck sending both attachments either.
public function build(Request $request)
{
$today = Carbon::now()->format('Y-m-d');
return $this->from([
'email' => $request->email,
'name' => $request->name
])
->to( 'jobs#domain.com' )
->subject( New job application '.$request->name.' for the position of '.$request->role.'.')
->view('emails.jobsform')
->with([
'name' => $request->name,
'tel' => $request->tel,
'email' => $request->email,
'role' => $request->role,
'location' => $request->location,
'call_code' => $request->call_code,
])
->attach(
$request->cv, [
'as' => $today.".".$request->name.".".$request->role.'.pdf',
'mime' => 'application/pdf'],
$request->cover_letter, [
'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf',
'mime' => 'application/pdf']
);
}
Hope someone can help, thanks.
In example, he use attach with foreach(), it means that for new file he has new attach, and u want to attach all files in one attach(). Try this
->attach(
$request->cv, [
'as' => $today.".".$request->name.".".$request->role.'.pdf',
'mime' => 'application/pdf'])
->attach(
$request->cover_letter, [
'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf',
'mime' => 'application/pdf']
);
Related
I'm still new to laravel and I have a simple app and aSo I have a route that will store data based on the request in my controller.
public funtion store(Request $request, $id){
if ($request->has('work_experiences')) {
WorkExperience::create([
'user_id' => $user->id,
'position' => $request->work_experiences['position'],
'company' => $request->work_experiences['company'],
'start_date' => $request->work_experiences['start_date'],
'end_date' => $request->work_experiences['end_date'],
]);
}
if ($request->has('education')) {
Education::create([
'user_id' => $user->id,
'degree' => $request->education['degree'],
'university' => $request->education['university'],
'start_date' => $request->education['start_date'],
'end_date' => $request->education['end_date'],
]);
}
if ($request->has('job_interests')) {
JobInterest::create([
'user_id' => $user->id,
'job_position' => $request->job_interests['position'],
]);
}}
}
and in my test
public function test_authenticated_user_can_edit_education_profile()
{
$this->withoutExceptionHandling();
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->post('/candidate' . '/' . $user->id, [
'user_id' => $user->id,
'position' => 'position',
'company' => 'company',
'start_date' => Carbon::now(),
'end_date' => Carbon::now(),
]);
$this->assertCount(1, WorkExperience::all());
}
when I run the test, the assertCount seems to fail because the response didn't work/insert the data to DB. where do I do wrong?
Well, the test is right.
It should fail because there is no work_experiences key in your request data.
The test request should look like:
$response = $this->post('/candidate' . '/' . $user->id, [
'work_experiences' => [
'user_id' => $user->id,
'position' => 'position',
'company' => 'company',
'start_date' => Carbon::now(),
'end_date' => Carbon::now(),
]
]);
So your data should go under a work_experiences key such that $request->has('work_experiences') returns true and executes the WorkExperience::create() statement.
Currently your endpoint only allows for a single "work experience" to be created. Seeing that you've named it work_experiences I assume you'd want to pass in an array/collection of "work experiences" - but that won't work with the current implementation; you'll have to loop over them instead - something like this:
if ($request->has('work_experiences')) {
foreach ($request->input('work_experiences') as $experience) {
WorkExperience::create([
'user_id' => $request->user()->id,
'position' => $experience['position'],
'company' => $experience['company'],
'start_date' => $experience['start_date'],
'end_date' => $experience['end_date'],
]);
}
}
And then your test should look something like this:
$response = $this->post('/candidate' . '/' . $user->id, [
'work_experiences' => [
[
'user_id' => $user->id,
'position' => 'position',
'company' => 'company',
'start_date' => Carbon::now(),
'end_date' => Carbon::now(),
],
// more "work experiences"
]
]);
i have a problem this controller is not working how can i do? should send mutiple emails how do i solve?
I don't know how to handle it
function submit(Request $request) {
$this->validate($request, [
'email' => 'required|email',
'file' => 'mimes:pdf,doc,docx'
]);
$data = array(
'name' => $request->name,
'cognome' => $request->cognome,
'luogo' => $request->luogo,
'date' => $request->date,
'telefono' => $request->telefono,
'email' => $request->email,
'citta' => $request->citta,
'provincia' => $request->provincia,
'studio' => $request->studio,
'lingua' => $request->lingua,
'livello' => $request->livello,
'lingua2' => $request->lingua2,
'livello2' => $request->livello2,
'file' => $request->file,
'agree' => $request->agree
);
Mail::send('mail', $data, function($message) use ($request,$data){
$message->to('luis#gmail.com', 'luis')->subject('Send mail ' . $request->name);
$message->from($request->email, $request->name);
if($request->hasFile('file')){
$message->attach($request->file('file')->getRealPath(), array(
'as' => $request->file('file')->getClientOriginalName(),
'mime' => $request->file('file')->getMimeType())
);
}
});
Session::flash('success', 'Mail spedita con sucesso');
}
I wish I could solve the problem
any advice? on how to do it?
Let me show you my code, and place comments for you guys to better understand:
$homework = new Homework([ // I create Homework (And I indeed want to get the ID of the one that was just created).
'subject_id' => $request->subject_id,
'user_id' => auth()->user()->id,
'title' => $request->name,
'image' => $path,
'progress' => $request->progress,
'description' => $request->description,
'duedate' => $request->date
]);
$homework->save(); // I save it
$homeworkid = Homework::where('id', $id)->first(); // I try to retrieve it, but I'm not sure how to get it as I need to define `$id`.
$progress = newProgress([
'user_id' => auth()->user()->id,
'homework_id' => $homeworkid, // I need this for the relationship to work.
'title' => 'Initial Progress',
'description' => 'This progress is auto-generated when you create an assignment',
'username' => auth()->user()->name,
'progress' => $homeworkid->progress
]);
$progress->save(); // I save the progress
Well, as you saw, I'm trying to retrieve the ID of Homework right after it was created, but I'm not sure how to define $id in order to get it.
There is no need to instantiate a new model and saving it if your are not doing anything between instantiating and saving, you can use the create method instead:
$homework = Homework::create([
'subject_id' => $request->subject_id,
'user_id' => auth()->user()->id,
'title' => $request->name,
'image' => $path,
'progress' => $request->progress,
'description' => $request->description,
'duedate' => $request->date
]);
$homework->id; // get the id
After saving / creating the model you can access the id like you normally would:
$homework->id
What you then could do is setup the relationships between your models so you can do the following after creating a new homework:
$homework->newProgress()->create([
'user_id' => auth()->user()->id,
'title' => 'Initial Progress',
'description' => 'This progress is auto-generated when you create an assignment',
'username' => auth()->user()->name,
'progress' => $homework->progress
]);
This way you don't have to pass the homework id when creating a new newProgress, laravel will pass it automatically for you.
This is very simple for you. No need to complex it.
$homework->save(); // I save it
After this line just use only
$progress = newProgress([
'user_id' => auth()->user()->id,
'homework_id' => $homework->id, // I need this for the relationship to work.
'title' => 'Initial Progress',
'description' => 'This progress is auto-generated when you create an assignment',
'username' => auth()->user()->name,
'progress' => $homework->progress
]);
You don't no need this line of code
$homeworkid = Homework::where('id', $id)->first(); // I try to retrieve it, but I'm not sure how to get it as I need to define `$id`.
$data = $homework->save();
Get the ID this way: $data->id
I need to pass some variables to my sending email.
Here is my code:
$data = [
'first' => $request->first,
'last' => $request->last,
'business_org' => $request->business_org,
'instagram' => $request->instagram,
'email' => $request->email,
'phone' => $request->phone,
'unique' => $request->unique,
'purchased' => $request->products_purchased,
'city' => $request->city,
'state' => $request->state,
'filename' => $fileName
];
// send email with details
Mail::send('emails.justshoot', $data, function($message) {
$message->from('us#something.com', 'Just Shoot Upload');
$message->to('myemail#gmail.com')->cc('myemail#gmail.com');
});
I then attempt to access the variable so I can display it in my email.
emails.justshoot.blade.php
{{$data}} gives an error. What am I doing wrong?
You are totally fine with what you are doing, but the second param is the data to pass to the view, and as with the with([]) method to call the view, the array passed will generate an object for each entry, and so with what you are doing you are generating $first, $last, $business_org and the $data is just the name of the array, so it isn't been passed as element to the view: if you want this, you should pass [$data] to the mail send :
$data = [
'first' => $request->first,
'last' => $request->last,
'business_org' => $request->business_org,
'instagram' => $request->instagram,
'email' => $request->email,
'phone' => $request->phone,
'unique' => $request->unique,
'purchased' => $request->products_purchased,
'city' => $request->city,
'state' => $request->state,
'filename' => $fileName
];
// send email with details
Mail::send('emails.justshoot', [$data], function($message) {
$message->from('us#something.com', 'Just Shoot Upload');
$message->to('myemail#gmail.com')->cc('myemail#gmail.com');
});
and then in the view you can do {{$data}}
TIPS: You should create a mail with php artisan and then you are able to do whatever you want, in a more elegant way
I am running some basic validation inside a Laravel 5.5 controller like this...
$this->validate($request, [
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'required',
]);
Is there a way to check if 'mykey' matches a php string I have saved? I know I can do an if statement and compare them but wondered if there was a way I could do this inside the validation itself?
You can use in rule, This works for n number of values
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => [
Rule::in([env('MY_KEY'),config('app.another_key')]),
]
]);
Laravel provides a regex option for validation. Depending on the complexity of the string comparison it may be useful:
https://laravel.com/docs/5.5/validation#rule-regex
You can this rule:
$key = "my_saved_key"
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'in:' . $key,
]
]);