2026年5月20日
2026年5月20日
WordPressのSendGridでメールを送信する方法
はじめに
SendGridはTwilioが提供するメール配信サービスで、無料プランで月間100通まで送信できます。WordPressと組み合わせることで、確実なトランザクションメール配信が実現します。
事前準備:SendGrid APIキーを取得
1. sendgrid.com でアカウント作成
2. Settings → API Keys → Create API Key
3. 権限: Mail Send のみ選択(最小権限の原則)
4. 生成されたAPIキーをコピー(再表示不可)
解決手順
ステップ1:送信ドメインを認証(重要)
SendGridダッシュボード → Settings → Sender Authentication → Domain Authentication:
- ドメイン: example.com を入力
- 生成されたDNSレコードをドメイン管理画面に追加(CNAME 3件)
- Verify をクリックして認証完了
ステップ2:WP Mail SMTPでSendGridを設定
wp plugin install wp-mail-smtp --activate
管理画面 → WP Mail SMTP → 設定:
- メーラー: SendGrid を選択
- APIキー: 取得したAPIキーを入力
- From Email: 認証済みドメインのメールアドレス
ステップ3:phpmailer_initでSendGrid SMTP設定(コードで設定)
// functions.php — SendGrid SMTP接続
add_action('phpmailer_init', function(PHPMailer\PHPMailer\PHPMailer $phpmailer) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.sendgrid.net';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587;
$phpmailer->Username = 'apikey'; // 固定値
$phpmailer->Password = defined('SENDGRID_API_KEY') ? SENDGRID_API_KEY : '';
$phpmailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$phpmailer->From = 'noreply@example.com';
$phpmailer->FromName = get_bloginfo('name');
});
// wp-config.php
define('SENDGRID_API_KEY', 'SG.xxxxxxxxxxxxxxxxxxxx');
ステップ4:送信テストと確認
# テストメール送信
wp eval "
\$headers = ['Content-Type: text/html; charset=UTF-8'];
\$result = wp_mail(
'test@example.com',
'SendGrid テスト',
'<p>SendGrid経由でメールが送信されました。</p>',
\$headers
);
echo \$result ? '送信成功' : '送信失敗';
"
SendGridダッシュボード → Activity で送信履歴を確認できます。
SendGridの無料プランと制限
| プラン | 月間送信数 | 料金 |
|--------|-----------|------|
| 無料 | 100通/日 | 無料 |
| Essentials | 50,000通/月 | $19.95〜 |
| Pro | 100,000通/月 | $89.95〜 |
注意事項
- APIキーは
wp-config.phpの定数として管理し、絶対にGitリポジトリにコミットしないでください - 送信元ドメインのSPF/DKIMレコードも必ず設定してください(SendGridダッシュボードで確認可能)
- バウンス率が高いと送信者スコアが下がります。メールリストの品質管理を行ってください
まとめ
SendGridをWordPressで使うには、送信ドメインの認証 → APIキー取得 → phpmailer_init フックかWP Mail SMTPプラグインで接続設定の手順を踏んでください。月100通以内なら無料で利用できます。