susanBuck / e15-spring23

0 stars 0 forks source link

Independent Challenge: Connection could not be established with host #32

Closed lmarshman closed 1 year ago

lmarshman commented 1 year ago

I'm working on the independent challenge and have run into an error message that I don't understand, and can't find information on when I try to research it. I've followed the information in the Laravel Mail documentation and am confused where I've gone wrong. The exact error message I am receiving is:

Connection could not be established with host "mailpit:1025": stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution

The error occurs in my BookController in the store function when I try to send the email.

    {
        $request->validate([
            'title' => 'required|max:225',
            'slug' => 'required|unique:books,slug',
            'author_id' => 'required',
            'published_year' => 'required|digits:4',
            'cover_url' => 'required|url',
            'info_url' => 'required|url',
            'purchase_url' => 'required|url',
            'description' => 'required|min:100'
        ]);

        $action = new StoreNewBook((object) $request->all());

        $book = Book::where('slug', '=', $request->slug)->first();

        **Mail::to($request->user())->send(new BookAdded($book));**

        return redirect('/books/create')->with(['flash-alert' => 'The book '.$action->results->title.' was added.']);
    }

I'm not sure if the issue is in my mailable, in my mail configuration file, or the actual Store function noted above. Has anyone else ever run into this error?

susanBuck commented 1 year ago

The error message makes reference to mailpit which is the default email testing tool Laravel comes pre-configured with.

Looking at your .env file, I can see it still has all the default values, including the MAIL_HOST set to mailpit:

MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

You'll need to update these values to reflect the MailTrap settings. As a hint, if you go into your inbox settings in MailTrap and choose Laravel as the integration, it'll give you all the values you need to put in your .env file:

image

If that doesn't sort things out, let me know!

lmarshman commented 1 year ago

Ah! That was the missing piece! Thank you!