2026年5月31日
2026年5月31日
WordPressのメールをSMTP送信に変更する方法
はじめに
「WordPressからのメールが届かない・迷惑メールに入る」「お問い合わせフォームの通知メールが送信されない」「共有サーバーのPHP mail()が制限されている」——SMTPを設定することでメールの到達率を大幅に改善できます。
症状・原因
WordPressはデフォルトでサーバーのmail()関数を使ってメールを送信します。共有サーバーやVPSではSMTPポートが制限されていたり、送信元IPがブラックリストに登録されていたりして、メールが届かないことがあります。SMTPサーバーを経由することで認証済みの送信者として処理され、到達率が向上します。
解決手順
ステップ1:phpmailer_initフックでSMTPを設定する
// functions.php: プラグインなしでSMTPを設定
add_action( 'phpmailer_init', function( PHPMailer\PHPMailer\PHPMailer $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.gmail.com'; // SMTPサーバー
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587; // TLS: 587 / SSL: 465
$phpmailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS; // TLSの場合
$phpmailer->Username = 'your@gmail.com'; // SMTPユーザー名
$phpmailer->Password = 'your_app_password'; // アプリパスワード(ハードコードしない)
// 送信者情報
$phpmailer->setFrom( 'noreply@example.com', 'サイト名' );
} );
// 認証情報はwp-config.phpで定数として管理する(セキュリティのため)
// wp-config.php:
// define( 'SMTP_HOST', 'smtp.gmail.com' );
// define( 'SMTP_USER', 'your@gmail.com' );
// define( 'SMTP_PASS', 'your_app_password' );
// define( 'SMTP_PORT', 587 );
// functions.phpで定数を参照
add_action( 'phpmailer_init', function( PHPMailer\PHPMailer\PHPMailer $phpmailer ) {
if ( ! defined( 'SMTP_HOST' ) ) return;
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = true;
$phpmailer->Port = defined( 'SMTP_PORT' ) ? SMTP_PORT : 587;
$phpmailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->setFrom( get_option( 'admin_email' ), get_bloginfo( 'name' ) );
} );
ステップ2:Gmailのアプリパスワードを設定する
Gmail SMTPの設定手順:
1. Googleアカウント → セキュリティ → 2段階認証プロセスを有効化
2. Googleアカウント → セキュリティ → アプリパスワード
3. 「アプリを選択」→「その他(カスタム名)」→「WordPress」と入力
4. 生成された16桁のパスワードをSMTP_PASSに設定
SMTPパラメータ(Gmail):
- Host: smtp.gmail.com
- Port: 587(STARTTLS)または 465(SSL)
- SMTPAuth: true
- Username: Gmailアドレス
- Password: アプリパスワード(Googleアカウントのパスワードではない)
ステップ3:SendGrid・Amazon SESを設定する
// 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->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$phpmailer->Username = 'apikey'; // SendGridは固定値 'apikey'
$phpmailer->Password = defined('SENDGRID_API_KEY') ? SENDGRID_API_KEY : '';
$phpmailer->setFrom( 'noreply@example.com', get_bloginfo('name') );
} );
// Amazon SES SMTP設定
add_action( 'phpmailer_init', function( PHPMailer\PHPMailer\PHPMailer $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = 'email-smtp.ap-northeast-1.amazonaws.com'; // 東京リージョン
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587;
$phpmailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$phpmailer->Username = defined('SES_SMTP_USER') ? SES_SMTP_USER : '';
$phpmailer->Password = defined('SES_SMTP_PASS') ? SES_SMTP_PASS : '';
// SESではIAMのSMTP認証情報を使用(IAMパスワードではない)
$phpmailer->setFrom( 'noreply@example.com', get_bloginfo('name') );
} );
ステップ4:送信元アドレスをカスタマイズする
// functions.php: WordPressのデフォルト送信元を変更
// デフォルトは wordpress@ドメイン名
add_filter( 'wp_mail_from', function( $email ) {
return 'noreply@example.com'; // 自分のドメインのアドレスに変更
} );
add_filter( 'wp_mail_from_name', function( $name ) {
return get_bloginfo( 'name' ); // サイト名を送信者名に
} );
// Content-Typeをデフォルトでテキストにする(HTMLメールが不要な場合)
add_filter( 'wp_mail_content_type', function() {
return 'text/plain';
} );
// HTMLメールを送るカスタム関数
function send_html_mail( $to, $subject, $html_body ) {
add_filter( 'wp_mail_content_type', function() { return 'text/html'; } );
$result = wp_mail( $to, $subject, $html_body );
// フィルターをリセット(ほかのメール送信に影響しないよう)
remove_filter( 'wp_mail_content_type', function() { return 'text/html'; } );
return $result;
}
ステップ5:メール送信をテスト・デバッグする
// functions.php: テスト用メール送信(管理者のみ、本番では削除)
add_action( 'admin_init', function() {
if ( isset( $_GET['test_smtp'] ) && current_user_can( 'manage_options' ) ) {
$result = wp_mail(
get_option( 'admin_email' ),
'SMTP テストメール',
"SMTPが正常に設定されています。\n送信時刻: " . current_time( 'mysql' )
);
wp_die( $result
? '✅ メール送信成功。受信ボックスを確認してください。'
: '❌ メール送信失敗。SMTP設定を確認してください。'
);
}
} );
// テスト: https://example.com/wp-admin/?test_smtp=1 にアクセス
// SMTPデバッグを一時的に有効化
add_action( 'phpmailer_init', function( PHPMailer\PHPMailer\PHPMailer $phpmailer ) {
// デバッグレベル(0=無効, 1=クライアント, 2=クライアント+サーバー, 4=接続)
$phpmailer->SMTPDebug = 2;
$phpmailer->Debugoutput = function( $str, $level ) {
error_log( "[SMTP Debug L{$level}] {$str}" );
};
} );
// debug.logでSMTPハンドシェイクの詳細を確認できる
注意事項
- SMTP認証情報(パスワード・APIキー)は
functions.phpにハードコードしないでください。wp-config.phpにdefine()定数として記述するか、環境変数($_ENV)を使ってください。wp-config.phpはWebからアクセス不可なため、functions.phpより安全です。 - Gmailの場合、通常のパスワードではなくアプリパスワードを使ってください。2段階認証が有効になっていないと生成できません。
WP Mail SMTPプラグインを使うとGUI設定が可能で、phpmailer_initフックの実装が不要になります。ただし設定値をDBに保存するため、DBが公開された場合のリスクがあります。定数での管理(WPMS_*定数)がより安全です。
まとめ
SMTP設定は「phpmailer_initフックでisSMTP()・Host・Port・Username・Passwordを設定→認証情報はwp-config.phpの定数で管理→wp_mail_fromで送信元を変更→テスト用エンドポイントで送信確認→SMTPDebugでdebug.logに詳細を出力してデバッグ」の流れで整備します。関連記事:WordPressのお問い合わせフォーム(CF7)をカスタマイズする方法、WordPressのURLをwwwあり・なしに統一する方法。