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

Can't use mailhog through php mail function #999

Closed
g4b0 opened this issue Jun 5, 2017 · 11 comments
Closed

Can't use mailhog through php mail function #999

g4b0 opened this issue Jun 5, 2017 · 11 comments

Comments

@g4b0
Copy link

g4b0 commented Jun 5, 2017

Info:

  • Docker version ($ docker --version): 17.03.1-ce, build c6d412e
  • Laradock commit ($ git rev-parse HEAD): 5899596
  • System info (Mac, PC, Linux): Linux
  • System info disto/version: Debian Jessie

Issue:

What seems to be going wrong?

I'm working on a project that use php mail() to send email and I'm not able to send mail through mailhog.


Expected behavior:

What should be happening instead?

No email is sent, and the following appear in php-fpm logs:

php-fpm_1 | [05-Jun-2017 07:03:11] WARNING: [pool www] child 7 said into stderr: "sh: 1: -t: not found"
php-fpm_1 | 172.21.0.6 - 05/Jun/2017:07:03:11 +0000 "GET /email.php" 200


Reproduce:

How might we be able to reproduce the error?

Just run the following script


Relevant Code:

<?php 
    ini_set( 'display_errors', 1 );
    error_reporting( E_ALL );
    $from = "emailtest@YOURDOMAIN";
    $to = "YOUREMAILADDRESS";
    $subject = "PHP Mail Test script";
    $message = "This is a test to check the PHP Mail functionality";
    $headers = "From:" . $from;
    mail($to,$subject,$message, $headers);
    echo "Test email sent";
@devployment
Copy link

Just encountered the same issue. My host machine is Windows 10. A workmate running the same code on a Mac as I do does not have this issue.

@jasperf
Copy link
Contributor

jasperf commented Dec 1, 2018

Issue I had running the mentioned test above inside the workspace using tinker was sh: 1: /usr/sbin/sendmail: not found So it had to work from php-fpm. Decided to just run the script form the public folder.

Did set

MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

In .env and with port open I can check Mailhog at http://laravel.test:8025/# but using a simple test script I do not see an email arriving in the Mailhog interface.

In the logs I saw:

$ docker-compose logs php-fpm
Attaching to smt-docker_php-fpm_1_b36031ec647e
php-fpm_1_b36031ec647e | [01-Dec-2018 02:02:59] NOTICE: fpm is running, pid 1
php-fpm_1_b36031ec647e | [01-Dec-2018 02:02:59] NOTICE: ready to handle connections
php-fpm_1_b36031ec647e | [01-Dec-2018 02:03:26] WARNING: [pool www] child 8 said into stderr: "sh: 1: -t: not found"
php-fpm_1_b36031ec647e | 172.25.0.9 -  01/Dec/2018:02:03:26 +0000 "GET /mailtest.php" 200
php-fpm_1_b36031ec647e | [01-Dec-2018 02:07:09] WARNING: [pool www] child 9 said into stderr: "sh: 1: -t: not found"
php-fpm_1_b36031ec647e | 172.25.0.9 -  01/Dec/2018:02:07:09 +0000 "GET /mailtest.php" 200

Found out at https://github.com/ilyasotkov/docker-php-msmtp that

the official PHP repository on Docker Hub doesn't have any MTA (Mail Transfer Agent) installed, meaning that the mail() PHP function won't work and cause this peculiar error:

WARNING: [pool www] child 6 said into stderr: "sh: 1: -t: not found"

The Docker README however states you can use Mailhog Sendmail:

.. in PHP you could add either of these lines to php.ini:

sendmail_path = /usr/local/bin/mhsendmail
sendmail_path = /usr/sbin/sendmail -S mail:1025

Neither are to be located inside the php-fpm container. In the Mailhog container it is though under /usr/sbin/sendmail.

Changing the sendmail path in php-fpm/php7.2.ini to sendmail_path = /usr/sbin/sendmail -S mail:1025 did not work though. Then I got:

php-fpm_1_ab46c8ff1467 | [01-Dec-2018 03:38:28] WARNING: [pool www] child 7 said into stderr: "sh: 1: /usr/sbin/sendmail: not found"
php-fpm_1_ab46c8ff1467 | 172.30.0.9 -  01/Dec/2018:03:38:28 +0000 "GET /mailtest.php" 200

so the sendmail needs to be inside php-fpm it seems.

Update:

Then I read at mailhog/MailHog#99 that mail in the earlier mentioned sendmail line is the alias for the container so I tried sendmail_path = /usr/sbin/sendmail -S mailhog:1025 No joy yet either..

@drizzersilverberg
Copy link

hello, @g4b0 @devployment @jasperf . I have the same sh: 1: /usr/sbin/sendmail: not found problem. Do you have resolved this problem? Either using Mailtrap or Mailhog, I still got the issue.

@bestlong
Copy link
Member

try PHPMailer use SMTP.

@dboune
Copy link

dboune commented Jul 18, 2019

I needed to have this for a legacy project which I can not change to use a proper mailer.

The following linked thread and comment resolved the issue for me, and I'm now able to use MailDev with the standard php mail() function. With some adaptation it could support whatever reasonable solution is desired.

I did not dig too deeply into the specific configuration, and only followed the author's instruction. It worked. I'm currently unaware of any side effects.

maildev/maildev#115 (comment)

  • In php-fpm/Dockerfile
###########################################################################
# SENDMAIL:
###########################################################################
ARG INSTALL_SENDMAIL=false

RUN if [ ${INSTALL_SENDMAIL} = true ]; then \
    apt-get update && apt-get -q -y install ssmtp mailutils \
    && rm -rf /var/lib/apt/lists/* \
    && echo "hostname=localhost.localdomain" > /etc/ssmtp/ssmtp.conf \
    && echo "root=<your-email>" >> /etc/ssmtp/ssmtp.conf \
    && echo "mailhub=maildev" >> /etc/ssmtp/ssmtp.conf \
    && echo "sendmail_path=sendmail -i -t" >> /usr/local/etc/php/conf.d/php-sendmail.ini \
    && echo "localhost localhost.localdomain" >> /etc/hosts \
;fi

Note for anyone attempting this on their own - You have to follow the existing pattern for INSTALL_* variables in .env and docker-compose.yml, or remove the conditional.

Would be better to abstract the variables.

Apologies for the lack of PR.

@stale
Copy link

stale bot commented Feb 2, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Stale label Feb 2, 2020
@dboune
Copy link

dboune commented Feb 2, 2020

This should probably not be closed.

@stale stale bot removed the Stale label Feb 2, 2020
@jasperf
Copy link
Contributor

jasperf commented Feb 2, 2020

Well, perhaps you should make this a PR after all @dboune . And one with variables added for localdomain, <your-email>. That way this could help others and me in the future.

@dboune
Copy link

dboune commented Feb 2, 2020

@jasperf I don’t disagree. However I’m no longer using laradock at the moment (though I’d like to be), and unfortunately this reminder comes right as I’m neck deep. If I can come back to it, I will do so.

@stale
Copy link

stale bot commented May 2, 2020

Hi 👋 this issue has been automatically marked as stale 📌 because it has not had recent activity 😴. It will be closed if no further activity occurs. Thank you for your contributions ❤️.

@stale stale bot added the Stale label May 2, 2020
@stale
Copy link

stale bot commented May 23, 2020

Hi again 👋 we would like to inform you that this issue has been automatically closed 🔒 because it had not recent activity during the stale period. We really really appreciate your contributions, and looking forward for more in the future 🎈.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants