John Davidson

javascript - LARAVEL: how to send Email data from view to controller

0 comments
Message:


I need to send email But Not getting email address in userController


from add.blade.php to [email protected]


this is the code of add.blade.php file:


<div class="modal-content">
<div class="modal--blocks">
<div class="small--heading">
<form class="col s12">
<div class="input-field">
<div class="chips"></div>

</div>
<div class="input-field-btn">
<button class="btn waves-effect waves-light" type="button" id="send_email">Send Email</button>
</div>
</form>
</div>
</div>
</div>

and this is the scripte in add.blade.php file:


<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="css/materialize/js/materialize.min.js"></script>

$(document).on('click', '#send_email', function(){
var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;

var instance = M.Chips.getInstance($('.chips'));
var email_address = instance.chipsData;
alert(instance);
alert(email_address);
// alert(email_address);
$.each(email_address, function(i, value){
if (!testEmail.test(value.tag)){
instance.deleteChip(i);
}
});
$("#send_email").text("processing..");
$("#send_email").prop('disabled', true);
var artist_id = "{{$artist->artist_id}}";
var request = $.ajax({
url: "{{url('/label/artist/weeklyreport/send')}}",
method: "POST",
data: { _token: $('meta[name="csrf-token"]').attr('content'), email:email_address, artist_uuid:artist_id}
});
request.done(function(resp) {
if (!resp.error) {
if (resp.result.status){
$("#modalEmailTemplates").modal('close');
$("#response_weekly_report").show();
}
}
});
});

userController.php file :


    public function weeklyreportSend(Request $request) {
dd($request->email);die;
// $email = 'shiyalparesh25@gmail.';
$post = [];
$post['email'] = array_column($request->email, 'tag');
$post['subject'] = Config::get('constant.REPLAY_NAME') . "- Weekly Report";
$post['data'] = $this->get_weekly_report_data($request->artist_uuid);

$result = Helper::sendMail($post, "weekly_report");

return response()->json(['error' => false, 'result' => $result]);
}

Problem :- i'm not getting email value in Controller.if any have problem like me Please Help



My Answer:
To send email data from a view to a controller in Laravel, you can use a form in your view to collect the email address and then send it to the controller using a POST request. Here's an example of how you can do this:

1. In your `add.blade.php` view, create a form that collects the email address:

html

@csrf





2. In your `web.php` routes file, define a route that points to a method in your controller:

php
Route::post('/send-email', 'UserController@sendEmail')->name('sendEmail');


3. In your `UserController`, create a method that receives the email address from the form and sends the email:

php
use Illuminate\Support\Facades\Mail;
use App\Mail\SendEmail;

public function sendEmail(Request $request)
{
$email = $request->input('email');

// Send email using Laravel Mail
Mail::to($email)->send(new SendEmail());

return redirect()->back()->with('success', 'Email sent successfully!');
}


4. Create a Mailable class to define the email content in `app/Mail/SendEmail.php`:

php
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendEmail extends Mailable
{
use Queueable, SerializesModels;

public function build()
{
return $this->view('emails.send');
}
}


5. Create a view for the email content in `resources/views/emails/send.blade.php`:

html

This is the email content.




Now, when a user enters an email address in the form in your `add.blade.php` view and submits it, the email address will be sent to the `sendEmail` method in your `UserController`, which will then send an email to that address using Laravel Mail.

Rate this post

3 of 5 based on 5354 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us