CakePHPでGmailを使ってメール送信

必要なライブラリを用意


以下のライブラリをDLして、app/contollers/componentsに配置します。

・qdmail
 ダウンロードdownload - Qdmail - PHP::Mail Library , Quick and Detailed for Multibyte
・qdsmtp
 ダウンロード - Qdsmtp-Simple SMTP Mailer for PHP

コントローラ


app/app_controller.php

<?php
class AppController extends Controller {
	var $mail_param = array(
		'host' => 'ssl://smtp.gmail.com',
		'port' => 465,
		'from' => 'admin@gmail.com',
		'protocol' => 'SMTP_AUTH',
		'user' => 'sample@gmail.com', // Gmailのメールアドレス
		'pass' => 'password',  // Googleのアカウントのパスワード
	);
}
?>


app/controllers/mails_controller.php

<?php
class MailsController extends AppController {
	// (中略)
	var $components = array('Qdmail');	
	function send() {
		$this->Qdmail->smtp(true);
		$this->Qdmail->smtpServer($this->mail_param);
		$this->Qdmail->to('yamada@gmail.com', '山田太郎様');
		$this->Qdmail->subject('ご注文ありがとうございます!');
		$this->Qdmail->from('admin@gmail.com', 'XXXオンラインショップ');
		$text = 'これはメール配信テストです。';
		$this->Qdmail->text($text);
		$this->Qdmail->send();
	}
}
動作確認


http://localhost/mails/send でメール送信されればOKです。