从php中的电子邮件服务器获取电子邮件

时间:2013-09-11 10:43:19

标签: php email email-integration

我编写代码来获取来自不同服务器(Gmail,yahoo,hotmail等)的电子邮件。我在gmail服务器上测试了我的代码并且它工作正常,但是对雅虎而言代码不起作用。我的代码是:

$data array will be like 

$data['type'] = 'pop3 or imap';
$data['protocol_type'] = 'ssl or tls or none';
$data['port'] = '993 or 995';
$data['login_name'] = 'user name';
$data['password'] = 'user password';
public function testconnectionAction()
    {

        $data = $_POST;


        if($data['type'] == 'pop3' && $data['protocol_type'] == 'none')
        {
               $hostname = '{'.$data['host'].':'.$data['port'].'/pop3}INBOX';
             $username = $data['login_name'];
              $password = $data['password'];
            $imap = imap_open($hostname,$username,$password)or die(imap_last_error());

            if (!imap_ping($imap)) {
                echo "Test Failed.";
                }else{
                      echo "Test Successfull.";
                }
        }elseif($data['type'] == 'imap' && $data['protocol_type'] == 'none') {
             $hostname = '{'.$data['host'].':'.$data['port'].'}INBOX';
             $username = $data['login_name'];
              $password = $data['password'];
            $imap = imap_open($hostname,$username,$password);
            if (!imap_ping($imap)) {
                echo "Test Failed.";
                }else{
                      echo "Test Successfull.";
                }
        }
      if($data['type'] == 'pop3' && $data['protocol_type'] == 'ssl')
      {
             $hostname = '{'.$data['host'].':'.$data['port'].'/pop3/ss}INBOX';
             $username = $data['login_name'];
             $password = $data['password'];
            $imap = imap_open($hostname,$username,$password);
      //  
            if (!imap_ping($imap)) {
                echo "Test Failed.";
                }else{
                      echo "Test Successfull.";
                }
      }
      if($data['type'] == 'imap' && $data['protocol_type'] == 'ssl')
      {
        $hostname = '{'.$data['host'].':'.$data['port'].'/imap/ssl/novalidate-cert}INBOX';
        $username = $data['login_name'];
        $password = $data['password'];

/* try to connect */
        $inbox = imap_open($hostname,$username,$password);

            if (!imap_ping($inbox)) {
                echo "Test Failed.";
                }else{
                      echo "Test Successfull.";
                }
      }

       if($data['type'] == 'pop3' && $data['protocol_type'] == 'tls')
      {
             $hostname = '{'.$data['host'].':'.$data['port'].'/pop3/tls}INBOX';
             $username = $data['login_name'];
             $password = $data['password'];
             $imap = imap_open($hostname,$username,$password)or die(imap_last_error());
            if (!imap_ping($imap)) {
                echo "Test Failed.";
                }else{
                      echo "Test Successfull.";
                }
      }
      if($data['type'] == 'imap' && $data['protocol_type'] == 'tls')
      {
        $hostname = '{'.$data['host'].':'.$data['port'].'/imap/tls}INBOX';
             $username = $data['login_name'];
             $password = $data['password'];
             $imap = imap_open($hostname,$username,$password);

            if (!imap_ping($imap)) {
                echo "Test Failed.";
                }else{
                      echo "Test Successfull.";
                }
      }

    }

我如何在雅虎上测试该代码是否正常工作。我尝试进入pop.yahoo.com获取主机和端口995,但它不起作用。

1 个答案:

答案 0 :(得分:0)

在搜索和测试后,我找到了用于测试的雅虎电子邮件设置。

Incoming Server: imap.mail.yahoo.com Port 993 with SSL
Outgoing Server: smtp.bizmail.yahoo.com Port 465 with SSL (requires Authentication to be enabled)
Username: Your Business Email address (e.g. name@mycompany.com)
Password: Your Business Email password 

对于Hotmail,我使用下面的代码:

Outlook.com Incoming Mail Server (POP3) - pop3.live.com (logon using Secure Password Authentication – SPA,SSL enabled mail server port: 995).Hope it help someone like me.