Skip to content

Instantly share code, notes, and snippets.

@rumbis
Forked from orlando/Dockerfile
Last active December 22, 2021 00:07
Show Gist options
  • Save rumbis/77b6200f70905d76e55419c9eeb3b8dd to your computer and use it in GitHub Desktop.
Save rumbis/77b6200f70905d76e55419c9eeb3b8dd to your computer and use it in GitHub Desktop.
WordPress Docker SMTP email configuration with environment variables

You need to provide these variables

  • WORDPRESS_SMTP_HOST - SMTP host
  • WORDPRESS_SMTP_PORT - SMTP port
  • WORDPRESS_SMTP_USERNAME - SMTP username
  • WORDPRESS_SMTP_PASSWORD - SMTP password
  • WORDPRESS_SMTP_FROM - address from which emails are sent in wordpress
  • WORDPRESS_SMTP_FROM_NAME - name in wordpress emails

I'm using this with mailgun credentials and works great

#!/bin/bash
# Make sure this file is executable by running chmod +x apache2-config.sh before building your image
cat >> /var/www/html/wp-config.php <<-EOF
/* SMTP Settings */
add_action( 'phpmailer_init', 'mail_smtp' );
function mail_smtp( \$phpmailer ) {
\$phpmailer->isSMTP();
\$phpmailer->Host = getenv('WORDPRESS_SMTP_HOST');
\$phpmailer->SMTPAutoTLS = true;
\$phpmailer->SMTPAuth = true;
\$phpmailer->Port = getenv('WORDPRESS_SMTP_PORT');
\$phpmailer->Username = getenv('WORDPRESS_SMTP_USERNAME');
\$phpmailer->Password = getenv('WORDPRESS_SMTP_PASSWORD');
// Additional settings
\$phpmailer->SMTPSecure = "tls";
\$phpmailer->From = getenv('WORDPRESS_SMTP_FROM');
\$phpmailer->FromName = getenv('WORDPRESS_SMTP_FROM_NAME');
}
EOF
# Run apache2
exec "apache2-foreground"
FROM wordpress:latest
# Setup SMTP by running apache2-config.sh
COPY ["apache2-config.sh", "/usr/local/bin/"]
CMD ["apache2-config.sh"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment