Add Docker setup with SMTP mail and logging

This commit is contained in:
2025-12-10 20:50:15 -08:00
parent c3a37187bf
commit 5ab3eb6039
4 changed files with 77 additions and 3 deletions

13
.dockerignore Normal file
View File

@@ -0,0 +1,13 @@
.git
.gitignore
Dockerfile
*.swp
*.swo
*.log
node_modules
**/node_modules
tmp
temp
*.zip
*.tar.gz
*.tgz

34
Dockerfile Normal file
View File

@@ -0,0 +1,34 @@
FROM php:8.2-apache
# Install lightweight SMTP client for PHP mail()
RUN apt-get update \
&& apt-get install -y --no-install-recommends msmtp ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Configure PHP to use msmtp as sendmail
RUN echo "sendmail_path = /usr/sbin/msmtp -t -i" > /usr/local/etc/php/conf.d/sendmail.ini
# Emit structured access logs with latency to stdout
RUN set -eux; \
{ \
echo 'LogFormat "ts=%{%Y-%m-%dT%H:%M:%S%z}t client=%a forwardedfor=\\"%{X-Forwarded-For}i\\" host=%v method=%m uri=%U query=\\"%q\\" protocol=%H status=%>s bytes=%O referer=\\"%{Referer}i\\" agent=\\"%{User-Agent}i\\" response_time_us=%D" logreq'; \
} > /etc/apache2/conf-available/logging.conf \
&& a2enconf logging \
&& a2disconf other-vhosts-access-log \
&& sed -i '/CustomLog /d;/ErrorLog /d' /etc/apache2/sites-available/000-default.conf \
&& { \
echo 'ErrorLog /proc/self/fd/2'; \
echo 'CustomLog /proc/self/fd/1 logreq'; \
} >> /etc/apache2/sites-available/000-default.conf
# Copy site into the Apache web root
COPY . /var/www/html/
# Entrypoint to template msmtp config from env vars, then hand off to stock PHP entrypoint
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["apache2-foreground"]
# Expose HTTP port
EXPOSE 80

26
docker-entrypoint.sh Normal file
View File

@@ -0,0 +1,26 @@
#!/bin/sh
set -e
# If SMTP vars are provided, write an msmtp config for authenticated SMTP
if [ -n "${SMTP_HOST:-}" ]; then
: "${SMTP_PORT:=587}"
: "${SMTP_FROM:=noreply@example.com}"
: "${SMTP_TLS:=on}"
cat > /etc/msmtprc <<EOF
account default
host ${SMTP_HOST}
port ${SMTP_PORT}
from ${SMTP_FROM}
auth on
user ${SMTP_USER:-}
password ${SMTP_PASSWORD:-}
tls ${SMTP_TLS}
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log
EOF
chmod 600 /etc/msmtprc
fi
exec docker-php-entrypoint "$@"

View File

@@ -16,10 +16,11 @@ $phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'yourname@yourdomain.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$to = getenv('MAIL_TO') ? getenv('MAIL_TO') : 'yourname@yourdomain.com'; // Override with MAIL_TO env var
$fromAddress = getenv('MAIL_FROM') ? getenv('MAIL_FROM') : 'noreply@yourdomain.com';
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers = "From: $fromAddress\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;