0

I set up a contact form which sends an email on completion using Laravel notifications, however it doesn't look like anything is being sent.

ROUTES

Route::post('/contact', 'ContactController@store');

CONTROLLER

public function store()
{
    request()->validate([
        'name' => 'required|max:255',
        'email' => 'required|email|unique:contacts|max:255',
        'message' => 'required|max:2000',
    ]);

    $contact = Contact::create(
        request()->only([
            'name',
            'email',
            'message',
        ])
    );

    User::first()->notify(new SendContactNotification($contact));

    return back()->with('success', 'Thank you, I will be in touch as soon as I can');
}

NOTIFICATION

protected $contact;


public function __construct($contact)
{
    $this->contact = $contact;
}

public function toMail($notifiable)
{
    return (new MailMessage)
                ->line($this->contact->name)
                ->line($this->contact->email)
                ->line($this->contact->message);
}

I do get the success message when I run it. However, nothing appears in my Mailtrap. Here's the mail section of the sanitised .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=username
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS='[email protected]'
MAIL_FROM_NAME='admin'

I can't see what I have done wrong. I also tried type hinting the contact in the notification like so:

public function __construct(Contact $contact)
{
    $this->contact = $contact;
}

That didn't work unfortunately. I also thought it might be something to do with my computer not being set up to send emails using php, but I was under the impression that the env file would take care of that.

The contacts are being stored in the database ok, but no emails are being sent. Would anyone be able to help?

6
  • Did you set up a mailtrap.io account ? Commented Aug 27, 2018 at 20:46
  • 1
    type hint the model and what kind of queue driver are u using ? Commented Aug 27, 2018 at 20:46
  • When you go into mailtrap and select the mailbox, it will give you the Username and Password for that mailbox. You are using those in your .env file? They are different from the username and password you'd use when signing into mailtrap.io. Commented Aug 27, 2018 at 20:57
  • @user3158900 yes I just sanitised the file for this question Commented Aug 27, 2018 at 21:07
  • @Andrew yes I did Commented Aug 27, 2018 at 21:08

1 Answer 1

2

It was the port in the env file, I changed it to:

MAIL_PORT=465

and it worked!

I knew port 2525 wasn't working because of this answer: https://stackoverflow.com/a/45418259/5497241

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.