使用手順
phpmailer をダウンロードして、phpMailer という名前で vendors 以下にコピーします。
Codeworx Technologies
http://phpmailer.codeworxtech.com/
僕が PHP をあまり理解していないからかもしれませんが、class.smtp.php 内で未定義の変数を使っているためエラーとなるので修正します。
class.smtp.php 165行目から
(省略)
public function StartTLS() {
$this->error = null; # to avoid confusion
if(!$this->connected()) {
$this->error = array(“error” => “Called StartTLS() without being connected”);
return false;
}
+ $extra = “”;
fputs($this->smtp_conn,”STARTTLS” . $extra . $this->CRLF);
(省略)
次に、app/controllers/components/email.php を作ります。*1
<?php
class EmailComponent extends Object {
var $from = FROM_EMAIL;
var $fromName = FROM_NAME;
var $smtpAuth = true;
var $smtpUserName = SMTP_USERNAME;
// Gmail mail address
var $smtpPassword = SMTP_PASSWORD;
// Gmail password
var $smtpHostNames= SMTP_HOST;
// smtp.gmail.com
var $smtpPort = SMTP_PORT;
// 587
var $smtpSecureType = SMTP_SECURE_TYPE;
// tls
var $smtpError = null;
var $to = null;
var $toName = null;
var $subject = null;
var $cc = null;
var $bcc = null;
var $layout = null;
var $elementDir = ‘email’;
var $template = ‘default’;
var $attachments = null;
var $controller = null;
function startup(&$controller) {
$this->controller = &$controller;
}
function _element($template) {
$temp_layout = $this->controller->layout;
$this->controller->layout = ”;
ob_start();
$this->controller->autoRender = false;
App::import(‘Core’, array(‘View’));
$viewClass = new View($this->controller);
$content = $viewClass->element($template , $this->controller->viewVars, true);
echo $viewClass->renderLayout($content, $this->layout) ;
$this->controller->autoRender = ‘auto’;
$body = ob_get_clean();
$this->controller->layout = $temp_layout;
return $body;
}
function bodyText() {
if ($this->layout == null) {
$this->layout = ‘email/text/default’;
}
return $this->_element($this->elementDir . ‘/text/’ . $this->template);
}
function bodyHtml() {
if ($this->layout == null) {
$this->layout = ‘email/html/default’;
}
return $this->_element($this->elementDir . ‘/html/’ . $this->template);
}
function attach($filename, $asfile = ”) {
if (empty($this->attachments)) {
$this->attachments = array();
$this->attachments[0]['filename'] = $filename;
$this->attachments[0]['asfile'] = $asfile;
} else {
$count = count($this->attachments);
$this->attachments[$count+1]['filename'] = $filename;
$this->attachments[$count+1]['asfile'] = $asfile;
}
}
function base64($str, $charset=“JIS”, $transfer_encoding=“B”, $linefeed=“\n“) {
return mb_encode_mimeheader($str, $charset, $transfer_encoding, $linefeed);
}
function send($html = false) {
App::import(‘Vendor’, ‘PHPMailer’, array(‘file’ => ‘phpMailer’ . DS . ‘class.phpmailer.php’));
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
if ($this->smtpAuth) {
$mail->SMTPAuth = true;
$mail->Host = $this->smtpHostNames;
$mail->Username = $this->smtpUserName;
$mail->Password = $this->smtpPassword;
}
$mail->From = $this->from;
$mail->FromName = $this->base64(mb_convert_encoding($this->fromName, “JIS”, “UTF-8″));
$mail->AddReplyTo($this->from);
// to
if (is_array($this->to)) {
foreach ($this->to as $to) {
$mail->AddAddress($to);
}
} else {
$mail->AddAddress($this->to, $this->base64(mb_convert_encoding($this->toName, “JIS”, “UTF-8″)));
}
// cc
if(!empty($this->cc)) {
if (is_array($this->cc)) {
foreach ($this->cc as $cc) {
$mail->AddCC($cc);
}
} else {
$mail->AddCC($this->cc);
}
}
// bcc
if(!empty($this->bcc)) {
if (is_array($this->bcc)) {
foreach ($this->bcc as $bcc) {
$mail->AddBcc($bcc);
}
} else {
$mail->AddBcc($this->bcc);
}
}
$mail->Port = $this->smtpPort;
$mail->SMTPSecure = $this->smtpSecureType;
$mail->CharSet = “iso-2022-jp”;
$mail->Encoding = “7bit”;
$mail->WordWrap = 70; // set word wrap to 70 characters
if (!empty($this->attachments)) {
foreach ($this->attachments as $attachment) {
if (empty($attachment['asfile'])) {
$mail->AddAttachment($attachment['filename']);
} else {
$mail->AddAttachment($attachment['filename'], $attachment['asfile']);
}
}
}
$mail->IsHTML($html); // set email format to HTML
$mail->Subject = mb_convert_encoding($this->subject, “JIS”, “UTF-8″);
if ($html) {
$mail->Body = mb_convert_encoding($this->bodyHtml(), “JIS”, “UTF-8″);
$mail->AltBody = mb_convert_encoding($this->bodyText(), “JIS”, “UTF-8″);
} else {
$mail->Body = mb_convert_encoding($this->bodyText(), “JIS”, “UTF-8″);
}
$result = $mail->Send();
if($result == false) {
$this->smtpError = $mail->ErrorInfo;
}
return $result;
}
}
?>
コントローラはまず、EmailComponent を読み込みます。
class UsersController extends AppController {
(省略)
+ var $components = array(‘Email’);
使い方
CakePHP の EmailComponent に似せています。
メールを送るには以下のようにします。
$this->set(‘user’, $user);
$this->Email->template = ’signup’; // views/elements/email/text/signup
$this->Email->to = $user['User']['pc_email'];
$this->Email->subject = ‘登録が完了しました’;
$this->Email->send();
if (!empty($this->Email->smtpError)) {
$this->log($this->Email->smtpError, LOG_DEBUG);
}
このとき、実際に使われるテンプレートは以下のようになります。
| layout |
views/layout/email/text/default |
| view |
views/elements/email/text/signup |
ビューは例えば以下のようにします。
<?php echo $user['User']['name'] ?>さん ユーザー登録が完了しました。
複数アドレスに送信
複数アドレスに送信したい場合、$this->Email->to, $this->Email->cc, $this->Email->bcc に配列を指定します。
$this->Email->to = array($user['User']['pc_email'], $user['User']['mobile_email']);
layout 変更
// layouts/email $this->Email->layout = ‘email’;
$result = $this->Email->send(true);
html メールの場合、実際に使われるテンプレートは以下のようになります。
| layout |
views/layout/email/html/default |
| element |
views/elements/email/html/signup |