Products
GG网络技术分享 2025-03-18 16:12 0
wordpress 的SMTP邮件服务可以帮助我们更好的管理网站,功能包括使用 WordPress 邮件评论回复通知、用户注册邮件通知、以及其他邮件通知功能,这些基本都会使用 SMTP邮件服务,WP自带了mail函数,但用自带mail函数发送邮件很容易失败或者被拒收。所以很多人选择用更方便高效的SMTP来发送邮件。要用SMTP发送邮件,首先需要拥有一个支持SMTP的邮箱(包括Gmail、QQ、126、163等主流邮箱都支持SMTP功能,可能需要到设置中打开),然后在WordPress中简单设置一下。下面介绍的是无插件实现SMTP发送功能,毕竟插件多了对速度有不利的影响。
关联文章导航
【开启wordpress 的SMTP邮件通知服务:WP SMTP插件——墨涩网】
【WordPress纯代码无插件开启SMTP邮件服务——墨涩网】
【解决腾讯云主机无法使用SMTP邮箱——墨涩网】
方法一:
编辑主题文件functions.php文件(wordpress可以在后台“外观”下的“编辑”中找到文件添加,手动修改地址:主机目录/wp-content/themes/主题名/functions.php)添加如下代码。
请修改代码中的邮箱信息为你自己的信息。(如果是QQ邮箱,登录密码填写生成的授权码并不是邮箱登录密码,具体获取方式请参考(开启wordpress 的SMTP邮件通知服务:WP SMTP插件——墨涩网)文章的第1-5步。
//WordPress邮箱SMTP配置开始(www.sunweihu.com)
add_action(\'phpmailer_init\', \'fanly_mail_smtp\');
function fanly_mail_smtp( $phpmailer ) {
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;//启用 SMTPAuth 服务
$phpmailer->FromName = \'墨涩网\'; //发件人名称
$phpmailer->Port =465;//MTP 邮件发送端口,常用的端口为25和465(SSL加密端口)
$phpmailer->SMTPSecure =\"ssl\";//是否验证 ssl,与 MTP 邮件发送端口对应,如果不填写,则上面的端口须为 25
$phpmailer->Host =\"smtp.qq.com\";//邮箱的 SMTP 服务器地址,目前 smtp.exmail.qq.com 为 QQ 邮箱
$phpmailer->Username = \"mosewang@qq.com\";//你的邮箱地址
$phpmailer->Password =\"fgywfgksfjcwbvva\";//你的邮箱登录密码(如果是QQ邮箱填写生成的授权码)
}
add_filter( \'wp_mail_from\', \'fanly_wp_mail_from\' );
function fanly_wp_mail_from() {
return \'mosewang@qq.com\'; //邮箱账户,和上面的邮箱地址一致
}
//WordPress邮箱SMTP配置end(www.sunweihu.com)
方法二:
直接修改WordPress源文件class-phpmailer.php和pluggable.php,这个方法需要对WordPress的2个源文件做小小的修改。好处就是你不需要每次更改主题都修改functions.php,而且更安全。
在WordPress的wp-includes目录下找到pluggable.php和class-phpmailer.php两个文件。
将pluggable.php中的”$phpmailer->IsMail(); “替换为:”$phpmailer->IsSMTP();”
在class-phpmailer.php中修改下面对应的设置:需要找到对应的参数修改即可。
public $Mailer = \'smtp\';
public $Host = \'smtp.gmail.com\'; //邮箱的SMTP服务器地址
public $Port = 465; //SMTP邮件发送端口
public $SMTPSecure = \"ssl\"; //是否验证 ssl或tls
public $SMTPAuth = true; //开启SMTPAuth
public $Username = \'username@gmail.com\'; //你的邮箱地址
public $Password = \'******\'; //你的邮箱登陆密码
注意:此方式并未测试,是否可用请自行测试。
wordpress创建自定义文章类型函数register_post_type的public参数有点难以搞明白,该参数在官网的说明中说:
public
(boolean) (optional) Whether a post type is intended to be used publicly either via the admin interface or by front-end users.
- Default: false
- \'false\' - Post type is not intended to be used publicly and should generally be unavailable in wp-admin and on the front end unless explicitly planned for elsewhere.
- \'true\' - Post type is intended for public use. This includes on the front end and in wp-admin.
Note: While the default settings of exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are inherited from public, each does not rely on this relationship and controls a very specific intention.
是否准备让文章类型在前台和后台管理界面公开。设置成flase的时候,文章类型将不会被公开使用,设置为true的时候,文章类型可以在前台和后台公开使用。
当你把public设置才fasle后,再到后台看,发现后台没什么变化,访问添加的该类型文章,也能显示,切换这个参数的值,没能发现什么变化,其实这个参数的说明中后面的Note是重点,这个参数的作用算是给另外几个参数继承用的,也就是设置了这个参数后,其它几个参数可以直接继承这个参数的值,不需另外设置。如果其它参数你都设置了具体的值,这个参数设置成ture或false都没什么关系了。
你也可以查看该register_post_type函数的源码,在wp-includes/post.php文件中,可以看到public参数只是在其它参数:exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are inherited from public没有值的时候给它们个默认值。
Demand feedback