feat: Add SMTP mail sending feature via msmtp
All checks were successful
Helm Chart Release / release-chart (push) Successful in 5s

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 <noreply@anthropic.com>
This commit is contained in:
2026-02-24 10:40:07 +09:00
parent 8b7a141caa
commit 06c63037f1
5 changed files with 385 additions and 2 deletions

View File

@@ -0,0 +1,157 @@
{{- if .Values.smtp.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "phpfpm.fullname" . }}-smtp-config
labels:
{{- include "phpfpm.labels" . | nindent 4 }}
data:
msmtprc: |
# msmtp設定ファイル
# PHPのsendmail_pathから呼び出される
defaults
{{- if .Values.smtp.tls.verify }}
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
{{- else }}
tls off
{{- end }}
{{- if .Values.smtp.tls.allowSelfSigned }}
tls_certcheck off
{{- else }}
tls_certcheck on
{{- end }}
{{- if eq .Values.smtp.protocol "starttls" }}
# STARTTLS (SMTP + STARTTLS at port 587)
protocol smtp
{{- else if eq .Values.smtp.protocol "tls" }}
# SSL/TLS (SMTPS at port 465)
protocol smtps
{{- else }}
# Auto mode - protocol will be determined by port
{{- if eq (int .Values.smtp.port) 465 }}
protocol smtps
{{- else }}
protocol smtp
{{- end }}
{{- end }}
account default
host {{ .Values.smtp.host }}
port {{ .Values.smtp.port }}
{{- if eq .Values.smtp.protocol "starttls" }}
protocol smtp
{{- else if eq .Values.smtp.protocol "tls" }}
protocol smtps
{{- else if eq (int .Values.smtp.port) 465 }}
protocol smtps
{{- else }}
protocol smtp
{{- end }}
{{- if .Values.smtp.auth.enabled }}
auth on
user {{ .Values.smtp.auth.username }}
passwordeval "cat /etc/smtp-secrets/password"
{{- else }}
auth off
{{- end }}
from {{ .Values.smtp.from }}
{{- if .Values.smtp.tls.verify }}
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
{{- else }}
tls off
{{- end }}
{{- if .Values.smtp.tls.allowSelfSigned }}
tls_certcheck off
{{- else }}
tls_certcheck on
{{- end }}
# PHPスクリプトで利用する設定用PHPクラス
php-smtp-config.php: |
<?php
/**
* PHPからSMTP設定を利用するためのヘルパークラス
* 使用例:
* SmtpConfig::init();
* mail($to, $subject, $body);
*
* // 別の送信者で上書きする場合:
* $headers = [
* 'From: custom@example.com',
* 'Return-Path: custom@example.com',
* ];
* mail($to, $subject, $body, implode("\r\n", $headers), '-f custom@example.com');
*/
class SmtpConfig {
private static $initialized = false;
/**
* SMTP設定を初期化
* PHP-FPMスタートアップ時に呼び出す
*/
public static function init() {
if (self::$initialized) {
return;
}
// PHPメール設定の確認
$sendmailPath = ini_get('sendmail_path');
if (!empty($sendmailPath)) {
error_log("SMTP configured via sendmail_path: " . $sendmailPath);
}
self::$initialized = true;
}
/**
* 指定した送信者でメール送信
*
* @param string $to 受信者アドレス
* @param string $subject メール件名
* @param string $body メール本文
* @param array $headers オプションのヘッダー
* @param string $fromAddress 送信元アドレス(オプション)
* @return bool
*/
public static function mail($to, $subject, $body, $headers = [], $fromAddress = null) {
$headerString = '';
if (!empty($headers)) {
if (is_array($headers)) {
$headerString = implode("\r\n", $headers);
} else {
$headerString = (string)$headers;
}
}
// 送信元アドレスでmsmtpを呼び出す
$parameters = '';
if (!empty($fromAddress)) {
// メールヘッダーにFromを追加
if (!preg_match('/^From:/mi', $headerString)) {
$headerString .= (!empty($headerString) ? "\r\n" : '') . "From: " . $fromAddress;
}
// sendmail互換パラメータで送信者を指定
$parameters = '-f ' . escapeshellarg($fromAddress);
}
return mail($to, $subject, $body, $headerString, $parameters);
}
/**
* PHPのメール設定を取得
*/
public static function getConfig() {
return [
'sendmail_path' => ini_get('sendmail_path'),
'sendmail_from' => ini_get('sendmail_from'),
'SMTP' => ini_get('SMTP'),
'smtp_port' => ini_get('smtp_port'),
];
}
}
{{- end }}

View File

@@ -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 }}

View File

@@ -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 }}