分类 代码 下的文章

  • 创建php文件并执行测试 vi mailtest.php
<?php
function getHost($host)
{  //解析域名
    $getHost = gethostbyname($host);
    echo "尝试连接 $host ...<br>\r\n ";
    if (!$getHost) {
        echo "解析失败 (1)<HR>";
    } elseif ($getHost == $host) {
        echo "解析失败 (2): 可能是一个无效的主机名<HR>";
    } else {
        echo "域名解析为 $getHost ...<br>\r\n";
        openHost($host);
    }
}

function openHost($host)
{  //连接主机

    if (function_exists('fsockopen')) {
        $fp = fsockopen($host, 25, $errno, $errstr, 60);
    } elseif (function_exists('pfsockopen')) {
        echo "服务器不支持Fsockopen,尝试pFsockopen函数 ...<br>\r\n";
        $fp = pfsockopen($host, 25, $errno, $errstr, 60);
    } else
        exit('服务器不支持Fsockopen函数');

    if (!$fp) {
        echo "代号:$errno,<br>\n错误原因:$errstr<HR>";
    } else {
        echo "SMTP服务器连接ok!<br>\r\n";
        fwrite($fp, "");
        $out0 = fgets($fp, 128);
#echo $out0;
        foreach (["220"] as $port) {
            if (strncmp($out0, $port, 3) == 0) { // 判断三位字符内容
                echo $port . ' SMTP服务端响应正常<HR>';
            } else {
                echo $port . '服务器端错误<HR>';
            }
        }
    }
}

//SMTP服务器地址

foreach (["smtp.163.com", "smtp.sina.cn", "smtp.sina.com", "smtp.qq.com", "smtp.126.com", "smtp.gmail.com"] as $host) {
    echo getHost($host) . "\n\n\n";
}

if (function_exists('mail')) {
    echo 'mail方法正常';
}

如果以上正常,那么就检查是否邮箱帐号的问题

  • 查看是否开启openssl
php -r 'echo !extension_loaded('openssl')?"Not Available":"Available";';
执行结果
PHP Warning:  Use of undefined constant openssl - assumed 'openssl' (this will throw an Error in a future version of PHP) in Command line code on line 1
后来发现把内容写入到php文件中执行正常的
  • 未配置CA证书的情况
先查询配置文件路径
bash-4.4# php -i | grep cafile
openssl.cafile => no value => no value

创建ca文件, 把下载的CA文件的内容粘贴进去 [下载最新的CA cert文件](https://curl.haxx.se/ca/cacert.pem)
vi /etc/ssl/cacert.pem

修改php配置文件, 添加下面的内容
bash-4.4# vi /etc/php7/php.ini
openssl.cafile = /etc/ssl/cacert.pem
curl.cainfo = /etc/ssl/cacert.pem

判断是否开启openssl
php -m | grep openssl
如果没有开启,搜索php.ini下面内容, 把前面的;删除
extension=openssl


再次确认配置是否生效
bash-4.4# php -i | grep cafile
openssl.cafile => /etc/ssl/cacert.pem => /etc/ssl/cacert.pem
  • 最终还是查到了原因, 配置的域名邮箱的域名解析的关联mx值丢失导致的
MX记录
未设置或暂未生效

参考链接: https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

  • array_diff_assoc2
array_diff_assoc 在比对两个带key的数组时, 如果右边的数组没有对应的key, 左边对应的key是数组, 那没事, 不然就会转换左边的内容为字符串带来警告
function array_diff_assoc2($array, $array2)
{
    $d = [];
    foreach ($array as $key => $item) {
        if (array_key_exists($key, $array2)) {
            if (is_array($item)) {
                if (is_array($array2[$key])) {
                    if ($item != $array2[$key]) {
                        $d[$key] = $item;
                    }
                } else if (
                    (strpos($array2[$key], '[') === false 
                        && strpos($array2[$key], '{') === false)
                    || $item != json_decode($array2[$key], true)
                ) {
                    $d[$key] = $item;
                }
            } else if (is_array($array2[$key])) {
                if ($item != json_encode($array2[$key], 256)) {
                    $d[$key] = $item;
                }
            } else if ($item . '' != $array2[$key] . '') {
                $d[$key] = $item;
            }
        } else {
            $d[$key] = $item;
        }
    }
    return $d;
}
  • json_encode [php特殊环境下无法使用该方法的过度办法]
function json_encodes($arr)
{
    $parts = array();
    $is_list = false;
    //Find out if the given array is a numerical array
    if (is_array($arr)) {
        $keys = array_keys($arr);
        $max_length = count($arr) - 1;
        if (
            !is_null($keys)
            && array_key_exists('0', $keys)
            && ($keys [0] === 0)
            && ($keys [$max_length] === $max_length)) { //See if the first key is 0 and last key is length - 1
            $is_list = true;
            for ($i = 0; $i < count($keys); $i++) { //See if each key correspondes to its position
                if ($i != $keys [$i]) { //A key fails at position check.
                    $is_list = false; //It is an associative array.
                    break;
                }
            }
        }
        foreach ($arr as $key => $value) {
            if (is_array($value)) { //Custom handling for arrays
                if ($is_list)
                    $parts [] = json_encodes($value); /* :RECURSION: */
                else
                    $parts [] = '"' . $key . '":' . json_encodes($value); /* :RECURSION: */
            } else {
                $str = '';
                if (!$is_list)
                    $str = '"' . $key . '":';
                //Custom handling for multiple data types
                if (is_numeric($value) && $value === $value * 1) {
                    $str .= $value;
                } elseif ($value === false)
                    $str .= 'false'; //The booleans
                elseif ($value === true)
                    $str .= 'true';
                elseif ($value === null)
                    $str .= 'null';
                else
                    $str .= '"' . addslashes($value) . '"'; //All other things
                // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
                if (is_string($str)) {
                    $str = str_replace(PHP_EOL, '\r\n', $str);
                }
                $parts [] = $str;
            }
        }
    }

    $json = implode(',', $parts);
    if ($is_list)
        return '[' . $json . ']'; //Return numerical JSON
    return '{' . $json . '}'; //Return associative JSON
}

让在textarea中按tab符号时输入4个空格, 使用时放于页面末尾即可

HTMLTextAreaElement.prototype.getCaretPosition = function () {
    //return the caret position of the textarea
    return this.selectionStart;
};
HTMLTextAreaElement.prototype.setCaretPosition = function (position) {
    //change the caret position of the textarea
    this.selectionStart = position;
    this.selectionEnd = position;
    this.focus();
};
HTMLTextAreaElement.prototype.hasSelection = function () {
    //if the textarea has selection then return true
    if (this.selectionStart == this.selectionEnd) {
        return false;
    } else {
        return true;
    }
};
HTMLTextAreaElement.prototype.getSelectedText = function () {
    //return the selection text
    return this.value.substring(this.selectionStart, this.selectionEnd);
};
HTMLTextAreaElement.prototype.setSelection = function (start, end) {
    //change the selection area of the textarea
    this.selectionStart = start;
    this.selectionEnd = end;
    this.focus();
};
var textarea = document.getElementsByTagName('textarea')[0];
textarea.onkeydown = function (event) {
    //support tab on textarea
    if (event.keyCode == 9) { //tab was pressed
        var newCaretPosition;
        newCaretPosition = textarea.getCaretPosition() + " ".length;
        textarea.value = textarea.value.substring(0, textarea.getCaretPosition()) + " " + textarea.value.substring(textarea.getCaretPosition(), textarea.value.length);
        textarea.setCaretPosition(newCaretPosition);
        return false;
    }
    if (event.keyCode == 8) {
        //backspace
        if (textarea.value.substring(textarea.getCaretPosition() - 4, textarea.getCaretPosition()) == " ") {
            //it's a tab space
            var newCaretPosition;
            newCaretPosition = textarea.getCaretPosition() - 3;
            textarea.value = textarea.value.substring(0, textarea.getCaretPosition() - 3) + textarea.value.substring(textarea.getCaretPosition(), textarea.value.length);
            textarea.setCaretPosition(newCaretPosition);
        }
    }
    if (event.keyCode == 37) { //left arrow
        var newCaretPosition;
        if (textarea.value.substring(textarea.getCaretPosition() - 4, textarea.getCaretPosition()) == " ") {
            //it's a tab space
            newCaretPosition = textarea.getCaretPosition() - 3;
            textarea.setCaretPosition(newCaretPosition);
        }
    }
    if (event.keyCode == 39) {
        //right arrow
        var newCaretPosition;
        if (textarea.value.substring(textarea.getCaretPosition() + 4, textarea.getCaretPosition()) == " ") {
            //it's a tab space
            newCaretPosition = textarea.getCaretPosition() + 3;
            textarea.setCaretPosition(newCaretPosition);
        }
    }
}