{"id":26758,"date":"2025-02-18T00:29:13","date_gmt":"2025-02-17T15:29:13","guid":{"rendered":"https:\/\/motoki-design.co.jp\/wordpress\/?p=26758"},"modified":"2026-10-04T08:16:25","modified_gmt":"2026-10-03T23:16:25","slug":"wordpress-phpmailer","status":"publish","type":"post","link":"https:\/\/motoki-design.co.jp\/wordpress\/wordpress-phpmailer\/","title":{"rendered":"WordPress\u2502\u30b3\u30fc\u30c9\u3060\u3051\u3067SMTP\u3092\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5\u2502PHPMailer"},"content":{"rendered":"<p class=\"wp-block-paragraph\">phpmailer_init\u3067WordPress\u306e\u30e1\u30fc\u30eb\u9001\u4fe1\u3092SMTP\u3078\u5207\u308a\u66ff\u3048\u308b\u4f8b\u3067\u3059\u3002SMTP\u306e\u63a5\u7d9a\u60c5\u5831\u3092\u74b0\u5883\u5909\u6570\u304b\u3089\u8aad\u307f\u53d6\u308a\u3001\u7ba1\u7406\u8005\u306e\u64cd\u4f5c\u30671\u901a\u9001\u308b\u30c6\u30b9\u30c8\u753b\u9762\u3092\u7528\u610f\u3057\u307e\u3059\u3002<\/p>\n<h2 class=\"wp-block-heading\">\u9001\u4fe1\u5143\u3068\u63a5\u7d9a\u60c5\u5831\u3092\u6e96\u5099<\/h2>\n<p class=\"wp-block-paragraph\">\u30e1\u30fc\u30eb\u63d0\u4f9b\u5143\u304c\u6307\u5b9a\u3059\u308bSMTP\u30db\u30b9\u30c8\u3001\u8a8d\u8a3c\u30e6\u30fc\u30b6\u30fc\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u3001\u9001\u4fe1\u3092\u8a31\u53ef\u3055\u308c\u305fFrom\u30a2\u30c9\u30ec\u30b9\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u6b21\u306e\u4f8b\u306f587\u756a\u30dd\u30fc\u30c8\u306eSTARTTLS\u7528\u3067\u3059\u3002465\u756a\u306e\u6697\u9ed9TLS\u304c\u6307\u5b9a\u3055\u308c\u305f\u5951\u7d04\u3067\u306f\u3001\u30dd\u30fc\u30c8\u3068\u6697\u53f7\u5316\u65b9\u5f0f\u3092\u4e21\u65b9\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u5931\u6557\u3057\u305f\u304b\u3089\u6697\u53f7\u5316\u3092\u5916\u3059\u624b\u9806\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p class=\"wp-block-paragraph\">\u30b5\u30fc\u30d0\u30fc\u306ePHP\u5b9f\u884c\u74b0\u5883\u3078WAZA_SMTP_HOST\u3001WAZA_SMTP_USER\u3001WAZA_SMTP_PASSWORD\u3001WAZA_SMTP_FROM\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u74b0\u5883\u5909\u6570\u306e\u8a2d\u5b9a\u65b9\u6cd5\u306f\u30db\u30b9\u30c6\u30a3\u30f3\u30b0\u306b\u4f9d\u5b58\u3057\u307e\u3059\u3002\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u8a18\u4e8b\u3001\u5171\u6709\u30b3\u30fc\u30c9\u3001\u753b\u9762\u30ed\u30b0\u3078\u8a18\u8f09\u3057\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u65e2\u5b58\u306eSMTP\u30d7\u30e9\u30b0\u30a4\u30f3\u3068\u306f\u4e8c\u91cd\u8a2d\u5b9a\u3057\u307e\u305b\u3093\u3002<\/p>\n<h2 class=\"wp-block-heading\">SMTP\u306e\u8a2d\u5b9a\u30b3\u30fc\u30c9<\/h2>\n<p class=\"wp-block-paragraph\">\u30c6\u30fc\u30de\u3092\u5207\u308a\u66ff\u3048\u3066\u3082\u5fc5\u8981\u306a\u51e6\u7406\u306a\u306e\u3067\u5c02\u7528\u30d7\u30e9\u30b0\u30a4\u30f3\u306a\u3069\u3078\u8ffd\u52a0\u3057\u307e\u3059\u3002\u5b50\u30c6\u30fc\u30defunctions.php\u306b\u5165\u308c\u308b\u5834\u5408\u3001\u65e2\u5b58PHP\u5185\u3067\u306f\u958b\u59cb\u30bf\u30b0\u3092\u7701\u304d\u307e\u3059\u3002\u74b0\u5883\u5909\u6570\u304c\u672a\u8a2d\u5b9a\u306a\u3089\u9001\u4fe1\u3092\u5931\u6557\u3055\u305b\u3001\u5225\u65b9\u5f0f\u3078\u9ed9\u3063\u3066\u5207\u308a\u66ff\u3048\u307e\u305b\u3093\u3002<\/p>\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"php\"><code>function waza_configure_smtp($mailer) {\n    $host = getenv('WAZA_SMTP_HOST');\n    $user = getenv('WAZA_SMTP_USER');\n    $pass = getenv('WAZA_SMTP_PASSWORD');\n    $from = getenv('WAZA_SMTP_FROM');\n    if (!$host || !$user || !$pass || !is_email($from)) {\n        throw new \\PHPMailer\\PHPMailer\\Exception('SMTP configuration is incomplete.');\n    }\n    $mailer-&gt;isSMTP();\n    $mailer-&gt;Host = $host;\n    $mailer-&gt;SMTPAuth = true;\n    $mailer-&gt;Port = 587;\n    $mailer-&gt;SMTPSecure = \\PHPMailer\\PHPMailer\\PHPMailer::ENCRYPTION_STARTTLS;\n    $mailer-&gt;Username = $user;\n    $mailer-&gt;Password = $pass;\n    $mailer-&gt;SMTPDebug = 0;\n    $mailer-&gt;setFrom($from, get_bloginfo('name'), false);\n    \/\/ \u554f\u3044\u5408\u308f\u305b\u30d5\u30a9\u30fc\u30e0\u304c\u6307\u5b9a\u3057\u305fReply-To\u306f\u6d88\u3055\u306a\u3044\u3002\n}\nadd_action('phpmailer_init', 'waza_configure_smtp');\n<\/code><\/pre><\/div>\n<p class=\"wp-block-paragraph\">Reply-To\u306f\u554f\u3044\u5408\u308f\u305b\u30d5\u30a9\u30fc\u30e0\u304c\u8a2d\u5b9a\u3057\u305f\u5024\u3092\u7dad\u6301\u3057\u307e\u3059\u3002From\u306f\u5951\u7d04\u3067\u8a31\u53ef\u3055\u308c\u305f\u30a2\u30c9\u30ec\u30b9\u3092\u4f7f\u3044\u307e\u3059\u3002MX\u306f\u53d7\u4fe1\u5148\u306b\u95a2\u308f\u308b\u8a2d\u5b9a\u3067\u3042\u308a\u3001SMTP\u63a5\u7d9a\u306e\u6210\u529f\u3060\u3051\u3092\u6c7a\u3081\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002SPF\u30fbDKIM\u30fbDMARC\u306f\u63d0\u4f9b\u5143\u306e\u6848\u5185\u306b\u6cbf\u3063\u3066\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<h2 class=\"wp-block-heading\">\u7ba1\u7406\u8005\u304c\u660e\u793a\u7684\u306b1\u901a\u9001\u308b\u30c6\u30b9\u30c8\u753b\u9762<\/h2>\n<p class=\"wp-block-paragraph\">\u30c6\u30b9\u30c8\u6642\u3060\u3051\u6b21\u3092\u8ffd\u52a0\u3057\u3001WAZA_SMTP_TEST_TO\u3078\u81ea\u5206\u304c\u53d7\u4fe1\u78ba\u8a8d\u3067\u304d\u308b\u5b9b\u5148\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002\u300c\u30c4\u30fc\u30eb\u2192SMTP\u30c6\u30b9\u30c8\u300d\u306e\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u305fPOST\u3060\u3051\u3067\u9001\u4fe1\u3057\u3001\u6a29\u9650\u3068nonce\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u540c\u3058\u5229\u7528\u8005\u306e\u9023\u7d9a\u5b9f\u884c\u306f60\u79d2\u9593\u6291\u5236\u3057\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"php\"><code>function waza_smtp_test_page() {\n    if (!current_user_can('manage_options')) return;\n    echo '&lt;div class=\"wrap\"&gt;&lt;h1&gt;SMTP\u30c6\u30b9\u30c8&lt;\/h1&gt;&lt;p&gt;\u74b0\u5883\u5909\u6570WAZA_SMTP_TEST_TO\u3067\u6307\u5b9a\u3057\u305f\u3001\u81ea\u5206\u304c\u53d7\u4fe1\u78ba\u8a8d\u3067\u304d\u308b\u5b9b\u5148\u30781\u901a\u9001\u308a\u307e\u3059\u3002&lt;\/p&gt;';\n    echo '&lt;form method=\"post\" action=\"' . esc_url(admin_url('admin-post.php')) . '\"&gt;';\n    echo '&lt;input type=\"hidden\" name=\"action\" value=\"waza_smtp_test\"&gt;';\n    wp_nonce_field('waza_smtp_test');\n    submit_button('\u30c6\u30b9\u30c8\u30e1\u30fc\u30eb\u30921\u901a\u9001\u4fe1');\n    echo '&lt;\/form&gt;&lt;\/div&gt;';\n}\nadd_action('admin_menu', function () {\n    add_management_page('SMTP\u30c6\u30b9\u30c8', 'SMTP\u30c6\u30b9\u30c8', 'manage_options', 'waza-smtp-test', 'waza_smtp_test_page');\n});\nadd_action('admin_post_waza_smtp_test', function () {\n    if (!current_user_can('manage_options') || ($_SERVER&#91;'REQUEST_METHOD'] ?? '') !== 'POST') {\n        wp_die('\u5b9f\u884c\u3067\u304d\u307e\u305b\u3093\u3002', '', array('response' =&gt; 403));\n    }\n    check_admin_referer('waza_smtp_test');\n    $to = getenv('WAZA_SMTP_TEST_TO');\n    if (!is_email($to)) wp_die('\u30c6\u30b9\u30c8\u5b9b\u5148\u3092\u8a2d\u5b9a\u3057\u3066\u304f\u3060\u3055\u3044\u3002');\n    $key = 'waza_smtp_test_' . get_current_user_id();\n    if (get_transient($key)) wp_die('\u7d9a\u3051\u3066\u9001\u4fe1\u305b\u305a\u3001\u53d7\u4fe1\u7d50\u679c\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002');\n    set_transient($key, 1, MINUTE_IN_SECONDS);\n    $ok = wp_mail($to, 'WordPress SMTP test', 'SMTP\u306e\u53d7\u4fe1\u78ba\u8a8d\u7528\u30e1\u30c3\u30bb\u30fc\u30b8\u3067\u3059\u3002');\n    wp_die($ok ? '\u9001\u4fe1\u51e6\u7406\u306f\u6210\u529f\u3057\u307e\u3057\u305f\u3002\u53d7\u4fe1\u7bb1\u3068\u8ff7\u60d1\u30e1\u30fc\u30eb\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002' : '\u9001\u4fe1\u51e6\u7406\u306b\u5931\u6557\u3057\u307e\u3057\u305f\u3002\u7ba1\u7406\u753b\u9762\u3078\u623b\u3063\u3066\u8a2d\u5b9a\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002');\n});\n<\/code><\/pre><\/div>\n<h2 class=\"wp-block-heading\">\u9001\u4fe1\u6210\u529f\u3068\u53d7\u4fe1\u78ba\u8a8d\u3092\u5206\u3051\u308b<\/h2>\n<p class=\"wp-block-paragraph\">wp_mail\u304ctrue\u3067\u3082\u53d7\u4fe1\u7bb1\u3078\u5230\u7740\u3057\u305f\u4fdd\u8a3c\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u53d7\u4fe1\u7bb1\u30fb\u8ff7\u60d1\u30e1\u30fc\u30eb\u30fb\u9001\u4fe1\u30b5\u30fc\u30d3\u30b9\u306e\u7d50\u679c\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\u5931\u6557\u6642\u306f\u30db\u30b9\u30c8\u3001\u30dd\u30fc\u30c8\u3068\u6697\u53f7\u5316\u306e\u7d44\u3001\u8a8d\u8a3c\u3001From\u306e\u8a31\u53ef\u3001\u9001\u4fe1\u4e0a\u9650\u3092\u8abf\u3079\u307e\u3059\u3002\u751f\u306e\u8a8d\u8a3c\u60c5\u5831\u3092\u30ed\u30b0\u3078\u51fa\u3055\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u30c6\u30b9\u30c8\u753b\u9762\u306f\u8abf\u67fb\u5f8c\u306b\u505c\u6b62\u3057\u307e\u3059\u3002<\/p>\n<p class=\"wp-block-paragraph\">\u623b\u3059\u5834\u5408\u306f\u3053\u306e\u30d5\u30c3\u30af\u767b\u9332\u3092\u505c\u6b62\u3057\u3001\u5143\u306e\u30e1\u30fc\u30eb\u9001\u4fe1\u8a2d\u5b9a\u3092\u5fa9\u5143\u3057\u3066\u78ba\u8a8d\u3057\u307e\u3059\u3002\u691c\u8a3c\u306fPHPMailer\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u8a2d\u5b9a\u78ba\u8a8d\u306b\u9650\u5b9a\u3057\u3001\u5b9fSMTP\u63a5\u7d9a\u3084\u30e1\u30fc\u30eb\u914d\u4fe1\u3092\u5b9f\u884c\u3057\u3066\u3044\u307e\u305b\u3093\u3002<\/p>\n<p class=\"wp-block-paragraph\">\u53c2\u7167\uff1a<a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/phpmailer_init\/\">phpmailer_init<\/a>\u3001<a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_mail\/\">wp_mail<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>phpmailer_init\u3067WordPress\u306e\u30e1\u30fc\u30eb\u9001\u4fe1\u3092SMTP\u3078\u5207\u308a\u66ff\u3048\u308b\u4f8b\u3067\u3059\u3002SMTP\u306e\u63a5\u7d9a\u60c5\u5831\u3092\u74b0\u5883\u5909\u6570\u304b\u3089\u8aad\u307f\u53d6\u308a\u3001\u7ba1\u7406\u8005\u306e\u64cd\u4f5c\u30671\u901a\u9001\u308b\u30c6\u30b9\u30c8\u753b\u9762\u3092\u7528\u610f\u3057\u307e\u3059\u3002 \u9001\u4fe1\u5143\u3068\u63a5\u7d9a\u60c5\u5831\u3092\u6e96\u5099 \u30e1\u30fc\u30eb\u63d0\u4f9b\u5143\u304c\u6307 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":26766,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":"","book_review_data":"","footnotes":"","ssp_meta_title":"WordPress\u3067\u30e1\u30fc\u30eb\u30b5\u30fc\u30d0\u30fc\u8a2d\u5b9a\uff5c\u30b3\u30fc\u30c9\u3067SMTP\u5316","ssp_meta_description":"\u74b0\u5883\u5909\u6570\u3092\u4f7f\u3063\u305fSMTP\u8a2d\u5b9a\u3068\u3001\u7ba1\u7406\u8005\u6a29\u9650\u30fbPOST\u30fbnonce\u3092\u78ba\u8a8d\u3059\u308b\u30c6\u30b9\u30c8\u9001\u4fe1\u51e6\u7406\u3002587\u756a\u30dd\u30fc\u30c8\u3068STARTTLS\u306e\u4f8b\u3001\u8fd4\u4fe1\u5148\u306e\u7dad\u6301\u3001\u78ba\u8a8d\u3068\u623b\u3057\u65b9\u3092\u89e3\u8aac\u3002"},"categories":[91],"tags":[],"area":[],"class_list":["post-26758","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress-customize"],"_links":{"self":[{"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/posts\/26758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/comments?post=26758"}],"version-history":[{"count":7,"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/posts\/26758\/revisions"}],"predecessor-version":[{"id":30792,"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/posts\/26758\/revisions\/30792"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/media\/26766"}],"wp:attachment":[{"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/media?parent=26758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/categories?post=26758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/tags?post=26758"},{"taxonomy":"area","embeddable":true,"href":"https:\/\/motoki-design.co.jp\/wordpress\/wp-json\/wp\/v2\/area?post=26758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}