最近博客搬家到万网的免费虚拟主机上,发现了一个bug,邮件一直无法发送。WordPress程序(3.8版本以后)默认使用的是stream_socket_client函数发信,而万网的免费虚拟主机使用的是fsockopen函数发信,因此,我们需做以下修改:
第一步、在万网的免费虚拟主机后台启用fsockopen函数(PHP.in设置 → PHP函数设置)。如下图所示:
第二步、修改WordPress程序中wp-includes/class-smtp.php文件,找到以下内容部分(202行左右):
$this->smtp_conn = @stream_socket_client( $host . ":" . $port, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $socket_context );
修改为:
$this->smtp_conn = fsockopen($host, $port, $errno, $errstr);
第三步、安装SMTP Mailer插件https://wordpress.org/plugins/smtp-mailer/ 。
推荐免插件实现SMTP邮件服务的方法,将以下代码添加到主题functions.php文件中:
//使用smtp发送邮件(请根据自己使用的邮箱设置SMTP) add_action('phpmailer_init', 'mail_smtp'); function mail_smtp( $phpmailer ) { $phpmailer->FromName = '常阳时光'; //发件人名称 $phpmailer->Host = 'smtp.qq.com'; //修改为你使用的邮箱SMTP服务器 $phpmailer->Port = 465; //SMTP端口 $phpmailer->Username = '123@qq.com'; //邮箱账户 $phpmailer->Password = '123123123'; //邮箱密码 $phpmailer->From = '123@qq.com'; //邮箱账户 $phpmailer->SMTPAuth = true; $phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25时->留空,465时->ssl) $phpmailer->IsSMTP(); }