Search code examples
laravellaravel-5.3laravel-mail

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?


Solution

  • 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.