From 06c63037f17715e1b75b62cdd244be487435b517 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Feb 2026 10:40:07 +0900 Subject: [PATCH] feat: Add SMTP mail sending feature via msmtp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PHPアプリケーションからSMTP経由でメール送信を行う機能を追加。 msmtpをPHP-FPMコンテナに統合し、mail()関数で直接利用可能。 Features: - STARTTLS(port 587)とSSL/TLS(port 465)に対応 - 送信元アドレスは固定だがPHPで上書き指定可能 - パスワードはKubernetes Secretで安全に管理 - 自己署名証明書対応オプション - Gmail、Office365など一般的なSMTPサーバーに対応 Changes: - values.yaml: smtp設定セクションを追加 - templates/secret-smtp.yaml: パスワード管理用Secret - templates/configmap-smtp.yaml: msmtprc設定ファイル生成 - templates/configmap-smtp.yaml: PHPヘルパークラス(SmtpConfig) - templates/deployment.yaml: msmtpインストールと設定 - README.md: SMTP設定パラメータ表と使用例を追加 Protocol support: - auto: 自動判定(推奨) - starttls: SMTP + STARTTLS(ポート587) - tls: SSL/TLS(ポート465) PHP Usage: SmtpConfig::init(); mail($to, $subject, $body); // または別の送信者で上書き SmtpConfig::mail($to, $subject, $body, $headers, 'custom@example.com'); Co-Authored-By: Claude Sonnet 4.5 --- README.md | 115 +++++++++++++++++++++++++ templates/configmap-smtp.yaml | 157 ++++++++++++++++++++++++++++++++++ templates/deployment.yaml | 59 ++++++++++++- templates/secret-smtp.yaml | 13 +++ values.yaml | 43 ++++++++++ 5 files changed, 385 insertions(+), 2 deletions(-) create mode 100644 templates/configmap-smtp.yaml create mode 100644 templates/secret-smtp.yaml diff --git a/README.md b/README.md index b7ab2b8..4b692e2 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,21 @@ kubectl logs -l app.kubernetes.io/name=phpfpm -c nginx | `externalDatabase.username` | ユーザー名 | `user` | | `externalDatabase.password` | パスワード | `pass` | +### SMTP設定 + +| パラメータ | 説明 | デフォルト | +|-----------|------|-----------| +| `smtp.enabled` | SMTP機能有効化 | `false` | +| `smtp.host` | SMTPサーバーホスト名 | `smtp.example.com` | +| `smtp.protocol` | プロトコル(auto/starttls/tls) | `auto` | +| `smtp.port` | ポート番号 | `587` | +| `smtp.auth.enabled` | 認証有効化 | `true` | +| `smtp.auth.username` | SMTPユーザー名 | `smtp-user@example.com` | +| `smtp.auth.password` | SMTPパスワード | `""` (Secretで指定) | +| `smtp.from` | 送信元メールアドレス(固定) | `noreply@example.com` | +| `smtp.tls.verify` | TLS証明書検証 | `true` | +| `smtp.tls.allowSelfSigned` | 自己署名証明書を許可 | `false` | + ### Ingress設定 | パラメータ | 説明 | デフォルト | @@ -544,6 +559,106 @@ helm install production-api cafepieters/phpfpm \ --set externalDatabase.password=$(kubectl get secret db-password -o jsonpath='{.data.password}' | base64 -d) ``` +### 例10: SMTP設定によるメール送信 + +PHPアプリケーションからSMTP経由でメール送信を行う設定です。 + +```yaml +# values.yaml +smtp: + enabled: true + + # Gmail の場合 + host: "smtp.gmail.com" + protocol: "starttls" # または "tls" (port 465) + port: 587 + + # Office365の場合は以下のようにコメント解除 + # host: "smtp.office365.com" + # port: 587 + # protocol: "starttls" + + auth: + enabled: true + username: "your-email@gmail.com" + # password は以下の方法で指定: + # helm install ... --set smtp.auth.password='your-password' + password: "" + + # 送信元アドレス(PHPで上書き可能) + from: "noreply@your-domain.com" + + tls: + verify: true + allowSelfSigned: false + +persistence: + enabled: true +``` + +**PHPでの使用例**: + +```php + ini_get('sendmail_path'), + 'sendmail_from' => ini_get('sendmail_from'), + 'SMTP' => ini_get('SMTP'), + 'smtp_port' => ini_get('smtp_port'), + ]; + } + } +{{- end }} diff --git a/templates/deployment.yaml b/templates/deployment.yaml index 11db36d..23f6c25 100644 --- a/templates/deployment.yaml +++ b/templates/deployment.yaml @@ -51,7 +51,12 @@ spec: # MySQL/MariaDB接続に必要なパッケージ APK_PACKAGES="$APK_PACKAGES mysql-client mysql-dev" {{- end }} - + + {{- if .Values.smtp.enabled }} + # SMTP送信に必要なパッケージ + APK_PACKAGES="$APK_PACKAGES msmtp ca-certificates" + {{- end }} + {{- if .Values.composer.additionalApkPackages }} # ユーザー指定の追加APKパッケージ {{- range .Values.composer.additionalApkPackages }} @@ -142,7 +147,30 @@ spec: {{- end }} {{- end }} {{- end }} - + + {{- if .Values.smtp.enabled }} + # ======================================== + # SMTP設定(msmtp) + # ======================================== + echo "Configuring SMTP for mail sending..." + + # msmtprc設定ファイルをコピー + cp /etc/smtp-config/msmtprc /etc/msmtprc + chmod 600 /etc/msmtprc + + # PHP設定をphp.ini.d/に作成 + cat > /usr/local/etc/php/conf.d/99-smtp.ini << 'PHP_SMTP_EOF' + ; SMTP設定 - msmtp経由でメール送信 + sendmail_path = "/usr/bin/msmtp -t" + sendmail_from = "{{ .Values.smtp.from }}" + PHP_SMTP_EOF + + echo "SMTP configured:" + echo " Host: {{ .Values.smtp.host }}:{{ .Values.smtp.port }}" + echo " Protocol: {{ .Values.smtp.protocol }}" + echo " From: {{ .Values.smtp.from }}" + {{- end }} + echo "Setup complete, starting PHP-FPM..." php-fpm ports: @@ -155,6 +183,16 @@ spec: - name: composer-config mountPath: /tmp/composer-init {{- end }} + {{- if .Values.smtp.enabled }} + - name: smtp-config + mountPath: /etc/smtp-config + readOnly: true + {{- if .Values.smtp.auth.enabled }} + - name: smtp-secrets + mountPath: /etc/smtp-secrets + readOnly: true + {{- end }} + {{- end }} env: {{- if .Values.externalDatabase.enabled }} - name: DB_HOST @@ -202,4 +240,21 @@ spec: - name: composer-config configMap: name: {{ include "phpfpm.fullname" . }}-composer-config + {{- end }} + {{- if .Values.smtp.enabled }} + - name: smtp-config + configMap: + name: {{ include "phpfpm.fullname" . }}-smtp-config + items: + - key: msmtprc + path: msmtprc + {{- if .Values.smtp.auth.enabled }} + - name: smtp-secrets + secret: + secretName: {{ include "phpfpm.fullname" . }}-smtp + items: + - key: password + path: password + mode: 0600 + {{- end }} {{- end }} \ No newline at end of file diff --git a/templates/secret-smtp.yaml b/templates/secret-smtp.yaml new file mode 100644 index 0000000..b62b37a --- /dev/null +++ b/templates/secret-smtp.yaml @@ -0,0 +1,13 @@ +{{- if .Values.smtp.enabled }} +apiVersion: v1 +kind: Secret +metadata: + name: {{ include "phpfpm.fullname" . }}-smtp + labels: + {{- include "phpfpm.labels" . | nindent 4 }} +type: Opaque +data: + {{- if .Values.smtp.auth.enabled }} + password: {{ .Values.smtp.auth.password | b64enc }} + {{- end }} +{{- end }} diff --git a/values.yaml b/values.yaml index 89db6d6..11f79f5 100644 --- a/values.yaml +++ b/values.yaml @@ -130,4 +130,47 @@ externalDatabase: username: user password: pass +# SMTP設定(PHPのメール送信用) +smtp: + enabled: false + + # SMTPサーバー設定 + host: "smtp.example.com" + + # プロトコルとポート設定 + # - auto: 自動判定(推奨) + # - starttls: STARTTLS(ポート587) + # - tls: SSL/TLS(ポート465) + protocol: "auto" + + # ポート番号(通常は自動判定されるため手動設定は不要) + # protocol: auto の場合は以下の値を参考に + # - 25: SMTP (非暗号化) + # - 587: SMTP + STARTTLS (暗号化) + # - 465: SMTP + SSL/TLS (暗号化) + port: 587 + + # 認証設定 + auth: + enabled: true + username: "smtp-user@example.com" + # password はSecretで管理する + # values-secret.yaml などで以下のように指定: + # smtp: + # auth: + # password: "your-smtp-password" + password: "" + + # 送信元アドレス(固定値) + # PHPプログラムで mail() 関数の $additional_parameters で上書き可能 + # 例: mail($to, $subject, $body, $headers, "-f user@example.com") + from: "noreply@example.com" + + # TLS/SSL設定 + tls: + # 証明書検証を有効化(本番環境では true を推奨) + verify: true + # 自己署名証明書を許可する場合(テスト環境のみ) + allowSelfSigned: false + resources: {} \ No newline at end of file