AHdark
一个多语种开发蒟蒻
AHdark Blog

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

赞赏
本文链接:https://ahdark.com/technology/840.shtml
本文采用 CC BY-NC-SA 3.0 Unported 协议进行许可
欢迎加入Q群交流:654022768

AH Dark

文章作者

本博客的运营者、主要开发者、主要作者 深度学习算法工程师,后端工程师,嵌入式软件开发工程师 Azure认证 运维工程师

发表评论

textsms
account_circle
email

AHdark Blog

WPJAM_Basic修正:From请求头和DKIM签名
为WPJAM_Basic插件加入DKIM签名和Form请求头以避免被计入垃圾邮件和伪造邮件
扫描二维码继续阅读
2021-08-16