| 1 |
<?php |
| 2 |
/* |
| 3 |
Name: SMTP 发信 |
| 4 |
URI: https://mp.weixin.qq.com/s/SbuvSL01hT3Jxp9doWZ8zg |
| 5 |
Description: SMTP 发信可以让你使用第三方邮箱的 SMTP 服务来发送邮件。 |
| 6 |
Version: 2.0 |
| 7 |
*/ |
| 8 |
class WPJAM_SMTP{ |
| 9 |
public static function __callStatic($method, $args){ |
| 10 |
return wpjam_call_option(static::class, $method, ...$args); |
| 11 |
} |
| 12 |
|
| 13 |
public static function get_fields(){ |
| 14 |
return [ |
| 15 |
'smtp_setting' => ['title'=>'SMTP 设置', 'type'=>'fieldset', 'fields'=>[ |
| 16 |
'host' => ['title'=>'SMTP地址', 'type'=>'text', 'class'=>'', 'value'=>'smtp.qq.com'], |
| 17 |
'user' => ['title'=>'邮箱账号', 'type'=>'email', 'class'=>''], |
| 18 |
'pass' => ['title'=>'邮箱密码', 'type'=>'password', 'class'=>''], |
| 19 |
'ssl' => ['title'=>'发送协议', 'type'=>'text', 'class'=>'', 'value'=>'ssl'], |
| 20 |
'port' => ['title'=>'SSL端口', 'type'=>'number', 'class'=>'', 'value'=>'465'], |
| 21 |
]], |
| 22 |
'mail_from_name' => ['title'=>'发送� |
| 23 |
姓名', 'type'=>'text', 'class'=>''], |
| 24 |
'reply_to_mail' => ['title'=>'回复地址', 'type'=>'email','class'=>'', 'description'=>'不填则用户回复使用SMTP设置中的邮箱账号'] |
| 25 |
]; |
| 26 |
} |
| 27 |
|
| 28 |
public static function get_form(){ |
| 29 |
return [ |
| 30 |
'submit_text' => '发送', |
| 31 |
'callback' => fn($data)=> wp_mail($data['to'], $data['subject'], $data['message']), |
| 32 |
'validate' => true, |
| 33 |
'fields' => [ |
| 34 |
'to' => ['title'=>'收件人', 'type'=>'email', 'required'], |
| 35 |
'subject' => ['title'=>'主题', 'type'=>'text', 'required'], |
| 36 |
'message' => ['title'=>'� |
| 37 |
容', 'type'=>'textarea', 'required'], |
| 38 |
] |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
public static function add_hooks(){ |
| 43 |
if(array_all(['host', 'user', 'pass'], fn($k)=> self::get_setting($k))){ |
| 44 |
add_action('phpmailer_init', function($phpmailer){ |
| 45 |
$phpmailer->Mailer = 'smtp'; |
| 46 |
$phpmailer->SMTPAuth = true; |
| 47 |
$phpmailer->SMTPSecure = self::get_setting('ssl', 'ssl'); |
| 48 |
$phpmailer->Host = self::get_setting('host'); |
| 49 |
$phpmailer->Port = self::get_setting('port', '465'); |
| 50 |
$phpmailer->Username = self::get_setting('user'); |
| 51 |
$phpmailer->Password = self::get_setting('pass'); |
| 52 |
|
| 53 |
if($reply_to = self::get_setting('reply_to_mail')){ |
| 54 |
$phpmailer->AddReplyTo($reply_to, self::get_setting('mail_from_name', '')); |
| 55 |
} |
| 56 |
}); |
| 57 |
|
| 58 |
wpjam_hook('wp_mail_from', self::get_setting('user')); |
| 59 |
wpjam_hook('wp_mail_from_name', 'maybe', self::get_setting('mail_from_name') ?: null); |
| 60 |
} |
| 61 |
|
| 62 |
if(wp_doing_ajax() || wpjam_is_json_request()){ |
| 63 |
add_action('wp_mail_failed', 'wpjam_send_json'); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
wpjam_register_option('wpjam-smtp', [ |
| 69 |
'model' => 'WPJAM_SMTP', |
| 70 |
'title' => '发信设置', |
| 71 |
'menu_page' => [ |
| 72 |
'parent' => 'wpjam-basic', |
| 73 |
'page_title' => 'SMTP 发信', |
| 74 |
'network' => false, |
| 75 |
'summary' => __FILE__, |
| 76 |
'function' => 'tab', |
| 77 |
'tabs' => [ |
| 78 |
'smtp' => ['title'=>'发信设置', 'function'=>'option'], |
| 79 |
'send' => ['title'=>'发送测试', 'function'=>'form', 'form'=>['WPJAM_SMTP', 'get_form']] |
| 80 |
] |
| 81 |
] |
| 82 |
]); |