Home > database >  Why my MailHog doesn't integrate with PHP?
Why my MailHog doesn't integrate with PHP?

Time:08-08

I am setting up the dev environment on the M1 MBP. The PHP and NGINX are installed with the HomeBrew directly on the machine. The ElasticSearch and MailHog - with the Docker.

I am struggling to see the email in the MailHog web interface after I send one through from the PHP file. The project is working and my local PHP and NGINX communicates with the Dockerised ElasticSearch well, so I am sure MailHog is "installed" properly; I see the interface on my web on port 8025 as well.

I've played around with the php.ini file (/opt/homebrew/etc/php/7.4/php.ini). I've double checked that ini file is the one that is used for the project (verified it with changing memory_limit value. I've updated the Postfix (/etc/postfix/main.cf) configuration with the following configuration too:

# For MailHog
myhostname = localhost
relayhost = [localhost]:1025

...and reloaded it like this sudo postfix reload.

My /etc/hosts file has this line: 127.0.0.1 l.month db elasticsearch mail; and pinging them all works too.

When I keep the php.ini file line (containing the sendmail_path) commented out the mail function in PHP script works; but the email is sent directly to my real email address, not the MailHog. If I uncomment it and set it to sendmail_path = /usr/sbin/sendmail -S mail:1025 then the mail function from PHP is returning false. I don't understand why, since this value is advised to be used from this source. However if I use the sendmail_path = /usr/sbin/sendmail -t -i PHP again works, but the MailHog doesn't intercept anything. I've double checked that the real path for the sendmail is /usr/sbin/sendmail by executing the command which sendmail.

I also played around with the SMTP value and the smtp_port (in same ini file); I changed values from localhost to 127.0.0.1 and mail; and the 25 to 1025 respectively. I didn't forget to restart my PHP service after each change either.

Furthermore, I experimented with the postfix configuration values by changing localhost to 127.0.0.1 and mail (as well as reloading it) as well; no luck.

To make sure I tried all of it, I also logged out and logged in to my account to make sure the changes take place; no luck again.

I assume that I do not need the mhsendmail file on my local if I have the docket container running, so I never installed it, hoping it is in the docker container; otherwise, then what's the point of the container after all? If I am wrong with my assumption, please let me know.

Regarding logs, no errors are shown; only this message:

[05-Aug-2022 13:11:24 UTC] mail() on [/Users/[path_here]]src/pub/mail.php:3]: To: [email protected] -- Headers:  -- Subject: Test Subject

FireWall is disabled on the machine.

Please help me solve my problem so that I could see the email send from the PHP in the MailHog web interface. Thanks!

CodePudding user response:

I found the issue. Found because decided to go with the workaround - downloaded the MailHog with the HomeBrew instead of using Docker. But it didn't work! I realised I forgot to download the mhsendmail since I am using the local MailHog.

Then it hit me! I googled this: do i need to install mhsendmail if I use docker. And I got the answer: No, mhsendmail is not included in the mailhog/mailhog docker container. You will need to download mhsendmail (by either curl or go get) in the container that you are planning to send email from.

So apparently I was wrong to assume that MailHog comes with the mhsendmail binary.

It started to work when I downloaded one and updated the postfix config file to:

myhostname = localhost
relayhost = [127.0.0.1]:1025

I didn't even need to touch the php.ini! The sendmail line can stay commented out!

I hope this answer helps others as well!

  • Related