[[CodeIgniter]] > Qdmail

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

#contents

* Qdmailとは? [#i9e2d3cb]
- PHP高機能日本語メール送信ライブラリ・文字化けフリー - Qdmail - PHP::Mail Library
http://hal456.net/qdmail/
>Qdmailとは、PHPのマルチバイト環境(特に日本語)にて、「文字化けしない」「簡単に"デコメ(デコメール)"やHTMLメール等の電子メールを送信することができる」メールクラスライブラリです。文字化け完全制覇を目指しています。

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

* CodeIgniterでQdmailを使う方法 [#y901f9f4]

** コントローラーから直接呼び出す方法 [#aa6e3f16]
公式サイトにあった説明。

-CodeIgniterで日本語メール送信
http://hal456.net/qdmail/code_igniter

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

#code(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のライブラリにする方法 [#j1cf5d96]
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の中身は以下のようにする。
#code(php){{
<?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で利用

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

#code(php){{
<?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のメソッドを同じになります。
$this->mailer->の後ろのメソッドは、Qdmailのメソッドと同じになります。


** メソッドチェーンを可能にする方法 [#pf3cdf71]
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) [#r55d08f6]

#code(php){{
<?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 [#h5ec86f6]

#code(php){{
<?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も利用されていました。

* まとめ [#f22bed82]

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

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS