CodeIgniter > Qdmail

PHP製のメール送信ライブラリ「Qdmail」

Qdmailとは?

  • PHP高機能日本語メール送信ライブラリ・文字化けフリー - Qdmail - PHP::Mail Library
    http://hal456.net/qdmail/

    Qdmailとは、PHPのマルチバイト環境(特に日本語)にて、「文字化けしない」「簡単に"デコメ(デコメール)"やHTMLメール等の電子メールを送信することができる」メールクラスライブラリです。文字化け完全制覇を目指しています。

メール送信の文字化けで困ったら、Qdmailと。
ありがたく使わせていただきます。m(__)m

CodeIgniterでQdmailを使う方法

コントローラーから直接呼び出す方法

公式サイトにあった説明。

CIことCodeIgniterでもQdmailを利用して簡単に日本語メールを送信することができます。(CodeIgniter1.6.3にて動作確認)
携帯デコメもOK

  1
  2
  3
  4
  5
  6
  7
  8
<?php include('qdmail.php');
 
class Mail extends Controller {
    function sendmail(){
        $data = array('hello'=>'こんにちは');
        $content=$this->load->view('mailview',$data,true);
       qd_send_mail('html','address@example.com','件名',$content,'from@example.com');
     }
} ?>

$this->load->view メソッドの第3引数をtrueにするのがポイントです。
これで、$this->load->viewは画面に返すのでなく、画面に書くべき内容を返り値として渡してくれます。

このやり方だと、qdmail.phpをコントローラーと同じディレクトリ

/application/controllers/

に置かなきゃいけないので、管理しづらい。

やはり、ライブラリのファイルは、

/application/libraries/

に置いて使った方が、管理上は分かりやすいだろう。

CodeIgniterのライブラリにする方法

QdmailをCodeIgniterのライブラリとして使ってみよう。

(参考)CodeIgniter ライブラリの作成
http://codeigniter.jp/user_guide_ja/general/creating_libraries.html

qdmail.phpを

/application/libraries/qdmail.php

に置く。

CodeIgniterのユーザー作成ライブラリのファイル「Mailer.php」を、

/application/libraries/Mailer.php

に作成する。

Mailer.phpの中身は以下のようにする。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Library with Qdmail for CodeIgniter 2.0
 * http://hal456.net/qdmail/
 */
require_once 'qdmail.php';
 
class Mailer extends Qdmail
{
    public function __construct($option = array())
    {
        parent::__construct($option);
    }
}
 
/* End of file welcome.php */
/* Location: ./application/libraries/Mailer.php */
  • Qdmailを継承したMailerクラスを作成
  • Mailerというユーザーライブラリとして、CodeIgniterで利用

呼び出し

コントローラーからライブラリを呼び出して使う。
以下のようなメソッドを用意して、Qdmailを利用する。

  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
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Form extends CI_Controller
{
    public function index()
    {
        echo 'メールフォームのトップページです。';
    }
 
    public function send()
    {
        $mail['from_name'] = 'Kenshiro Kasumi';
        $mail['from']      = 'ken@hokutoshinken.jp';
        $mail['to']        = 'raoh@kenoh-gun.com';
        $mail['subject']   = '北斗同窓会のご案内';
        $mail['body']      = '来週の金曜日の予定です。';
             
        // sendmail
        if ($this->_sendmail($mail))
        {
            echo 'メール送信OK';
        }
        else
        {
            echo 'メール送信エラー';
        }
    }
 
    private function _sendmail($mail = array())
    {
        // Qdmail
        $this->load->library('mailer');
        
        $this->mailer->from($mail['from'], $mail['from_name']);
        $this->mailer->to($mail['to']);
        $this->mailer->subject($mail['subject']);
        $this->mailer->text($mail['body']); // テキストメール
        return $this->mailer->send();
    }
}
?>

$this->mailer->の後ろのメソッドは、Qdmailのメソッドと同じになります。

メソッドチェーンを可能にする方法

Qdmailを拡張して、メソッドチェーン(メソッドを連続して使用)ができるようにしたライブラリが紹介されていました。

  • CodeIgniter用のQdmailライブラリを作ったよ 2011-04-19
    http://d.hatena.ne.jp/localdisk/20110419/

    国産高機能メールライブラリのPHP高機能日本語メール送信ライブラリ・文字化けフリー - Qdmail - PHP::Mail Library , Quick and Detailed for MultibyteをCodeIgniter(以下CI)で使いやすいようにライブラリ化してみた。CI既存ののEmailライブラリとインターフェースは合わせてるので、使いやすいのではないかと。あと例によってメソッドチェーンできるようにしてます。

使用例(Controller)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
<?php
class Home extends CI_Controller {
 
    public function __construct() {
        parent::__construct();
    }
 
    public function index() {
        $this->load->library('mailer');
        $ret = $this->mailer->to('test@test.org', 'なまえ')
                     ->subject('テスト送信')
                     ->message('テストメッセージ')
                     ->from('hoge@hoge.com', 'テストほげ')
                     ->cc('test@test.com', 'テスト')
                     ->bcc('test@test.jp')
                     ->send();
        $this->load->view('home');
    }
}

$this->mailerの後に続けて、メソッドを連続して使用できるんですね!
記述が簡便になって、便利そうです。

application/libraries/Mailer.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
require_once 'qdmail.php';
require_once 'qdsmtp.php';
 
/**
 * Mail Library
 *
 * @author localdisk
 */
class Mailer {
 
    /**
     * mailer
     * 
     * @var Qdmail
     */
    private $_mailer;
 
    public function __construct($option = array()) {
        $this->_mailer = new Qdmail();
        $this->initialize($option);
    }
 
    /**
     * initialize
     * 
     * @param array $config 
     */
    public function initialize($config = array()) {
        if (strtolower($config['protocol']) === 'smtp') {
            $this->_mailer->smtp(TRUE);
            $param = array();
            if ($config['smtp_user'] === '' && $config['smtp_pass'] === '') {
                $param['protocol'] = 'SMTP';
            } else {
                $param['protocol'] = 'SMTP_AUTH';
                $param['user'] = $config['smtp_user'];
                $param['pass'] = $config['smtp_pass'];
                
            }
            $param['host'] = $config['smtp_host'];
            $param['port'] = (isset($config['smtp_port']) === FALSE) ? 25 : $config['smtp_port'];
            $this->_mailer->smtpServer($param);
        }
    }
 
    /**
     * to
     * 
     * @param  mixed  $addr
     * @param  mixed  $name
     * @param  boolean $add
     * @return Mailer 
     */
    public function to($addr = null, $name = null, $add = FALSE) {
        $this->_mailer->to($addr, $name, $add);
        return $this;
    }
 
    /**
     * subject
     * 
     * @param  string $subj
     * @return Mailer 
     */
    public function subject($subj = null) {
        $this->_mailer->subject($subj);
        return $this;
    }
 
    /**
     * text
     * 
     * @param  string $cont
     * @param  string $length
     * @param  string $charset
     * @param  string $enc
     * @param  string $org_charset
     * @return Mailer 
     */
    public function text($cont, $length = null, $charset = null, $enc = null, $org_charset = null) {
        $this->_mailer->text($cont, $length, $charset, $enc, $org_charset);
        return $this;
    }
 
    /**
     * message
     * Email Libarary の互換メソッド
     * 
     * @param  string $body
     * @return Mailer 
     */
    public function message($body) {
        $this->text($body);
        return $this;
    }
 
    /**
     * from
     * 
     * @param  string $addr
     * @param  string $name
     * @return Mailer 
     */
    public function from($addr = null, $name = null) {
        $this->_mailer->from($addr, $name);
        return $this;
    }
 
    /**
     * send
     * 
     * @param mixed $option 
     */
    public function send($option = null) {
        $this->_mailer->send($option);
    }
 
    /**
     * cc
     * 
     * @param  mixed   $addr
     * @param  mixed   $name
     * @param  boolean $add
     * @return Mailer 
     */
    public function cc($addr = null, $name = null, $add = false) {
        $this->_mailer->cc($addr, $name, $add);
        return $this;
    }
 
    /**
     * bcc
     * 
     * @param  mixed   $addr
     * @param  mixed   $name
     * @param  boolean $add
     * @return Mailer 
     */
    public function bcc($addr = null, $name = null, $add = false) {
        $this->_mailer->bcc($addr, $name, $add);
        return $this;
    }
 
    /**
     * reply_to
     * 
     * @param  mixed   $addr
     * @param  mixed   $name
     * @return Mailer 
     */
    public function reply_to($addr = null, $name = null) {
        $this->_mailer->replyto($addr, $name);
        return $this;
    }
 
    /**
     * clear
     * 
     * @return Mailer
     */
    public function clear() {
        $this->_mailer->reset();
        return $this;
    }
 
    /**
     * attach
     * 
     * @param  string  $param
     * @param  boolean $add
     * @return Mailer 
     */
    public function attach($param, $add = false) {
        $this->_mailer->attach($param, $add);
        return $this;
    }
 
    /**
     * print_debugger
     * 
     * @return string 
     */
    public function print_debugger() {
        $msg = '<pre>';
        $msg =  'to:  '          . print_r($this->_mailer->to, TRUE)           . "\n";
        $msg .= 'cc:'            . print_r($this->_mailer->cc, TRUE)           . "\n";
        $msg .= 'bcc:'           . print_r($this->_mailer->bcc, TRUE)          . "\n";
        $msg .= 'from:'          . print_r($this->_mailer->from, TRUE)         . "\n";
        $msg .= 'replyto:'       . print_r($this->_mailer->replyto, TRUE)      . "\n";
        $msg .= 'otherheader:'   . print_r($this->_mailer->other_header, TRUE) . "\n";
        $msg .= 'subject:'       . print_r($this->_mailer->subject, TRUE)      . "\n";
        $msg .= 'subject:'       . print_r($this->_mailer->subject, TRUE)      . "\n";
        $msg .= 'body:'          . print_r($this->_mailer->content, TRUE)      . "\n";
        $msg .= '</pre>';
        return $msg;
    }
 
    /**
     * __call
     * 
     * @param mixed $name
     * @param mixed $arguments 
     * @return mixed
     */
    public function __call($name, $arguments) {
        if (!method_exists($this->_mailer, $name)) {
            return  FALSE;
        }
        if (!is_callable(array($this->_mailer, $name), TRUE)) {
            return FALSE;
        }
        return call_user_func_array(array($this->_mailer, $name), $arguments);
    }
}

qdmail.phpと共に、qdsmtp.phpも利用されていました。

まとめ

PHPでメール送信をするときに文字化けに悩まされたら、Qdmailを使うと大概解決します。
Qdmailは、MITライセンスで配布されていました。(=とても自由に使えます)
CodeIgniterでもQdmailを使わせていただければと存じます。


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2011-07-10 (日) 20:33:58 (4673d)