Skip to content

Instantly share code, notes, and snippets.

@orlando
Created April 19, 2018 02:09
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save orlando/42883f9ed188e45817c50359bc3fa680 to your computer and use it in GitHub Desktop.
Save orlando/42883f9ed188e45817c50359bc3fa680 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"]
@Avataar120
Copy link

Great solution ! Thx !

@Zaimeth
Copy link

Zaimeth commented Dec 10, 2020

this is work sir? btw, thx 👍

@orlando
Copy link
Author

orlando commented Dec 10, 2020

@Zaimeth I haven’t use it in a while now. Let me know if it works for you

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