wordpress icon indicating copy to clipboard operation
wordpress copied to clipboard

Email does not work

Open md5 opened this issue 10 years ago • 96 comments

As it currently stands, anything in Wordpress that needs to send email is broken. The wp_mail function that is used for sending out email by default is a thin wrapper over PHP's mail function, which defaults to calling /usr/sbin/sendmail -t -i (cf. http://php.net/manual/en/mail.configuration.php). There are php.ini settings for SMTP, but they are only used on Windows.

For my own work based on this image, I've been installing ssmtp and configuring it to use an external relay host for SMTP, but that's clearly not a turnkey solution for everyone. It also looks like there are many Wordpress plugins that provide SMTP support, but I don't think that installing any of them by default in this image seems reasonable.

Perhaps all of this should just be documented, but I wanted to get it on your radar.

md5 avatar Nov 17 '14 20:11 md5

Yeah... This was a hole we've had on our radar for a long time now, but didn't have a good solution. Perhaps an actual issue will get some brainshare going. We thought about something like ssmtp, but there really is no good solution that'll work for 90% of users out of the box. Email is hard. :(

tianon avatar Nov 17 '14 20:11 tianon

It looks like WP actually uses something called PHPMailer under the hood, which does support SMTP, but it requires a custom hook for configuration: http://codex.wordpress.org/Plugin_API/Action_Reference/phpmailer_init#Examples

md5 avatar Nov 17 '14 20:11 md5

@md5 An alternative is to use a service like mailchimp and Mandrill. I know these are paying services, and not on your own service but the good thing (for Mandrill) is that you can opt out at any moment.

My experience is, your server emails will be probably put in SPAM folder. This is not the case for Mandrill.

omarabid avatar Jan 02 '15 08:01 omarabid

@omarabid using Mailchimp or Mandrill still requires using SMTP, which isn't supported out of the box by Wordpress under Linux. As I mentioned in an earlier comment, it would be possible to add one of the WP plugins that supports SMTP, but I don't think it would be reasonable for a generic Wordpress image to include specific plugins.

In my case, I was using ssmtp to connect to Google Apps with a dedicated mail user. This will avoid spam issues just as well as Mandrill. The case you're talking about is likely the default scenario of a PHP app using the local /usr/sbin/sendmail to send its mail out with no relay host settings. In that case, assuming the IP either isn't listed in the domain's SPF records or the IP is part of a spammy netblock, you will indeed have deliverability problems.

md5 avatar Jan 02 '15 18:01 md5

using Mailchimp or Mandrill still requires using SMTP

No, otherwise, what's the point of using Mandrill.

omarabid avatar Jan 03 '15 12:01 omarabid

Ok, you're right that you can use Mandrill with their REST API instead of SMTP, but that requires installing this plugin or some other plugin that knows how to use that API.

As for the point of using Mandrill with SMTP, there are a ton of reasons you might want to do that, including open tracking, click tracking, deliverability concerns, email volume, etc. Not of those advantages of the Mandrill service rely on using the REST API.

md5 avatar Jan 03 '15 16:01 md5

@md5 I was just suggesting a solution. Obviously, this should be available/fixed for the official Docker repo.

omarabid avatar Jan 03 '15 23:01 omarabid

Wondering how modular you can make the smtp solution> A nice implementation that I've seen is this one https://github.com/catatnight/docker-postfix , but it does bolt on TLS and and OpenDKIM. Perhaps it could be paired down to just postfix? I'm assuming it would only be used via container link so you don't need TLS. DKIM is fall back to avoid DNS shenanigans, admittedly not having it or SSP will probably cause some filters to flag you as spammy.

Thoughts? I think it's time to retreat to the lab :grinning:

booyaa avatar Apr 21 '15 21:04 booyaa

We are using SSMTP to relay to our main mailserver. We have a php sendmail.ini file that contains the single line:

sendmail_path = /usr/sbin/ssmtp -t

We have a sed in the Dockerfile that changes the mailhub, rewritedomain and removes the hostname line from the ssmtp file (this way it gets the container id as the hostname)

RUN sed -i -e 's/mailhub=mail/mailhub=ourmailrelay.domain/' \
    -e 's/#rewriteDomain=/rewriteDomain=ourdomain/' \
    -e '/hostname=/d' \
    /etc/ssmtp/ssmtp.conf

This works really well, and as ssmtp isn't a daemon, it doesn't require any process management

timwsuqld avatar Apr 21 '15 21:04 timwsuqld

@booyaa I don't like that the Postfix container you mention is using supervisord and running its own rsyslog, but that's a moot point probably.

I think the real issue is that PHP is still going to expect a working sendmail-compatible command which will need to be configured to talk to the linked SMTP container (whether it's postfix or some other SMTP server). Getting such a setup working and maintaining it would be error prone.

I still think the best that can be done here without assuming too much is simply to document the options.

md5 avatar Apr 21 '15 21:04 md5

Random musing: this problem would be a great candidate for a mail service docker-compose.yml firing up smtp, imap, spam assassin, clamav and webmail

booyaa avatar Apr 22 '15 06:04 booyaa

@booyaa :+1:

md5 avatar Apr 22 '15 16:04 md5

One good option is to configure SSMTP with ENV variables. This way, you could put your GMAIL or Mandrill cred, and it would work out of the box!

pierreozoux avatar Jun 27 '15 14:06 pierreozoux

If this is an old post, or there has previously been a resolution please disregard.

One quick way of getting mail working inside the container is to install msmtp.

Then, in a configuration file(example: msmtp):

account default
host 172.17.42.1
auto_from on

Copy this to /etc/ inside the container

Update the php.ini or set the sendmail_path variable like so:

sendmail_path = "/usr/bin/msmtp -t -i"

TL;DR Use msmtp to point to the guest machine or some other container.

kcmerrill avatar Jul 25 '15 22:07 kcmerrill

@md5 Any updates on this? I like the new container for SMTP approach. It separates concerns. However, this seems like a WordPress issue for me.

omarabid avatar Jul 28 '15 20:07 omarabid

@omarabid I'm not aware of any developments on this issue other than what you can read in this thread.

md5 avatar Jul 28 '15 21:07 md5

Perhaps since a bunch of you have been able to successfully run ssmtp in the container alongside Wordpress, I thought I'd pose the question here. My install, even after properly configuring /etc/ssmtp/ssmtp.conf with the main SMTP server to relay to still errors out.

Via a simple mail() function test it fails, via wp_mail as well. When I try sendmail from the command line (inside the container) it locks after sending the last <CR>.<CR> and after a timeout period errors with sendmail: Cannot open server-address:port, which matches what I have in the .conf file. I'm using SSL on the other endpoint so wondering if that could play any part in the issue.

robertoandrade avatar Aug 26 '15 14:08 robertoandrade

Have you tried telnet server-address port to see if that hangs as well?

Try something like this:

$ docker run -t --rm debian:jessie 'apt-get update && apt-get install -y telnet && telnet server-address port'

If that hangs too, then the problem is probably being caused by firewall rules on the host (outside the container). This can happen if the server you're trying to contact is on an RFC 1918 private subnet like something under 192.168.0.0/16 and your host thinks it should drop packets from the Docker bridge network.

md5 avatar Aug 26 '15 15:08 md5

Yeah, did it, and it works fine. it responds. so don't think it's networking related.

Anyways ended up ditching ssmtp completely on the container and just configured WordPress to use the external mailer directly via a plugin that intercepts the calls to mail(): http://coffee2code.com/wp-plugins/configure-smtp/

robertoandrade avatar Aug 26 '15 16:08 robertoandrade

That's odd. I haven't seen sendmail time out for reasons other than packet drops, but perhaps the SMTP server was overloaded or something?

It would be good to get to the bottom of the issue if only for the sake of other users who hit problems using ssmtp.

BTW, I should point out that sendmail inside the container will be a symlink to ssmtp if that's all you have installed.

md5 avatar Aug 26 '15 16:08 md5

Ok, just to continue the discussion, here is a working example with ssmtp configured with env variables:

https://github.com/indiehosters/wordpress/

pierreozoux avatar Oct 02 '15 17:10 pierreozoux

So I've managed to come up with a solution that involves another container and a WordPress plugin. The container is running an SMTP client that I wrote (named Hectane) and the WordPress plugin routes all calls to wp_mail() through the container.

Here are the commands to get it up and running:

docker run -d --name db mysql:latest
docker run -d --name mail hectane/hectane:0.2.1
docker run \
    -d -P \
    --link db:db \
    --link mail:mail \
    wordpress:latest

Once the containers are up and running, it's simply a matter of installing and activating this WordPress plugin and changing the hostname to "mail" in "Settings".

WordPress Configuration

Once that's done, all emails will be sent through Hectane and delivered via SMTP.

nathan-osman avatar Oct 04 '15 06:10 nathan-osman

Hi @nathan-osman, is it possible use hectane/hectane container with Drupal 8 ?

joao-parana avatar Nov 15 '15 20:11 joao-parana

I am using SendGrid for emails. It is super easy to set up with one of the many WP plugins and then you will send emails through an API while getting insights on open rate, bounce rate etc.

ahansson89 avatar Dec 17 '15 17:12 ahansson89

I presume you could use anyone of the smtp plugins to get this to work? E.g. https://wordpress.org/plugins/wp-smtp/

geekscrapy avatar Jan 13 '16 09:01 geekscrapy

I'm far from an email expert, but we pulled down this repo some time ago and just set up sendmail to work with it. We're actually looking for another solution which centralizes it so we can spam check and rate limit outgoing email, but we've had the sendmail solution up for the better part of a year on production sites. The trick is that you have to fully qualify your host name, which you can pass in from docker and re-install sendmail.

We've totally rewritten the dockerfile/start.sh to use wp-cli, php 7.0 and getting services working in lieu of supervisord, but here are the tweaks for this repo :

[Dockerfile]
# Sendmail Installation
RUN apt-get -y install sendmail

[Start.sh]
/etc/init.d/sendmail start
supervisord -n

You have to make sure the host name of the docker instance is a FQDN. We use the scheme host-dock-{port}.hostingcompany.com. If you set this through docker -h then it will configure the host file that sendmail requires. If you don't do this then sendmail will time out (10 sec), which is probably the problem mentioned earlier in this thread.

sashman-jaspin avatar Apr 22 '16 15:04 sashman-jaspin

@sashman-jaspin - Thank you. Did just that in the bitnami/php-fpm container and it works just fine.

michelalbers avatar May 01 '16 18:05 michelalbers

Managed to get it working with this setup :

  • install sendmail
  • edit containers's hosts file to add this line : container_IP container_ID container_ID.localdomain
  • Install a wordpress smtp plugin

But i want to do it without the plugin, i think i'm near the solution proposed by @sashman-jaspin ! Problem : it work with the plugin but not without ! while sendmail is working in the container (command line) i just can't make wordpress using it... I think my solution is in the last part of @sashman-jaspin method (maybe i'm wrong) but i don't fully understand it, can someone ELI5 ?

UPDATE With some tricks i managed to make it work without plugin, also updated my blog post about it, (in french) envoyer des emails avec wordpress sur docker.

qsypoq avatar May 15 '16 18:05 qsypoq

Ok, I found an acceptable solution \o/ without anything installed!

The idea is to use a "must use plugin". The advantage is that we don't have to activate it. And it is quiet hidden from the user perspective.

So, this configures SMTP support for WordPress using ENV variables: https://github.com/indiehosters/docker-wordpress/blob/master/run.sh

Should I PR to make this available to everybody?

pierreozoux avatar Jul 14 '16 08:07 pierreozoux

I'm trying with hectane as mentioned by @nathan-osman , but it seems that wp still tryes to send emails with mail(). Anyone else has tryed it? I put the link as suggested and set "mail" as host. Also tryed to put the mail container/service endpoint as host. Nothing works...

alessiovmagri avatar Jul 17 '16 16:07 alessiovmagri