web-dev-qa-db-ja.com

特定の 'wp_mail'アクセスに対してのみ 'SMTP phpmailer_init' SMTP設定を使用する方法

phpmailer_initまたはwp_mailパラメータに対して実行できる条件付きチェックはありますか。それにより、特定のphpmailer_initアクションにのみ私のカスタムwp_mail SMTP設定を適用できますか、それともphpmailer_initは常にサイト全体で実行されますか。

1
tomyam

phpmailer_initは常にすべてのwp_mail()呼び出しに対して起動します - ただし、以下のように条件付きでフック/フック解除することができます。

function wpse_224496_phpmailer_init( $phpmailer ) {
    // SMTP setup

    // Always remove self at the end
    remove_action( 'phpmailer_init', __function__ );
}

function wpse_224496_wp_mail( $mail ) {
    // Example: only SMTP for emails addressed to [email protected]
    if ( $mail['to'] === '[email protected]' )
        add_action( 'phpmailer_init', 'wpse_224496_phpmailer_init' );

    // Example: only SMTP for subject "Foo"
    if ( $mail['subject'] === 'Foo' )
        add_action( 'phpmailer_init', 'wpse_224496_phpmailer_init' );

    // Other properties
    $mail['message'];
    $mail['headers']; // Could be string or array
    $mail['attachments']; // Could be string or array

    return $mail;
}

add_filter( 'wp_mail', 'wpse_224496_wp_mail' );
3
TheDeadMedic