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

115
README.md
View File

@@ -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
<?php
// 設定を初期化
require_once '/etc/smtp-config/php-smtp-config.php';
SmtpConfig::init();
// デフォルトの送信元で送信
$to = 'user@example.com';
$subject = 'テストメール';
$body = 'このメールはSMTP経由で送信されています。';
if (mail($to, $subject, $body)) {
echo 'メール送信成功';
} else {
echo 'メール送信失敗';
}
// 異なる送信元で上書き指定
$headers = [
'From: custom@your-domain.com',
'Return-Path: custom@your-domain.com',
];
SmtpConfig::mail($to, $subject, $body, $headers, 'custom@your-domain.com');
```
**デプロイ手順**:
```bash
# 1. Secretにパスワードを設定して展開
helm install my-app cafepieters/phpfpm \
-f values.yaml \
--set smtp.auth.password='your-secure-password'
# または、既存のSecretから取得
helm install my-app cafepieters/phpfpm \
-f values.yaml \
--set smtp.auth.password=$(kubectl get secret email-password -o jsonpath='{.data.password}' | base64 -d)
```
**設定詳細**:
- **protocol**:
- `auto`: 自動判定(推奨)
- `starttls`: SMTP + STARTTLSポート587
- `tls`: SSL/TLSポート465
- **from**: 固定の送信元アドレス
- PHPコードで `$fromAddress` パラメータを指定することで上書き可能
- `mail($to, $subject, $body, $headers, '-f custom@example.com')` で指定
- **認証**: PLAIN認証に対応ほとんどのSMTPサーバーで利用可能
**よくある設定**:
| プロバイダ | ホスト | ポート | プロトコル |
|-----------|-------|--------|----------|
| Gmail | smtp.gmail.com | 587 | starttls |
| Office365 | smtp.office365.com | 587 | starttls |
| Sendgrid | smtp.sendgrid.net | 587 | starttls |
| Amazon SES | email-smtp.region.amazonaws.com | 587 | starttls |
## アップグレード
```bash