Last active
December 18, 2015 01:19
-
-
Save ishtaka/5703313 to your computer and use it in GitHub Desktop.
[PHP]PEAR::Mail送信クラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'Mail.php'; | |
require_once 'Mail/mime.php'; | |
class SendMail | |
{ | |
private $mimeObj; | |
private $checkAddress = array( | |
'To' => array(), | |
'Cc' => array(), | |
'Bcc' => array(), | |
'From' => array(), | |
'Reply-To' => array(), | |
); | |
private $addArray = array(); | |
private $to; | |
private $header = array(); | |
private $subject = ''; | |
private $body = ''; | |
private $bodyDir = ''; | |
private $bodyFilePath = ''; | |
private $attacheDir = ''; | |
private $fileEncode = 'UTF-8'; | |
private $mailEncode = 'ISO-2022-JP'; | |
/** | |
* コンストラクタ | |
* @access public | |
* | |
*/ | |
public function __construct() | |
{ | |
$this->mimeObj = new Mail_Mime("\n"); | |
$this->mimeObj->setParam('text_encoding', '7bit'); | |
$this->bodyDir = realpath(dirname(__FILE__)); | |
$this->attacheDir = realpath(dirname(__FILE__)); | |
} | |
/** | |
* エンコードを設定する | |
* @access public | |
* @param string $mailEncode | |
* @param string $fileEncode | |
*/ | |
public function setEncode($mailEncode, $fileEncode) | |
{ | |
$this->mailEncode = $mailEncode; | |
$this->fileEncode = $fileEncode; | |
} | |
/** | |
* メール本文をファイルから取得し設定する | |
* @access private | |
* @throws RuntimeException ErrorMessage | |
*/ | |
private function getBody() | |
{ | |
if (!empty($this->body)) return; | |
if (!file_exists($this->bodyFilePath)) { | |
throw new RuntimeException('MailBodyFile does not exists'); | |
} | |
if(!($body = file_get_contents($this->bodyFilePath))) { | |
throw new RuntimeException('Failed to get MailBodyFile'); | |
} | |
$body = mb_convert_encoding( | |
$body, | |
$this->mailEncode, | |
$this->fileEncode | |
); | |
$this->mimeObj->setTxtBody($body); | |
} | |
/** | |
* メール本文をプレーンテキストで設定する | |
* @access private | |
*/ | |
public function setBody($body) | |
{ | |
$this->body = mb_convert_encoding( | |
$body, | |
$this->mailEncode, | |
$this->fileEncode | |
); | |
$this->mimeObj->setTxtBody($body); | |
} | |
/** | |
* 本文のファイルパスを設定する | |
* @access public | |
* @param string $filename | |
*/ | |
public function setBodyFile($filename) | |
{ | |
$this->bodyFilePath = $this->bodyDir . '/' . basename($filename); | |
} | |
/** | |
* 件名をセットする | |
* @access public | |
* @param string $subject | |
*/ | |
public function setSubject($subject) | |
{ | |
$subject = mb_convert_encoding( | |
$subject, | |
$this->mailEncode, | |
$this->fileEncode | |
); | |
$this->subject = mb_encode_mimeheader( | |
$subject, | |
$this->mailEncode, | |
'B' | |
); | |
} | |
/** | |
* メッセージを構築する | |
* @access private | |
*/ | |
private function build() | |
{ | |
$param = array( | |
'head_charset' => $this->mailEncode, | |
'text_charset' => $this->mailEncode, | |
); | |
$this->body = $this->mimeObj->get($param); | |
} | |
/** | |
* アドレスをセットする | |
* @access public | |
* @param array $addArray | |
*/ | |
public function setAddArray(array $addArray) | |
{ | |
mb_convert_variables($this->mailEncode, $this->fileEncode, $addArray); | |
$this->addArray = $addArray; | |
} | |
/** | |
* ヘッダーを作成する | |
* @access private | |
* @throws LogicException ErrorMessage | |
*/ | |
private function makeHeader() | |
{ | |
if (empty($this->addArray)) throw new LogicException('address is not set'); | |
if (!5 == count($addArray = array_merge($this->checkAddress, $this->addArray))) { | |
throw new LogicException('Wrong address format'); | |
} | |
$headers = array_map( | |
function($arr) { return implode(',', $arr); }, | |
$addArray | |
); | |
$headers['Subject'] = $this->subject; | |
$this->to = $headers['To']; | |
$this->headers = $this->mimeObj->headers($headers); | |
} | |
/** | |
* メールを送信する | |
* @access public | |
*/ | |
public function sendMail() | |
{ | |
try { | |
$this->getBody(); | |
$this->build(); | |
$this->makeHeader(); | |
} catch (RuntimeException $e) { | |
echo $e->getMessage(), PHP_EOL; | |
return false; | |
} catch (LogicException $e) { | |
echo $e->getMessage(), PHP_EOL; | |
return false; | |
} | |
$mail = Mail::factory('mail'); | |
$mail->send($this->to, $this->headers, $this->body); | |
} | |
/** | |
* ファイルを添付する | |
* @access public | |
* @param string $attacheFileName 添付ファイル名 | |
* @param string $dir 添付用ディレクトリ配下のパス> | |
*/ | |
public function attache($attacheFileName, $dir = '') | |
{ | |
$attachName = mb_convert_encoding( | |
basename($attacheFileName), | |
$this->mailEncode, | |
$this->fileEncode | |
); | |
$attacheFilePath = $this->getAttacheFilePath($attacheFileName, $dir); | |
if (file_exists($attacheFilePath)) { | |
$attache = file_get_contents($attacheFilePath); | |
$this->mimeObj->addAttachment( | |
$attache, | |
'application/octet-stream', | |
$attachName, | |
false, | |
'base64', | |
'attachment', | |
'', | |
'', | |
'', | |
'base64', | |
'base64', | |
'', | |
$this->mailEncode); | |
} | |
} | |
/** | |
* 添付ファイルパスを取得する | |
* @access private | |
* @param string $filename 添付ファイル名 | |
* @param string $dir 添付用ディレクトリ配下のパス | |
*/ | |
private function getAttacheFilePath($filename, $dir) | |
{ | |
if(!empty($dir)) { | |
$dir = '/' .str_replace('../', '' ,trim($dir, "/")); | |
} | |
$attacheFilePath = $this->attacheDir | |
. $dir | |
. '/' . basename($filename); | |
return $attacheFilePath; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment