メール送信

ここで会員登録時のメール送信を作ります。 Emailコンポーネントの使い方は本家サイトのドキュメントの通りです。

/app 直下の app_controller.php を開いて、

class AppController extends Controller {
}
となっている中に、
    var $components = array('Session', 'Email');
と入れます。

そして、以下のようなファンクションを入れます。

function _sendEmail($template, $subject, $to, $attachments, $from, $replyto, $fromName, $sendAs, $bcc) {
    if (!$from) {$from = 'your@email';}
    if (!$replyto) {$replyto = 'your@email';}
    if ($fromName) {$from = $fromName . " <$from>";}
    $this->Email->to = $to;
    $this->Email->bcc = $bcc;
    $this->Email->subject = $subject;
    $this->Email->replyTo = $replyto;
    $this->Email->from = $from;
    $this->Email->template = $template;
    if ($attachments) {$this->Email->attachments = $attachments;}
    $this->Email->sendAs = $sendAs;
    $this->Email->send();
}

smtp で送信する場合は、$this->Email->send() の部分を以下に書き換えます。

        $this->Email->smtpOptions = array(
            'port'=>'587',
            'timeout'=>'30',
            'host' => 'your.host.name',
            'username'=>'username',
            'password'=>'password'
        );
        $this->Email->delivery = 'smtp';
        $this->Email->send();
        $this->set('smtp-errors', $this->Email->smtpError);

gmailで送る場合は、smtpOptions のところを以下のようにします。

        $this->Email->smtpOptions = array(
            'port'=>'465',
            'timeout'=>'30',
            'host' => 'ssl://smtp.gmail.com',
            'username'=>'your@email',
            'password'=>'yourpassword'
        );

メールの文字コードは、デフォルトだとUTF-8で送られます。 charsetは以下で設定できますが、ISO-2022-JP にすると subject が文字化けするようです。

$this->Email->charset = "ISO-2022-JP";

from のメールアドレスは固定だし、text形式のメールしか送らない、というのであれば、 最初に設定した app_controller.php で、

    var $components = array(
        'Session', 
        'Email' => array(
            'from' => 'your@email',
            'sendAs' => 'text',
        ),
    );
というように書くこともできます。

こうすると、function _sendEmail() の中では指定する必要がありません。

//        $this->Email->sendAs = $sendAs;

つまり、var $components の方にどんどん指定していってしまうと、function _sendEmail() の方に書くことがなくなり、どんどん減っていきます。
自分の使いやすいと思うあたりに調節すればよいと思います。

メール文面のテンプレートは、まずレイアウトを

  • /app/views/layouts/email/html/default.ctp
  • /app/views/layouts/email/text/default.ctp
に入れます。

テキストメールの場合は、とりあえず、default.ctp に

<?php echo $content_for_layout; ?>
とだけ書いて保存します。

文面は

  • /app/elements/email/html/default.ctp
  • /app/elements/email/text/default.ctp
に入れます。

文面は普通に、確認画面の view.ctp と同じように作ればいいと思います。

以下の会員登録がありました。
-----------------------------------------------------
メールアドレス: <?php echo $member['Member']['email'] ."\n"; ?>
会員種別:<?php echo $types[$member['Member']['type_id']] ."\n"; ?>
誕生日:<?php echo $member['Member']['birthday']['year']; ?>年<?php echo $member['Member']['birthday']['month']; ?>月<?php echo $member['Member']['birthday']['day']; ?>日
好きな物:
<?php
if (!empty($member['Favorite'])) {
    foreach ($member['Favorite']['Favorite'] as $favorite) {
        echo "\t".$favorites[$favorite['id']]."\n";
    }
}
?>

こんな感じです。
こちらの elements の中の方のファイル名を上記 $this->Email->template に指定します。

ここではとりあえず、

/app/elements/email/text/member_add.ctp
としました。

で、コントローラ members_cntroller.php のアクション add のsaveしてる部分の下に

            } else {
                $this->Member->create();
                if ($this->Member->save($this->data)) {
                    $this->set('member', $this->data);
                    $attachments = array();
                    $this->_sendEmail('member_add', 'ユーザー登録のお知らせ', 'your@email', $attachments,'','','','text',array());
                    $this->Session->setFlash(__('The member has been saved', true));
                    $this->redirect(array('action' => 'complete'));
                } else {
                    $this->Session->setFlash(__('The member could not be saved. Please, try again.', true));
                }
            }
を入れます。
メールを受信して確認します。