Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate Laravel MailHog capture configuration #4822

Merged

Conversation

tyler36
Copy link
Collaborator

@tyler36 tyler36 commented Apr 17, 2023

The Issue

Laravel requires additional configuration to work with DDEV's Mailhog.

With current setup, sending an email results in the following exception:

   Symfony\Component\Mailer\Exception\TransportException  Connection could not be established with host "mailpit:1025": stream_socket_client(): php_network_getaddresses: getaddrinfo for mailpit failed: Temporary failure in name resolution.

How This PR Solves The Issue

This PR add MAIL_HOST=127.0.0.1 to the project's .env.

Manual Testing Instructions

  1. Follow quickstart guide for a Laravel project. You should see the Laravel "Welcome page"

  2. Launch Mailhog to watch for email:

    ddev launch -m
  3. Enter "tinker mode" by typing the following command:

    ddev artisan tinker
  4. Copy and past (or type) the following command into "tinker mode". This will attempt to send an email which should be caught by Mailhog.:

    Illuminate\Support\Facades\Mail::raw('Hello World!', function($msg) {$msg->to('example@example.com')->subject('Test Email'); });
  5. Type exit to exit "tinker mode".

Note: if you make changes to .env (or any php file), you need to exit and re-enter "tinker mode" for changes to apply.

Related Issue Link(s)

Fixes #4821

Release/Deployment Notes

Recent versions of DDEV automate the database settings, so it seems consistent to include the mail server.

Developers are free to opt out of DDEV managing this using currently documented best practises.

@tyler36 tyler36 requested a review from a team as a code owner April 17, 2023 00:36
@github-actions
Copy link

github-actions bot commented Apr 17, 2023

@@ -56,6 +56,7 @@ func laravelPostStartAction(app *DdevApp) error {
"DB_USERNAME": "db",
"DB_PASSWORD": "db",
"DB_CONNECTION": dbConnection,
"MAIL_HOST": "127.0.0.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it need the port as well, 1025?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The port is set to 1025 by default.

Composer copies over the .env.example which has the following mail related settings:

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

You can see that the default mail host doesn't work OOTB.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update PR to include port if you would prefer it in

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1025 is an unusual default mail port (25 is normal). Oh, I see, it's already there MAIL_PORT=1025. But we didn't have MAIL_HOST set right. Thanks.

@rfay
Copy link
Member

rfay commented Apr 17, 2023

It doesn't look like the "send email" instructions are adequate for a novice like me to manually test. Can you add more detail about how to do it? Thanks!

@tyler36
Copy link
Collaborator Author

tyler36 commented Apr 17, 2023

I updated the description to try and make it simpler. As long as you have a working Laravel install, you should be able to enter "tinker mode" (REPL). REPL allows you to run PHP commands in a bootstraped environment.

The command in the description tries to send an email; it it should be a simple copy/paste.

Let me know if you are getting a specific error message.

@tyler36
Copy link
Collaborator Author

tyler36 commented Apr 17, 2023

I have a ddev artisan command in my global commands. It has the #ddev-generated so I assume it's in the standard install right?

@rfay
Copy link
Member

rfay commented Apr 17, 2023

Yes, ddev artisan is provided as an official command for projects of type laravel, see https://ddev.readthedocs.io/en/latest/users/usage/cli/#lots-of-other-commands

@rfay
Copy link
Member

rfay commented Apr 17, 2023

That also means the quickstart could use it instead of ddev exec

@rfay
Copy link
Member

rfay commented Apr 17, 2023

@rfay rfay changed the title Automate Laravel mail config Automate Laravel MailHog capture configuration Apr 17, 2023
@rfay
Copy link
Member

rfay commented Apr 17, 2023

Thanks for tinker mode! Let me know when this is ready for review.

@tyler36 tyler36 requested a review from rfay April 17, 2023 23:44
@tyler36 tyler36 force-pushed the tyler36/20230417_automate_laravel_mail_config branch from d649d53 to 84d8fda Compare April 18, 2023 00:11
@rfay
Copy link
Member

rfay commented Apr 19, 2023

Sorry I haven't tested yet, will get to it!

Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works perfectly, code is good, thanks so much. thanks for the good testing instructions.

@rfay rfay merged commit e7ac10c into ddev:master Apr 20, 2023
14 of 21 checks passed
@tyler36 tyler36 deleted the tyler36/20230417_automate_laravel_mail_config branch May 16, 2023 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Laravel requires manually setting mail
2 participants