美文网首页
php 检测邮箱的 imap 和 smtp服务器

php 检测邮箱的 imap 和 smtp服务器

作者: 骑蚂蚁上高速_jun | 来源:发表于2021-04-21 11:03 被阅读0次

    本文章说明 php 检测邮箱imap和smtp服务器, 实际在生产中如果有条件的话,建议使用 java做邮件服务器,因为更专业,性能更好,功能更强大。。

    php 验证 imap服务

    php 进行imap验证的时候,依赖 php 的 imap C语言扩展。 请先安装 imap扩展,进入php源码包目录 ext目录,自行编译吧。很简单的

    imap 验证代码示例
    function checkImapUtils(){
        $imapServer = "imap.qiye.163.com";
        $imapPort = 993;
        $imapRule = "ssl"; // 可以是 字符串SSL 和 字符串空 两种可选值
        $email =  "Brian@universalquartzstone.com";
        $password = "XXXXXXXXXXXX"; // 你的密码
    
        if ($imapRule) {
            $mbox = "{" . $imapServer . ":" . $imapPort . "/imap/" . $imapRule . "}";
        } else {
            $mbox = "{" . $imapServer . ":" . $imapPort . "/imap" . "}";
        }
        try {
            $r = @imap_open($mbox, $email, $password);
            if (@imap_gc($r, IMAP_GC_ELT)) {
                return true;
            }
            if ($error = imap_last_error()) {
                // 检测到是 邮箱ssl安全证书过期的话,需要绕开ssl证书一次在进行验证
                if($imapRule && strpos($error,"Certificate failure") !== false){
                    $mbox = "{" . $imapServer . ":" . $imapPort . "/imap/" . $imapRule . "/novalidate-cert}";
                    $r = @imap_open($mbox, $email, $password);
                    if (@imap_gc($r, IMAP_GC_ELT)) {
                        return true;
                    }
                }
                throw new RuntimeException(imap_last_error());
            }
            if ($r) {
                if (!imap_ping($r)) {
                    throw new RuntimeException("请检查 imap 参数");
                }
                @imap_gc($r, IMAP_GC_ELT);
                return true;
            } else {
                throw new RuntimeException("请检查 imap 参数");
            }
        } catch (\ErrorException $exception) {
            throw new RuntimeException("邮箱地址或密码不匹配;如果邮箱设置了授权码,请输入授权码。");
        }
    }
    
    /**
     * 邮箱 imap 服务检测
     * 检测的速度与你本地网络环境因素有一部分关系
     */
    
    try{
        var_dump(checkImapUtils());
    }catch(Exception $e){
        var_dump($e->getMessage());
    }
    

    php 的smtp服务验证

    依赖 php 扩展包 安装命令 composer require swiftmailer/swiftmailer

    use RuntimeException;
    use Swift_SmtpTransport;
    use Swift_Mailer;
    use Swift_Preferences;
    use Swift_Message;
    use Swift_Attachment;
    use Swift_Mime_SimpleMessage;
    use App\Request\Mailer\HomeRequest;
    use Exception;
    use Swift_Image;
    
    smtp 检测示例
    function checkSmtpUtils(){
        $smtpServer = "smtp.qiye.163.com";
        $smtpPort= 465;
        $smtpRule = "ssl"; // 字符串ssl 或 空
        $username = "Brian@universalquartzstone.com";
        $password = "邮箱的登录密码或 授权码";
        return (new Swift_SmtpTransport($smtpServer, $smtpPort, $smtpRule))->
        setUsername($username)->
        setTimeout(60)->
        setPassword($password)->
        ping(); // 成功 返回 true , 失败返回false
    }
    
    
    

    相关文章

      网友评论

          本文标题:php 检测邮箱的 imap 和 smtp服务器

          本文链接:https://www.haomeiwen.com/subject/vinfpktx.html