Why is Laravel sending mail twice?
Asked Answered
M

5

6

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?

Micrometer answered 11/7, 2017 at 18:26 Comment(3)
public function send(ContactFormRequest $request) is this one post or get ?Obsequent
It's a post route. Route::post('contact-form', 'ContactController@send')->name('contact.form');Micrometer
I just realized that I'm seeing 2 copies of the same email in Mailtrap (for my testing env) because my app is sending using TO and BCC. Mailtrap needs to show 2 copies.Jameejamel
W
3

Ok, found a workaround. When I remove the 'to' entry in the mail.php config file, and hard code the destination address in the Mail::send/queue command instead of getting the destination with config('mail.to'), I get 1 email. If I use config() to define the recipient I get 2 emails. Hope this helps.

Whelm answered 26/8, 2017 at 8:38 Comment(0)
J
9

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

Joanejoanie answered 26/9, 2019 at 14:24 Comment(3)
This is actually what my issue was.Aircondition
I am using aws ses service and I have the same issue. Also, I am not using the bcc method. I haven't found any solution yetDictatorship
yes, right, it's fixed when I commented the bcc lineReceptor
W
3

Ok, found a workaround. When I remove the 'to' entry in the mail.php config file, and hard code the destination address in the Mail::send/queue command instead of getting the destination with config('mail.to'), I get 1 email. If I use config() to define the recipient I get 2 emails. Hope this helps.

Whelm answered 26/8, 2017 at 8:38 Comment(0)
D
1

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.

Disharmonious answered 14/8, 2018 at 2:54 Comment(1)
That would have worked if the issue was a double press of the submit button, but this was actually happening in during the phpunit tests, but its a nice UI response for the user so I'll still give you an upvoteMicrometer
C
0

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.

Coolish answered 8/7, 2020 at 15:8 Comment(1)
Good find which hopefully help some of there unfortunately that wasn't an issue for me, I've had this issue with both Windows and Ubuntu machinesMicrometer
C
0

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.

Coolish answered 9/7, 2020 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.