Why is Laravel sending mail twice?

Viewed 4582

I built a contact form which is sending two emails instead of one. Here's the controller:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\ContactFormRequest;
use App\Mail\ClientContact;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{
    /**
     * It sends a contact request and returns
     * the user back to the contact form.
     * @param ContactFormRequest $request
     * @return mixed
     */
    public function send(ContactFormRequest $request)
    {
        Mail::send(new ClientContact($request->only(['name','email','message'])));
        session()->flash('success','Your message was sent successfully!');
        return redirect()->back();
    }
}

Here's the ClientContact Mail Class

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ClientContact extends Mailable
{
    use Queueable, SerializesModels;
    /**
     * @var
     */
    public $message;


    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this
            ->from(config('mail.from.address'))
            ->markdown('emails.client.contact', ['message' => $this->message]);
    }
}

It's my testing environment so I'm capturing outgoing mail through Mailtrap.io and the queue is set to sync. I already tried removing the Queueable trait but that didn't change anything as it's not queuing anyway. I also tried putting it on the Queue, and when the queue finally processes the email I again, get two sent emails.

Any ideas?

5 Answers

It's a mailtrap issue, and only if you're using the bcc method.

I recently faced this exact issue. What I did is to add JS functionality (You can also use jQuery or other library) in the form, so whenever you submit the form, all inputs including the button will be disabled/set to readonly.

After using this, there's no more repetition of emails sending. Take a look at this (I'm using jQuery for this):

$(document).ready(function(){
    $('form').on('submit', function(){
        $('.btn').prop('disabled', true);
        $('.btn').html('Sending ...');
        $('input, textarea').prop('readonly', true);
    });
});

The browser is trying to call the post request in your application whenever you submit or click the button in your form, so by using this simple technique, you can disable the ability to send a more than one request in your form.

If your like me and use a Macbook etc (Apple related) you most likely have icloud turned on. This is the issue, icloud backs up and syncs the vendor folder which is a nightmare. In my case, I had 4 mail facades thanks to the wonderful world of OSX.

Turn icloud off for documents (but make a local back up that is not on icloud) and now delete the vendor folder inside your local backup and re-install. And.. your done. vola.

Chrome Extensions issue

Test your post/get/delete requests are not in chrome and if they are sending twice. If they are not sending twice or more in another browser, then check in chromes' incognito mode and if the same issue occurs.

If it does not happen in incognito then you have a faulty chrome plugin or extension that needs removing (not disabled). Please send a fault report to the developer who made the plugin/extension as a developer I am sure they would like to know.

Related