wp-envでメールが送れないのなぁぜなぁぜ？【WordPress】

`wordpress@localhost`が無効なアドレスなので送れないのです。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/db872330-8f99-8a75-ad0e-31c17ad3eb50.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/415881d5-b0a1-01be-36d2-2151e8360944.png)

https://github.com/WordPress/wordpress-develop/blob/03b79d88d698ec42dbc99ca9a839ab49ba96995c/src/wp-includes/pluggable.php#L408

# 解決策

フックで有効なアドレスにしよう

```php
add_filter( 'wp_mail_from', fn() => get_option('admin_email') );
```

# gmailで送る場合

まずは、2段階認証を有効にしてパスワードを作ります
https://qiita.com/7mpy/items/5cbb021aa7505b28b7a2

[wp-includes/pluggable.php:145](https://github.com/WordPress/wordpress-develop/blob/03b79d88d698ec42dbc99ca9a839ab49ba96995c/src/wp-includes/pluggable.php#L145)に追記する例

```php
function gmail($phpmailer) {
  $phpmailer->isSMTP();
  $phpmailer->Host = 'smtp.gmail.com';
  $phpmailer->SMTPAuth = true;
  $phpmailer->Port = 587;
  $phpmailer->Username = 'funaox@gmail.com';
  $phpmailer->Password = 'mddtgtfbrznzcmoe';
}

add_action('phpmailer_init', 'gmail');

add_filter( 'wp_mail_from', fn() => get_option('admin_email') );
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/4bbd3d27-41ba-4c2b-b0c0-ee4586596bdf.png)

※mddtgtfbrznzcmoeのアカウントは削除済みです

## 動作確認

```bash
$ wp-env run cli wp shell --allow-root
ℹ Starting 'wp shell --allow-root' on the cli container. 

wp> wp_mail( 'funaox@gmail.com', 'mail test', 'hello wp_mail!' );
=> bool(true)
wp>
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/5d54c32d-128c-f52b-0691-e49753f48032.png)

メールの受信箱

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/8985295c-5c30-f97b-2c97-88f5687fd205.png)


# mailtrapも便利！

https://mailtrap.io/

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/647980b3-f41c-eaa4-3f58-0d2296402830.png)

[wp-includes/pluggable.php:145](https://github.com/WordPress/wordpress-develop/blob/03b79d88d698ec42dbc99ca9a839ab49ba96995c/src/wp-includes/pluggable.php#L145)に追記する例

```php
function mailtrap($phpmailer) {
  $phpmailer->isSMTP();
  $phpmailer->Host = 'sandbox.smtp.mailtrap.io';
  $phpmailer->SMTPAuth = true;
  $phpmailer->Port = 2525;
  $phpmailer->Username = 'a1de692d385898';
  $phpmailer->Password = '********0f44';
}

add_action('phpmailer_init', 'mailtrap');

add_filter( 'wp_mail_from', fn() => get_option('admin_email') );
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/c3223cc0-c16e-95d6-ecad-e7d1eeb7dcc7.png)


## 動作確認

```bash
$ wp-env run cli wp shell --allow-root
ℹ Starting 'wp shell --allow-root' on the cli container. 

wp> wp_mail( 'vojoba3724@gameszox.com', 'mail test', 'hello wp_mail!' );
=> bool(true)
wp>
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/e3401cb4-2562-98b1-27e0-9d8198081bc1.png)


メールの受信箱
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/ca655a02-c462-8fab-897f-7fe6fb447166.png)

smtp設定してない場合はメール送れなかったorz
```bash
sendmail: can't connect to remote host (127.0.0.1): Connection refused
```

# まとめ

最初から素直にLocal by Flywheel使った方がいいと思います

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/72ee859d-e1c8-124e-d479-6a16d86900fe.png)


# 参考

https://techracho.bpsinc.jp/ebi/2023_01_10/126099