Contoller
[php]
App::uses(‘AppController’, ‘Controller’,’CakeEmail’, ‘Network/Email’);
public $components = array(‘Session’,’ContactMail’);
///////////////////////
public function contact($id = null) {
$this->Report->id = $id;
if (!$this->Report->exists()) {
throw new NotFoundException(__(‘Invalid %s’, __(‘report’)));
}
if ($this->request->is(‘post’) || $this->request->is(‘put’)) {
if ($this->Report->save($this->request->data)) {
$contact = $this->Report->findById($id); //保存後IDを取得
$this->ContactMail->send(
‘お問い合わせを送信しました’, //件名
‘contact’, //テンプレートファイル名
$contact, //渡すデータ
$contact[‘Child’][‘email’], //メールアドレス
$contact[‘Child’][‘childname’] . ‘様’ //名前
);
$this->Session->setFlash(
__(‘お問い合わせを送信しました’),
‘alert’,
array(
‘plugin’ => ‘TwitterBootstrap’,
‘class’ => ‘alert-success’
)
);
$this->redirect(array(‘action’ => ‘index’));
} else {
$this->Session->setFlash(
__(‘送信内容にエラーがあります。エラーメッセージに従って内容を修正してください。’),
‘alert’,
array(
‘plugin’ => ‘TwitterBootstrap’,
‘class’ => ‘alert-error’
)
);
}
} else {
$this->request->data = $this->Report->read(null, $id);
}
$users = $this->Report->User->find(‘list’);
$children = $this->Report->Child->find(‘list’);
$this->set(compact(‘users’, ‘children’));
}
[/php]
/Component/ContactMailComponent.php
[php]
<?php
App::uses(‘CakeEmail’, ‘Network/Email’);
class ContactMailComponent extends Component {
// SMTP設定
public $from_mail = ‘xxxxxxx@xxxxxx.jp’;//送信元メールアドレス
public $from_name = null; //送信者名 nullでも可
public $config = array (
‘host’ => ‘smtp.xxxx.jp’, //送信サーバーを書くよ
‘port’ => 587, //
‘username’ => ‘xxxxxxxx@xxxxx.jp’, //メールアドレス書くよ
‘password’ => ‘xxxxxx’, //パスワード書くよ
‘transport’ => ‘Smtp’
);
function send($subject, $template, $data, $to_mail, $to_name = null) {
// 送受信者設定
if( is_null($to_name) ) {
$to_name = $to_mail;
}
if( is_null($this->from_name) ) {
$this->from_name = $this->from_mail;
}
// 送信処理
$email = new CakeEmail($this->config);
$email
->template($template, ‘layout’)
->viewVars(array(‘data’ => $data))
->emailFormat(‘text’)
->to(array($to_mail => $to_name))
->from(array($this->from_mail => $this->from_name))
->bcc(array($this->from_mail => $this->from_name)) //->bcc(‘xxxx@xxxxx.jp’) BCCで他のメールアドレスにも送る
->subject($subject)
->send();
}
}
?>
[/php]