WordPress4.6から wp_mail関数の仕様が変りました。そして さらに WordPress4.6.1でちょっと変わりました。
WordPress4.6
まず、WordPress4.6に なってから wp_mail関数に 送る引数 $headers に「Reply-To」(返信先メールアドレス) が使えるようになりました。
使い方としたら wp_mailを使うコードがあれば「From」のところのように追記するだけです。
1 2 3 4 5 | $headers = 'From: My Name <myname@example.jp>' . "\r\n" ; $headers .= 'Reply-To: Reply Name <replyname@example.jp>' . "\r\n" ; ・ ・ wp_mail( 'test@example.org' , 'subject' , 'message' , $headers , $attachments ); |
または
1 2 3 4 5 | $headers [] = 'From: My Name <myname@example.jp>' ; $headers [] = 'Reply-To: Reply Name <replyname@example.jp>' ; ・ ・ wp_mail( 'test@example.org' , 'subject' , 'message' , $headers , $attachments ); |
スポンサードリンク
WordPress4.6.1
WordPress4.6.1 になって 変わったところはここです。
wp-includes/pluggable.php wp_mail()内
1 2 3 4 5 | WordPress4.6 $phpmailer ->setFrom( $from_email , $from_name ); WordPress4.6.1 $phpmailer ->setFrom( $from_email , $from_name , false ); |
変わったのは「false」が追加されている所ですが、何に「false」をしているかというと、、
wp-includes/class-phpmailer.php を見ると
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /** * Set the From and FromName properties. * @param string $address * @param string $name * @param boolean $auto Whether to also set the Sender address, defaults to true * @throws phpmailerException * @return boolean */ public function setFrom( $address , $name = '' , $auto = true) { $address = trim( $address ); $name = trim(preg_replace( '/[\r\n]+/' , '' , $name )); //Strip breaks and trim // Don't validate now addresses with IDN. Will be done in send(). if (( $pos = strrpos ( $address , '@' )) === false or (! $this ->has8bitChars( substr ( $address , ++ $pos )) or ! $this ->idnSupported()) and ! $this ->validateAddress( $address )) { $error_message = $this ->lang( 'invalid_address' ) . $address ; $this ->setError( $error_message ); $this ->edebug( $error_message ); if ( $this ->exceptions) { throw new phpmailerException( $error_message ); } return false; } $this ->From = $address ; $this ->FromName = $name ; if ( $auto ) { if ( empty ( $this ->Sender)) { $this ->Sender = $address ; } } return true; } |
これを見ると $auto のところに false が入りますね。
今までは $auto=true なので [Sender] にも「From」のメールアドレスが 設定されていましたが
WordPress4.6.1 からは $auto=false となるため [Sender] が空欄になってしまうという事です。
※[Sender]は 独自ドメインのメールを SPF に対応させる場合に使ったりします。
【参考】
Emails fail on certain server setups - Make WordPress Core
https://core.trac.wordpress.org/ticket/37736
でもやはり [Sender]に 入れたい場合は 以下のコードを my-plugin.php に張り付けてください。
1 2 3 4 5 6 7 8 9 10 | /** * Fires after PHPMailer is initialized. * @param PHPMailer &$phpmailer The PHPMailer instance, passed by reference. * * License: GPLv2 or later */ function nendebcom_initialized_mail_sender( $phpmailer ) { $phpmailer ->Sender = 'xxxxx@example.com' ; //Sender用のメールアドレス } add_action( 'phpmailer_init' , 'nendebcom_initialized_mail_sender' ); |
もし WordPress4.6.1 になってから 一部の宛先へのメールが届かなくなったりしてたら、ここらへんをチェックしてもいいかもしれません。
参考
WordPress Codex 日本語版 関数リファレンス/wp mail
https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/wp_mail
WordPress Code Reference wp_mail()
https://developer.wordpress.org/reference/functions/wp_mail/