-
Notifications
You must be signed in to change notification settings - Fork 2k
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
mail() does not work out of the box #135
Comments
The docs say this:
Looks like |
I ended up trying to add sendmail or postfix to a container extending from php:5.6-apache and pointing the php.ini sendmail_path to them correctly but ran into the problem of multiple services running inside the container. Ended up settling on using ssmtp to solve this. Here is a Dockerfile that should work with mail() out of the box.
mail() should work out of the box with this container. I'm not sure if this is something that should be modified and changed in the base php container or documented somewhere. Thoughts? |
I don't think it's possible to have a reasonable default configuration for Given that, I think that documenting the issue and possible ways to fix it is the best that can be done. I previously opened an issue for the |
Good points. Seems the most Docker-like way to do this would be to have two containers, the php one and a MTA one, have ssmtp setup in the php one linked to the MTA one. Not the easiest thing in the world to document in a general manner. I'll close this for now since it doesn't seem like there are any actions that can be taken. Thanks for the insight. |
This PHP feature request is relevant (enable direct SMTP support on non-Windows systems), though it doesn't seem like they'll ever implement it: https://bugs.php.net/bug.php?id=29629 |
It took me way too much time to research and empliment. Hope this helps someone else. for simple solution that may be filtered by spam filters: Dockerfile bare-bones example: .config_files/mail_config.sh example: add to your ./config_files/php.ini: Then run the following to initiate it after starting container EDIT May 16, 2019added ability to send as desired domain (see replace MASQUERADE_AS)previous EDIT Feb 10,2017I had another program that was editing the /etc/hosts file at start up, and so I changed my mail_config.sh The change also made it possible to not have to edit /etc/mail/sendmail.mc to remove localhost access thanks to a hint from @charafsalmi. ** /mail_config.sh
|
@stuartz-VernonCo do you know wether there is anything more to do to get it working with docker php-fpm or not ? Everytime I send a mail I get a 504 error. |
@charafsalmi Time out waiting for a response (504) could be an issue with firewall not allowing the connection back in, or on AWS not allowing emails to be sent out on port 25 to prevent spam. Could be a number of things. On AWS, I used a queue on a database for applications sending out email, and smtp the emails through our office365. |
@stuartz-VernonCo thank you ! it took me wayyyy so much time to find out why it wasn't working… it was not about the firewall, it was about what sendmail is expecting us to put in The last line of So, once I changed my It's sad that sendmail doesn't give any clue about this.
|
@charafsalmi You are a lifesaver. |
Just to confirm @charafsalmi all we need to do is run that last line on our docker container? |
@partounian Yes sir. |
It wasn't enough for me. I had to add RUN sed -i 's@local@internet@' /etc/exim4/update-exim4.conf.conf \
&& update-exim4.conf to be able to send emails to external addresses (like gmail). But even after that, I am still getting errors in
It's probably not in the scope of this issue, since |
@m0onspell You just saved my day! I had problems trying to configure Now its working with this commands. RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs exim4
RUN sed -i 's@local@internet@' /etc/exim4/update-exim4.conf.conf \
&& update-exim4.conf
... |
I solved this problem with MSMTP, sendgrid (works for us, you could pick another service, obv) and some environment variables, which I believe is pretty lightweight. I stumbled across this thread and the answers here helped me, so here's what I did for the next person: DOCKERFILE
ENTRYPOINT
|
@stuartz-VernonCo Thanks! Also, if you are having problems if the docker-container is always restarting after doing what he says, just make sure to call your php server again Example, this inside the Dockerfile: |
Well here is my simple solution: Dockerfile (for your custom php image) FROM php:fpm
RUN apt-get update && apt-get install -y sendmail
ADD ./php.sh /opt/php.sh
ADD sendmail.ini /usr/local/etc/php/conf.d/
RUN chmod u+x /opt/php.sh
WORKDIR /opt
CMD ["php.sh"] sendmail.ini # Enable sendmail
sendmail_path = "/usr/sbin/sendmail -t -i" php.sh #! /bin/sh
line=$(head -n 1 /etc/hosts)
line2=$(echo $line | awk '{print $2}')
echo "$line $line2.localdomain" >> /etc/hosts
/etc/init.d/sendmail start
php-fpm Be sure to specify a hostname inside your docker-compose.yml or when running the container, e.g.:
or docker-compose.yml version: '2'
services:
phpfpm:
image: acme/php
container_name: "my_php_fpm"
hostname: "foobar" Thats it - php mail() will work now. It's propably possible with alpine, too - but I did not manage to get that working yet. Feel free to enhance my solution ;) |
I dunno guys. ssmtp didn't work for me out of the box. I get |
Is there any update on it? Issue is still here... |
I am trying to search for a solution for this as the issue is still present |
Here's a simple way to enable PHP to send mail via a SMTP provider. Unless you want to manage your own sendmail service, and worry about the mail sending reputation of your host, etc, this is the way to go. You can replace the Sendgrid details in the example config with the SMTP provider of your choice (if you do, be sure to also update the port number, and note that some cloud providers block outbound port 25). One caveat is that the script will block on the Dockerfile FROM php:5.6-apache
RUN apt-get update && \
apt-get install -y ssmtp && \
apt-get clean
# SSMTP settings
COPY ssmtp.conf /etc/ssmtp/ssmtp.conf
# PHP mail settings
RUN echo 'sendmail_path = "/usr/sbin/ssmtp -t -i"' > /usr/local/etc/php/conf.d/mail.ini ssmtp.conf # Sendgrid setup
# https://sendgrid.com/docs/for-developers/sending-email/ssmtp/
mailhub=smtp.sendgrid.net:587
AuthUser=<username>
AuthPass=<password>
UseSTARTTLS=YES
# Allow the "From" email header.
FromLineOverride=YES |
Hi @WilliamDenniss! Thanks for sharing this solution. Tried myself after following your recipe and for me it's the neatest way to replicate sendmail function in containerized php applications. |
Hey @WilliamDenniss, I used your solution with the official WordPress docker image, and it worked flawlessly. Thanks! |
Hi there. I tried the ssmtp solution as you guys recommended and i got the issue that a testmail is sendable from my host system, but not inside the php:7.3-apache-stretch container. Here are my configs (i replaced my domain with xxx):
Now i try to send a testmail:
As i already mentioned, it works flawless on the hostsystem with the same configfiles:
The version of both ssmtps is 2.64. What's strange is, that it seems that the hostsystem begins with a STARTTLS handshake, and the container don't. Has anybody a hint for me? |
@Nimrod-666 just a guess, but is the port correct in your config? My example used port 587 for sendgrid to avoid the issue of port 25 being blocked on GCP, but you may need to update that with the port number of the SMTP server you are using. Note that some cloud providers block or throttle port 25, so you may need to use a different port if this affects you (as this can result in the container being able to send mail successfully locally, but then not when you deploy it to the cloud). I've updated my original comment with that information. |
I had to update this solution in order to make it work around here. Added these lines to php.sh:
As well as defining an ENTRYPOINT in Dockerfile for php.sh: |
The previous configs didn't work for me due various reasons.
exim4.conf
It's based on rickw/debian-exim-send |
In our setup we've a host with smtp configured to relay to mandrill. We want the containers to leverage this config. The simplest setup we've found is: In the hostiptables rule
postfix:main.cf:
master.cf:
In the Dockerfile
php.ini
We add the user id in the --from for traceability. An alternative is to replace hope this helps someone. |
Hi @charafsalmi ,
I tried this but didn't help. Also, the instruction isn't clear. If I am modifying |
It seems that the sendmail_from config setting isn't right? If additional configuration is required for this to work should this be noted in the documentation?
When running
php -i | grep sendmail_path
I get thisWhen running this script
The results are this:
The text was updated successfully, but these errors were encountered: