Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Laravel sending mail twice?

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?

like image 681
Azeame Avatar asked Jul 11 '17 18:07

Azeame


Video Answer


1 Answers

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

like image 177
Abdrhmn Avatar answered Oct 09 '22 20:10

Abdrhmn