首页 > PHP开发, 站长在线 > BAE Discuz X2.5 发送邮件方法

BAE Discuz X2.5 发送邮件方法

1.正确安装Discuz X2.5 for BAE,安装方法见 百度云bae安装discuz论坛教程
2.打开source/function/function_mail.php

3.找到elseif($_G['setting']['mail']['mailsend'] == 2)

我们需要修改的内容就在这个条件里

4.将该if条件块中的内容改为以下内容

[php]
<?php
/**
* 将fsocketopen改为socket_create
*/
/**
* if(!$fp = fsocketopen($_G['setting']['mail']['server'], $_G['setting']['mail']['port'], $errno, $errstr, 30)) {
* runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) CONNECT - Unable to connect to the SMTP server", 0);
* return false;
* }
*/
// stream_set_blocking($fp, true);
/**
* 请将fgets改为socket_read,fputs改为socket_write
*/
$fp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (socket_connect($fp, $_G['setting']['mail']['server'], $_G['setting']['mail']['port']) === false) {
runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) CONNECT - " . socket_strerror(socket_last_error()), 0);
return false;
}

$lastmessage = socket_read($fp, 512); //fgets
if (substr($lastmessage, 0, 3) != '220') {
runlog('SMTP', "{$_G[setting][mail][server]}:{$_G[setting][mail][port]} CONNECT - $lastmessage", 0);
return false;
}
socket_write($fp,/**
* ($_G['setting']['mail']['auth'] ? 'EHLO' : 'HELO').
*/ "HELO uchome\r\n"); //fputs
$lastmessage = socket_read($fp, 512);
if (substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) HELO/EHLO - $lastmessage", 0);
return false;
} while (1) {
if (substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) {
break;
}
$lastmessage = socket_read($fp, 512);
}

if ($_G['setting']['mail']['auth']) {
socket_write($fp, "AUTH LOGIN\r\n");
$lastmessage = socket_read($fp, 512);
if (substr($lastmessage, 0, 3) != 334) {
runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) AUTH LOGIN - $lastmessage", 0);
return false;
}

socket_write($fp, base64_encode($_G['setting']['mail']['auth_username']) . "\r\n");
$lastmessage = socket_read($fp, 512);
if (substr($lastmessage, 0, 3) != 334) {
runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) USERNAME - $lastmessage", 0);
return false;
}

socket_write($fp, base64_encode($_G['setting']['mail']['auth_password']) . "\r\n");
$lastmessage = socket_read($fp, 512);
if (substr($lastmessage, 0, 3) != 235) {
runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) PASSWORD - $lastmessage", 0);
return false;
}

$email_from = $_G['setting']['mail']['from'];
}

socket_write($fp, "MAIL FROM: <" . preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from) . ">\r\n");
$lastmessage = socket_read($fp, 512);
if (substr($lastmessage, 0, 3) != 250) {
socket_write($fp, "MAIL FROM: <" . preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from) . ">\r\n");
$lastmessage = socket_read($fp, 512);
if (substr($lastmessage, 0, 3) != 250) {
runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) MAIL FROM - $lastmessage", 0);
return false;
}
}

socket_write($fp, "RCPT TO: <" . preg_replace("/.*\<(.+?)\>.*/", "\\1", $toemail) . ">\r\n");
$lastmessage = socket_read($fp, 512);
if (substr($lastmessage, 0, 3) != 250) {
socket_write($fp, "RCPT TO: <" . preg_replace("/.*\<(.+?)\>.*/", "\\1", $toemail) . ">\r\n");
$lastmessage = socket_read($fp, 512);
runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) RCPT TO - $lastmessage", 0);
return false;
}

socket_write($fp, "DATA\r\n");
$lastmessage = socket_read($fp, 512);
if (substr($lastmessage, 0, 3) != 354) {
runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) DATA - $lastmessage", 0);
return false;
}

$timeoffset = $_G['setting']['timeoffset'];
if (function_exists('date_default_timezone_set')) {
@date_default_timezone_set('Etc/GMT' . ($timeoffset > 0 ? '-' : '+') . (abs($timeoffset)));
}

$headers .= 'Message-ID: <' . date('YmdHs') . '.' . substr(md5($email_message . microtime()), 0, 6) . rand(100000, 999999) . '@' . $_SERVER['HTTP_HOST'] . ">{$maildelimiter}";
socket_write($fp, "Date: " . date('r') . "\r\n");
socket_write($fp, "To: " . $email_to . "\r\n");
socket_write($fp, "Subject: " . $email_subject . "\r\n");
socket_write($fp, $headers . "\r\n");
socket_write($fp, "\r\n\r\n");
socket_write($fp, "$email_message\r\n.\r\n");
$lastmessage = socket_read($fp, 512);
if (substr($lastmessage, 0, 3) != 250) {
runlog('SMTP', "({$_G[setting][mail][server]}:{$_G[setting][mail][port]}) END - $lastmessage", 0);
}
socket_write($fp, "QUIT\r\n");

return true;

?>

[/php]

5.提交修改过的文件

6.以站长身份进入论坛后台管理中心→站长→邮件设置

发送方式选择 通过 SOCKET 连接 SMTP 服务器发送(支持 ESMTP 验证)

填写SMTP服务器及端口等信息

邮件头分隔符用LF作为分隔符

7.提交生效


本文固定链接: http://www.devba.com/index.php/archives/1255.html | 开发吧

BAE Discuz X2.5 发送邮件方法:目前有4 条留言

  1. 地板
    Harvey:

    博主您好,我安装了X2.5这个版本后,提示系统插件未初始化 点击修复也不管用,不知是怎么回事?

    2013-11-01 下午 8:53
  2. 板凳
    渴望蓝天:

    elseif($_G['setting']['mail']['mailsend'] == 2) {

    /**
    * 将fsocketopen改为socket_create
    */
    /**…….
    这个意思吗?放了不行啊

    还有就是我的discuz X3在bae上的网站为啥特别慢呢。Processed in 至少3s左右。。

    2013-08-30 下午 4:17
    • 渴望蓝天:

      哦对,这个是不是不支持X3

      2013-08-30 下午 4:17
  3. 沙发
    hankcs:

    博主好样的,bae不支持fsockopen,用这种方法就可以解决了,博主准备什么时候出X3.0?不嫌弃的话我可以帮忙

    2013-07-16 下午 10:31