LoginSignup
0
0

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

Last updated at Posted at 2023-09-09

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

image.png

image.png

解決策

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

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

gmailで送る場合

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

wp-includes/pluggable.php:145に追記する例

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

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

動作確認

$ 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

メールの受信箱

image.png

mailtrapも便利!

image.png

wp-includes/pluggable.php:145に追記する例

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

動作確認

$ 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

メールの受信箱
image.png

smtp設定してない場合はメール送れなかったorz

sendmail: can't connect to remote host (127.0.0.1): Connection refused

まとめ

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

image.png

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0