忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



2024/03/29 17:33 |
PHP1-58:PHPでIMAP

3分LifeHacking:
Gmailが落ちたときに“アクセス”する方法を考える
http://www.itmedia.co.jp/bizid/articles/0902/25/news099.html

自力でIMAPすりゃいいじゃん?
ということで繋いでみた。

メールの送信はmail()やPear::Mail、そしてQdmailなんかでさくっと行うことができますが、受信についてはこれといったものがありません。

GmailはIMAPに対応しているので、PHPのIMAP関数でこりこり書いていくしかないようです。
IMAPを使用するので、最初にGmailの設定で「IMAPを許可」する必要があります。
http://ja.wikipedia.org/wiki/Internet_Message_Access_Protocol
http://jp.php.net/manual/ja/book.imap.php

gmail_imap.class.php 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
    //初期設定
    
class gMail implements Iterator{
    //メールサーバ情報
    protected $params= array(
         'host'     => 'imap.gmail.com'
        ,'port'     => 993
        ,'auth'     => true
        ,'username' =>''
        ,'password' =>''
        ,'mailbox'  =>''
    );
    
    //IMAPインスタンス
    protected $imap=array();
    protected $mailbox='';
    
    //受信メール
    private $loop=0;
    protected $mail_data=array();
    
    //コンストラクタ
    public function __construct($user,$pass){
        $this->params['username']=$user;
        $this->params['password']=$pass;
        
        //IMAP:mailboxの作成
        $this->mailbox
            ='{'.$this->params['host'].':'.$this->params['port'].'/ssl}INBOX';
        //インスタンス
        $this->imap=imap_open(
            $this->mailbox,$this->params['username'],$this->params['password']
        );
    }
    
    //デストラクタ
    public function __destruct(){
        imap_close($this->imap);
    }
    
    //全件を取得
    public function getAll(){
        $count=$this->count();
        for($i=1;$i<$count+1;$i++){
            $this->mail_data[]=$this->get($i);
        }
        return $this->mail_data;
    }
    
    //件数を取得
    public function count(){
        $this->count=imap_num_msg($this->imap);
        return $this->count;
    }
    
    //受信
    public function get($num){
            $this->tmp=false;
        //ヘッダ
            $this->tmp['head']=imap_headerinfo($this->imap,$num);
            $this->tmp['head']->uid=imap_uid($this->imap,$num);
        //本文
            $this->tmp['body']=imap_body($this->imap,$num);
        //デコード
            $tmp=$this->_decode($this->tmp);
        //必要なぶんだけ返却
            $ret['to']=$tmp['head']->toaddress;
            $ret['to_address']=$tmp['to_address'];
            $ret['from']=$tmp['from'];
            $ret['from_address']=$tmp['from_address'];
            $ret['date']=$tmp['head']->date;
            $ret['subject']=$tmp['subject_decode'];
            $ret['body_decode']=$tmp['body_decode'];
            return $ret;
    }
    
    //デコード
    private function _decode($mail_data){
        //件名
            $mail_data['subject_decode']
                =mb_decode_mimeheader($mail_data['head']->subject);
        //宛先
            if(isset($mail_data['head']->to[0]->personal)){
                $mail_data['to']=mb_decode_mimeheader(
                    $mail_data['head']->to[0]->personal
                );
            }else{
                $mail_data['to']=$mail_data['head']->toaddress;
            }
            $mail_data['to_address']
                =$mail_data['head']->to[0]->mailbox
                .'@'.$mail_data['head']->to[0]->host;
        //差出人
            if(isset($mail_data['head']->from[0]->personal)){
                $mail_data['from']=mb_decode_mimeheader(
                    $mail_data['head']->from[0]->personal
                );
            }else{
                $mail_data['from']=$mail_data['head']->fromaddress;
            }
            $mail_data['from_address']
                =$mail_data['head']->from[0]->mailbox
                .'@'.$mail_data['head']->from[0]->host;
            
        //本文
            if(isset($mail_data['body'])){
                $mail_data['body_decode']=mb_convert_encoding(
                    $mail_data['body'],'UTF-8','JIS'
                );
            }
            return $mail_data;
    }
    
    //イテレータ
    function current(){
        return $this->mail_data[$this->loop];
    }
    function next(){
        $this->loop++;
    }
    function key(){
        return $this->loop;
    }
    function rewind(){
        $this->loop=0;
    }
    function valid(){
        if(isset($this->mail_data[$this->loop])){
            return true;
        }else{
            return false;
        }
    }
    
#↓クラスのおわり
}

gmail_imap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
    //初期設定
    header("Content-type: text/html; charset=utf-8");
    require_once('./gmail_imap.class.php');
    
    //ユーザ情報
    $mail_user='メアド';
    $mail_pass='パスワード';
    
    //アカウント
    $gmail = new gMail($mail_user,$mail_pass);
    
    //一件取得
    $ret=$gmail->get('8');
    
    //全件取得
    $ret=$gmail->getAll();
    foreach($ret as $key=>$val){
        var_dump($key,$val);
    }

何気にメール一覧の取得が難しいです。
imap_headersは、ヘッダ情報が文字列で返ってくるという謎仕様です。
string(83) " U       4)25-Feb-2009 =?ISO-2022-JP?B?GyRC =?ISO-2022-JP?B?MTUbJEI8f (10362 chars)"
こんなの送ってこられてもどうしようもないのですが…
他に全ヘッダを取得という関数が無いようなので、全メールについてimap_fetchheaderなりimap_headerinfoなりをせざるを得ないとかいうことになっています。
どうせなら一緒でいいやということでついでにimap_bodyも実行して全メール取得にしてしまいました。
これでメールの件数を取得、特定のメールを受信、全メール受信のスクリプトができました。

mb_detect_encodingがJISをまったくチェックしてくれないのでJISべた書きだったり、そのせいで標準以外のエンコードがされているメールが読めなかったり、そもそもマルチパート対策が全くなされていなかったりとメーラとしては弱いです。
またIMAPにはフォルダ移動や削除などサーバ側のメールを操作する機能もたくさんあるのですが、現在は受信トレイのメールを受信する以外の機能はありません。
そこらへんは必要になったら加えていけば便利になっていくことでしょう。

 

まあ根本的に外部メーラ使った方が遙かに手っ取り早いというのは秘密だ。

PR


2009/03/02 16:06 | Comments(0) | TrackBack() | PHP

トラックバック

トラックバックURL:

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<PHP1-59:PHPで暗号化 | HOME | PHP1-57:PHPでQdsmtp>>
忍者ブログ[PR]