nginx php mysql 邮箱_nginx 邮件代理认证(imap pop smtp)

nginx安装的时候要编译添加mail 支持 --with-mail --with-mail_ssl_module

nginx配置文件

worker_processes 4;

error_log logs/error.log info;

events {

worker_connections 1024;

}

mail {

auth_http 127.0.0.1:8070;

pop3_capabilities "TOP" "USER";

imap_capabilities "IMAP4rev1" "UIDPLUS";

smtp_capabilities "SIZE 10485760" ENHANCEDSTATUSCODES 8BITMIME DSN;

server {

listen 110;

protocol pop3;

proxy on;

}

server {

listen 143;

protocol imap;

proxy on;

}

server {

listen 25;

protocol smtp;

proxy on;

smtp_auth login plain;

xclient off;

}

}

auth 认证127.0.0.1:8070 这边是采用的 tornado

#!/usr/bin/env python

#coding:utf-8

import tornado.ioloop

import tornado.web

import tornado.httpserver

class IndexHandler(tornado.web.RequestHandler):

def get(self):

auth_user = self.request.headers['Auth-User']

auth_pass = self.request.headers['Auth-Pass']

client_ip = self.request.headers['Client-Ip']

auth_protocol = self.request.headers['Auth-Protocol']

self.set_header('Auth-Status','OK')

self.set_header('Auth-Server','imap.mail.com')

self.set_header('Auth-Port','143')

return

application = tornado.web.Application([

(r"/", IndexHandler),

])

if __name__ == "__main__":

application.listen(8070)

tornado.ioloop.IOLoop.current().start()

php版api认证

if(!isset($_SERVER ["HTTP_AUTH_USER"] ) || ! isset($_SERVER ["HTTP_AUTH_PASS"] )) {

header("Auth-Status: Invalid login or password");

exit;

}

$auth_user = $_SERVER['HTTP_AUTH_USER'];

$auth_pass = $_SERVER['HTTP_AUTH_PASS'];

$auth_protocol = $_SERVER['HTTP_AUTH_PROTOCOL'];

list($uid, $domain) = explode("@", $auth_user);

$conn = mysql_connect("127.0.0.1","root","123456");

if (!$conn){

die('Could not connect: ' . mysql_error());

}

mysql_select_db("mail", $conn);

$result = mysql_query("select host from mail where pdomain='".$domain."' limit 1");

while($row=mysql_fetch_array($result)){

$host = $row['host'];

}

/* PHP7使用mysqli连接数据库

$con = new mysqli($dbhost,$dbuser,$dbpwd,$dbname);

if(mysqli_connect_errno()){

echo "mysqli connect fail: " .mysqli_connect_error();

exit();

}

$result = $con->query("select host from mail where pdomain='".$domain."' limit 1");

while($row=mysqli_fetch_array($result)){

$host = $row['host'];

}

*/

switch($host){

case "mail1.com":

$backend = "192.168.1.1";

break;

case "mail2.com":

$backend = "192.168.1.2";

break;

}

if ($auth_protocol == "imap"){

//header("Auth-Status:OK");

header("Auth-Server:$backend");

header("Auth-Port:143");

exit();

}

else if($auth_protocol == "pop3"){

//header("Auth-Status:OK");

header("Auth-Server:$backend");

header("Auth-Port:110");

exit();

}

else if($auth_protocol == "smtp"){

#header("Auth-Status:OK");

header("Auth-Server:$backend");

header("Auth-Port:25");

exit();

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值