Files
phpfpm/templates/configmap-smtp.yaml
Claude 06c63037f1
All checks were successful
Helm Chart Release / release-chart (push) Successful in 5s
feat: Add SMTP mail sending feature via msmtp
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>
2026-02-24 10:44:12 +09:00

158 lines
4.6 KiB
YAML

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