laravel: mailgun sending double emails

Viewed 571

I try mailgun and it works to send an email, but the problem is when I run the function, it sent double email.

the function is like this:

public function sendEmail($id){
    try{
        $user= Users::find($id);
        $data = array('username'=>$user->name, 'email'=>$user->email);
        Mail::send('emails.hapstics', $data, function ($message) use($data){
            $message->to($data['email'], $data['username'])->subject('Test Subject');
        });

        return redirect('leads');
    }
    catch(Exception $e){}
}

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=*hidden
MAIL_PASSWORD=*hidden
MAIL_FROM_NAME=*hidden
MAILGUN_DOMAIN=*hidden
MAILGUN_SECRET=*hidden
2 Answers

You can use this pattern for sending email.

$user = User::findOrFail($id);
Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
                $m->from('hello@app.com', 'Your Application');

                $m->to($user->email, $user->name)->subject('Your Reminder!');
});
Related