0
votes

Currently I'm working on a Laravel Project which has a contact form. When the user fills in the contact form and clicks the action button, the data in the form should be sent to my current email.

I followed a tutorial which showed me the way to send a test mail through mailtrap.io. This worked completely fine as the content from the form got sent to the mailtrap mailbox.

Currently I changed my .env file to details I got on the mailgun dashboard using the sandbox domain which was created from Mailgun itself.

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=THE_USERNAME
MAIL_PASSWORD=THE_PASSWORD
MAIL_ENCRYPTION=tls

I also have installed Guzzle and my controller looks like this

<?php

namespace App\Http\Controllers;

use App\Mail\ContactFormMail; 
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class ContactFormController extends Controller
{
    public function create()
    {
        return view('contact.create');
    }

    public function store()
    {
        $data = request()->validate([
            'name' => 'required',
            'email' => 'required|email',
            'phone' => '',
            'subject' => 'required',
            'message' => 'required'
        ]);

        //dd(request()->all());

        // SEND EMAIL
        Mail::to('studiozalm040@gmail.com')->send(new ContactFormMail($data)); 

        //session()->flash('message', 'Bedankt voor je bericht. We zullen zo snel mogelijk contact met je opnemen.');
        return redirect('http://127.0.0.1:8000#contact')->with('message','Bedankt voor je bericht. We zullen zo snel mogelijk contact met je opnemen.');
    }
}

my ContactFormMail.php code is:

<?php

namespace App\Mail;

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

class ContactFormMail extends Mailable
{
    use Queueable, SerializesModels;

    public $data; 

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.contact.contact-form');
    }
}

Whenever I try to send the contact form, I receive no error or whatsoever. Yet the e-mail doesn't arrive. I must have been doing something wrong or forgot something...

Thank you in Advance.

1
Is there errors in your error-log? After you have ensured that, I would check the activity log in Mailgun to see if the API actually receives your request. If it does, I would check the spam folder and double check the e-mail adress is typed correctly.thephper
It could also be a issue with the config being cached (to old settings) or a mispelling etc. I normally check that by running "php artisan tinker" and then typing "config('mail')" to see the actual settings being consumed by Laravel.thephper
Sorry I comment again: You have typed "Mailgun" as MAIL_DRIVER but are typing the smtp credentials. So I would say that the MAIL_DRIVER should be set to "smtp" instead.thephper

1 Answers

0
votes

You are using the MAIL_DRIVER "mailgun" but are providing the SMTP credentials for mailgun.

If you want to use the mailgun API, you should provide the following environment variables:

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=x
MAILGUN_SECRET=y

If you instead want to use the smtp credentials you should change your MAIL_DRIVER from "mailgun" to "smtp" and then keep the rest of your current environment variabels. Your environment variables would then look like this:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=THE_USERNAME
MAIL_PASSWORD=THE_PASSWORD
MAIL_ENCRYPTION=tls

The smtp configurations happens in config/mail.php but the API configuration (if you want to use the mailgun mail driver) happens in config/services.php