Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

studentA's avatar

Sending email when button is clicked

after researching Laravel mailables, I have become more confused.

I have a 'complete' button that when clicked, I would like an email to be sent to the logged in user. So far I have: Ensured that the following in Mail.php 'driver' => 'smtp',

'host' => 'smtp.gmail.com',

'port' => 587,

'from' => array('address' => 'mywebappemail.com', 'name' => 'mywebapp Name'),

'encryption' => 'tls',

'username' => 'mywebappemail.com',

'password' => 'mywebappemail.compassword',

'sendmail' => '/usr/sbin/sendmail -bs',

'pretend' => false,

my env file: MAIL_DRIVER=smtp MAIL_HOST=smtp.googlemail.com MAIL_PORT=465 [email protected] MAIL_PASSWORD=mywebappemailpassword MAIL_ENCRYPTION=ssl

I understand that I need a method, however, unsure as to where to put it because I am wanting to send an email when a button is clicked and not when the user has created or stored something.

I've been following a website (https://code.tutsplus.com/tutorials/sending-emails-with-laravel-4-gmail--net-36105) and found the following method however not sure where to put it and how to adapt it for my web app.

Mail::send('folder.view', $data, function($message) { $message->to('registered-user@gmail. com', 'Jon Doe')->subject('Welcome to the Laravel 4 Auth App!'); });

I am really new to Laravel, please be kind.

Thanks in advance.

0 likes
15 replies
MichalOravec's avatar

You can follow this video.

And that tutorial is for Laravel 4, I hope that you use newer version of Laravel, at least 5.8.

1 like
bobbybouwmann's avatar

Well, you already said it right? You want to send the email whenever the user presses the button. The action behind the button is a POST request to one of your routes/controllers. If you want to send the email it should happen in that same controller.

In your case, you replace the Mail action from the tutorial with your own mail action int he same controller.

I suggest you follow this course: http://laravelfromscratch.com/

studentA's avatar

So say the page that my button is on is called example.blade.php and there is a get and a post route to ExampleController. On this example.blade.php I have a form.

I only want an email to be sent when the button this page (not in the form) is clicked.

At the moment my controller looks like this: $example = new Example(); $example->name = request('name'); $example->telephone = request('telephone'); $example->address = request('address'); $example->statement = request('statement'); $example->activity_example_id = DB::table('activity') ->where('activity_user_id', auth()->id()) ->max('activityid');// get the maximum report id for current user. $example->save();

return view('example'); (this is because the user can submit another entry for this form.

so does the following code go in the store method, even though I'm wanting it to relate to the button not the form?: request()->validate(['email' => 'required|email']);

    Mail::to(request('email'))
        ->send (new ContactForm());

my route looks like this: Route::get('/example', 'ExampleController@index'); Route::post('/example', 'ExampleController@store');

Thanks

studentA's avatar

I am using the newest version of Laravel. I am following that video but struggling to determine where the mail method goes as I don't want it to send an email when a form has been submitted. thank you for the link! It's quite helpful

MichalOravec's avatar

For example in jquery

$(document).on('click', 'your-button', function() {
    e.preventDefault();

    $.post('your-url', function(data) {
        // your reponse is in data
    });
});
``
studentA's avatar

i have literally just googled is there is an 'onClick: sendmail' function. I dont really understand what your onclick code does though?

MichalOravec's avatar

You said "I only want an email to be sent when the button this page (not in the form) is clicked."

So it's just example how can you do it in jquery.

You click on the button, you prevent normal behaviour and send in this case post request to your controller where is method to send your email.

studentA's avatar

oh I think I understand, I will see if i can do that

studentA's avatar

I have added the following on to the blade file with the complete button:

$(document).on('click', 'complete', function() { e.preventDefault(); $.post('/app/ContactController', function(data) { }); });

my button has id="complete"

my ContactController says: public function store(Request $request) { request()->validate(['email' => 'required|email']);

    Mail::to(request('email'))
        ->send (new ContactForm());

    return view('/');

my ContactForm says: public function build() { return $this->markdown('emails.complete-form'); }

the complete-form file says: @component('mail::message')

Introduction

The body of your message.

@component('mail::button', ['url' => '']) Button Text @endcomponent

Thanks, {{ config('app.name') }} @endcomponent

An email is not being sent. I presume the url to my controller is wrong?

MichalOravec's avatar
// take your email from your input or where you have it in html

$(document).on('click', '#complete', { email: '[email protected]' }, function() {
    e.preventDefault();

    // first paramater is url adress, in your casy it should be example
    $.post('example', function(data) {
        // your reponse is in data
    });
});
studentA's avatar

I'm so sorry for being thick. The email would be the email address of the user logged in unless that needs to be my webapp email.

the red error symbol showed very quickly when I inspected the page but disappeared straight away so I cant view it.

MichalOravec's avatar

@studenta In your example you have

Mail::to(request('email'))
        ->send (new ContactForm());

So I thought that you post email. It's up to you to figure out your logical problem.

Please or to participate in this conversation.