WPJAM_Basic修正:From请求头和DKIM签名

https://www.mail-tester.com的垃圾邮件评分测试中,我找到了该SMTP模块的缺陷同时进行了相关修复

<!-- File Path: /extends/wpjam-smtp.php -->
<?php
/*
Name: SMTP 发信
URI: https://blog.wpjam.com/m/wpjam-smtp/
Description: 简单配置就能让 WordPress 使用 SMTP 发送邮件。
Version: 1.0
*/
class WPJAM_SMTP{
    use WPJAM_Setting_Trait;

    private function __construct(){
        $this->init('wpjam-smtp');
    }

    public function on_phpmailer_init($phpmailer){
        $phpmailer->isSMTP();

        // $phpmailer->SMTPDebug	= 1;

        $phpmailer->SMTPAuth	= true;
        $phpmailer->SMTPSecure	= $this->get_setting('ssl');
        $phpmailer->Host		= $this->get_setting('host');
        $phpmailer->Port		= $this->get_setting('port');
        $phpmailer->Username	= $this->get_setting('user');
        $phpmailer->Password	= $this->get_setting('pass');
        
        // Added Header From to improve mail quality score.
        $phpmailer->setFrom($this->get_setting('user'),$this->get_setting('mail_from_name'));

        // DKIM
        if($this->get_setting('selector')!=null && $this->get_setting('secret')!=null) {
            $phpmailer->DKIM_domain     = explode("@",$this->get_setting('user'))[1];
            $phpmailer->DKIM_private    = ABSPATH.'dkim.key';
            $phpmailer->DKIM_selector   = $this->get_setting('selector');
            $phpmailer->DKIM_passphrase = $this->get_setting('secret');
            $phpmailer->DKIM_identity   = $phpmailer->From;
        }

		if($smtp_reply_to_mail	= $this->get_setting('reply_to_mail')){
            $name	= $this->get_setting('mail_from_name') ?: '';
            $phpmailer->AddReplyTo($smtp_reply_to_mail, $name);
        }
	}

    public function filter_wp_mail_from(){
        return $this->get_setting('user');
    }

    public function filter_wp_mail_from_name($name){
        return $this->get_setting('mail_from_name') ?: $name;
    }

    public static function ajax_send(){
        $to			= wpjam_get_data_parameter('to');
        $subject	= wpjam_get_data_parameter('subject');
        $message	= wpjam_get_data_parameter('message');

        if(wp_mail($to, $subject, $message)){
            wpjam_send_json();
        }
    }

    public static function on_wp_mail_failed($mail_failed){
        wpjam_send_json($mail_failed);
    }
}

add_action('wp_loaded', function(){
    $instance	= WPJAM_SMTP::get_instance();

    add_action('phpmailer_init',	[$instance, 'on_phpmailer_init']);
    add_filter('wp_mail_from',		[$instance, 'filter_wp_mail_from']);
    add_filter('wp_mail_from_name',	[$instance, 'filter_wp_mail_from_name']);

    if(is_admin() && (!is_multisite() || !is_network_admin())){
        wpjam_add_basic_sub_page('wpjam-smtp', [
            'menu_title'	=> '发信设置',
            'page_title'	=> 'SMTP邮件服务',
            'function'		=> 'tab',
            'tabs'			=> [
                'smtp'	=> [
                    'title'		=> '发信设置',
                    'function'	=> 'option',
                    'fields'	=> [
                        'smtp_setting'		=> ['title'=>'SMTP 设置',	'type'=>'fieldset','fields'=>[
                            'host'	=> ['title'=>'地址',		'type'=>'text',		'class'=>'all-options',	'value'=>'smtp.qq.com'],
                            'ssl'	=> ['title'=>'发送协议',	'type'=>'text',		'class'=>'',			'value'=>'ssl'],
                            'port'	=> ['title'=>'SSL端口',	'type'=>'number',	'class'=>'',			'value'=>'465'],
                            'user'	=> ['title'=>'邮箱账号',	'type'=>'email',	'class'=>'all-options'],
                            'pass'	=> ['title'=>'邮箱密码',	'type'=>'password',	'class'=>'all-options'],
                        ]],
                        'mail_from_name'	=> ['title'=>'发送者姓名',	'type'=>'text',	'class'=>''],
                        'reply_to_mail'		=> ['title'=>'回复地址',		'type'=>'email','class'=>'all-options',	'description'=>'不填则用户回复使用SMTP设置中的邮箱账号'],
                        'dkim_setting'      => ['title'=>'DKIM 设置',	'type'=>'fieldset','fields'=>[
                            'selector'	=> ['title'=>'Selector',		'type'=>'text',		'class'=>'all-options',	'description'=>'DNS解析主机记录为 [selector]._domainkey.example.com'],
                            'secret'	=> ['title'=>'私钥密码',	'type'=>'password',		'class'=>'all-options','description' => '请将私钥放在根目录下/dkim.key',]
                        ]],
                    ]
                ],
                'send'	=> [
                    'title'			=> '发送测试',
                    'function'		=> 'form',
                    'submit_text'	=> '发送',
                    'callback'		=> ['WPJAM_SMTP', 'ajax_send'],
                    'fields'		=> [
                        'to'		=> ['title'=>'收件人',	    'type'=>'email',	    'required'],
                        'subject'	=> ['title'=>'主题',		 'type'=>'text',		 'required'],
                        'message'	=> ['title'=>'内容',		 'type'=>'textarea',	 'class'=>'',   'rows'=>8,  'required'],
                    ]
                ],
            ],
            'summary'		=> 'SMTP 邮件服务扩展让你可以使用第三方邮箱的 SMTP 服务来发邮件,详细介绍请点击:<a href="https://blog.wpjam.com/m/wpjam-smtp/" target="_blank">SMTP 邮件服务扩展</a>,点击这里查看:<a target="_blank" href="http://blog.wpjam.com/m/gmail-qmail-163mail-imap-smtp-pop3/" target="_blank">常用邮箱的 SMTP 设置</a>。'
        ]);
    }
});

在我最终测试后,邮件Header将携带以下内容

DKIM-Signature: v=1; d=notice.ahdark.com; s=selector1;
 a=rsa-sha256; q=dns/txt; t=1629113085; c=relaxed/simple;
 h=Date:To:From:Reply-To:Subject:Message-ID:X-Mailer:MIME-Version:Content-Type;
 [email protected];
 z=Date:Mon,=2016=20Aug=202021=2011:24:45=20+0000

以此实现了DKIM签名

关于如何实现,我未来将会出一篇文章详细描述

代码内容将同步更新至https://gist.github.com/AH-dark/0ca64f43aabc31f7692929d2923a4dec

WPJAM_Basic修正:From请求头和DKIM签名 的发布基于协议 AHdark Blog License。如欲对此文章内容此文章转载、修改或行使任何超出预览和分享性质的行为,请参考此协议。
暂无评论

发送评论 编辑评论


|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇