<?php
class Sendmail
{
/*
* 메일발송을 위한 클래스
* 외부 SMTP 서버를 지원합니다.
* Author: Gwangsoo, Ryu (piver@ineemail.com)
*/
protected $UseSMTPServer = false; // 다른 SMTP 서버를 이용할 경우
protected $SMTPServer; // SMTP 서버 도메인
protected $SMTPPort = 25; // Port
protected $SMTPAuthUser; // SMTP 인증 사용자
protected $SMTPAuthPasswd; // SMTP 인증 비밀번호
protected $Socket;
protected $MailHeaderArray = array(); // 메일헤더를 담을 배열
protected $MailFrom; // 보내는 사람
protected $ReplyTo; // 회신받을 주소 (기본적으로 보내는 메일주소가 된다)
protected $MailTo = array(); // 받는 사람을 담을 배열
protected $Subject; // 메일제목
protected $MailBody; // 메일본문
protected $Charset = 'EUC-KR'; // 메일기본 캐릭터셋
protected $Attach = array(); // 인코딩된 첨부파일
protected $Boundary; // Bound
public function __construct($charset = 'EUC-KR')
{
$this->Boundary = md5(uniqid(microtime())); // 바운드를 초기화한다
if(!empty($charset)) $this->Charset = $charset; // 캐릭터셋
}
public function setFrom($email, $name = null)
{
// 보내는 메일
$this->setReplyTo($email);
return $this->MailFrom = ($name) ? $name . ' <' . $email . '>' : $email;
}
public function setReplyTo($email)
{
// 회신주소 - 기본적으로 보내는 메일을 회신주소로 셋한다
return $this->ReplyTo = $email;
}
public function setSubject($Subject)
{
// 제목
return $this->Subject = $Subject;
}
public function addTo($email, $name = null)
{
// 받는 메일을 추가한다
return $this->MailTo[$email] = $name;
}
public function addAttach($Filename, $Source)
{
// 첨부파일을 추가한다
$fp = fopen($Source, 'r'); // 소스파일을 연다
if($fp) {
$fBody = fread($fp, filesize($Source)); // 파일의 내용을 읽어온다
@fclose($fp);
$this->Attach[$Filename] = $fBody; // Attach 배열에 담는다
}
}
public function setMailBody($Body, $useHtml = true)
{
if(!$useHtml) { // 메일본문이 HTML 형식이 아니면 HTML 형식으로 바꾸어준다
$Body = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=' . $this->Charset . '">
<style type="text/css">
BODY, TH, TD, DIV, SPAN, P, INPUT {
font-size:12px;
line-height:17px;
}
BODY, DIV { text-align:justify; }
</style>
</head>
<body>
' . nl2br($Body) . '
</body>
</html>
';
}
$this->MailBody = $Body; // 메일본문을 셋한다
}
protected function AddBasicHeader()
{
// 메일의 기본 헤더를 작성한다
$this->addHeader('From', $this->MailFrom);
$this->addHeader('User-Agent', 'Dabuilder Mail System');
$this->addHeader('X-Accept-Language', 'ko, en');
$this->addHeader('X-Sender', $this->ReplyTo);
$this->addHeader('X-Mailer', 'PHP');
$this->addHeader('X-Priority', 1);
$this->addHeader('Reply-to', $this->ReplyTo);
$this->addHeader('Return-Path', $this->ReplyTo);
if(count($this->Attach) > 0) { // 첨부파일이 있을 경우의 헤더
$this->addHeader('MIME-Version', '1.0');
$this->addHeader('Content-Type', 'Multipart/mixed; boundary = "' . $this->Boundary . '"');
} else { // 첨부파일이 없는 일반 메일일 경우의 헤더
$this->addHeader('Content-Type', 'text/html; charset=' . $this->Charset);
$this->addHeader('Content-Transfer-Encoding', '8bit');
}
}
protected function addHeader($Content, $Value)
{
// 메일헤더의 내용을 추가한다
$this->MailHeaderArray[$Content] = $Value;
}
protected function MailAttach()
{
// 첨부파일이 있을 경우 메일본문에 첨부파일을 덧붙인다
$arrRet = array();
if(count($this->Attach) > 0) {
foreach($this->Attach as $Filename => $fBody) {
$tmpAttach = "--" . $this->Boundary . "\r\n";
$tmpAttach .= "Content-Type: application/octet-stream\r\n";
$tmpAttach .= "Content-Transfer-Encoding: base64\r\n";
$tmpAttach .= "Content-Disposition: attachment; filename=\"" . $Filename . "\"\r\n\r\n";
$tmpAttach .= $this->encodingContents($fBody) . "\r\n\r\n";
$arrRet[] = $tmpAttach;
}
}
return implode('', $arrRet);
}
public function setUseSMTPServer($boolean = null)
{
// 외부 SMTP 서버를 이용할 것인지를 셋한다
return (is_null($boolean)) ? $this->UseSMTPServer : $this->UseSMTPServer = $boolean;
}
public function setSMTPServer($smtpServer = null, $port = 25)
{
// 외부 SMTP 서버를 이용할 경우 SMTP 서버를 설정한다
$this->SMTPPort = $port;
return (is_null($smtpServer)) ? $this->SMTPServer : $this->SMTPServer = $smtpServer;
}
public function setSMTPUser($User = null)
{
// 외부 SMTP 서버를 이용할 경우 로그인 사용자를 설정한다
return (is_null($User)) ? $this->SMTPAuthUser : $this->SMTPAuthUser = $User;
}
public function setSMTPPasswd($Passwd = null)
{
// 외부 SMTP 서버를 이용할 경우 로그인 비밀번호를 설정한다
return (is_null($Passwd)) ? $this->SMTPAuthPasswd : $this->SMTPAuthPasswd = $Passwd;
}
protected function encodingContents($contets)
{
// 메일본문을 인코딩하는 역할을 한다
return chunk_split(base64_encode($contets));
}
protected function makeMailHeader()
{
// 보낼 메일의 헤더를 작성한다
$header = "";
foreach($this->MailHeaderArray as $Key => $Val)
$header .= $Key . ": " . $Val . "\r\n";
return $header . "\r\n";
}
public function send()
{
// 메일을 전송한다
$this->AddBasicHeader(); // 메일의 기본헤더를 생성한다
if($this->UseSMTPServer) return $this->_SMTPSend(); // 외부 SMTP 서버를 이용할 경우
else return $this->_localSend(); // 로컬 SMTP 를 이용할 경우
}
protected function _SMTPSend()
{
/*
* 외부 SMTP 서버를 이용할 경우 소켓접속을 통해서 메일을 전송한다
*/
$Succ = 0;
if($this->SMTPServer) {
$this->addHeader('Subject', $this->Subject); // 메일헤더에 제목을 추가한다
$MailBody = $this->makeMailBody(); // 메일본문을 생성한다
if(count($this->MailTo) > 0) { // 받는 메일이 있으면 다음 작업을 반복한다
foreach($this->MailTo as $Email => $Name) {
$mailTo = ($Name) ? $Name . ' <' . $Email . '>' : $Email; // 받는사람
$this->addHeader('To', $mailTo); // 메일헤더에 받는사람을 추가한다
$Contents = $this->makeMailHeader() . "\r\n" . $MailBody; // 메일헤더와 본문을 이용해 전송할 메일을 생성한다
$this->Socket = fsockopen($this->SMTPServer, $this->SMTPPort); // 소켓접속한다
if($this->Socket) {
$this->_sockPut('HELO ' . $this->SMTPServer);
if($this->SMTPAuthUser) { // SMTP 인증
$this->_sockPut('AUTH LOGIN');
$this->_sockPut(base64_encode($this->SMTPAuthUser));
$this->_sockPut(base64_encode($this->SMTPAuthPasswd));
}
$this->_sockPut('MAIL From:' . $this->ReplyTo); // 보내는 메일
$this->_sockPut('RCPT To:' . $Email); // 받는메일
$this->_sockPut('DATA');
$this->_sockPut($Contents); // 메일내용
$Result = $this->_sockPut('.'); // 전송완료
if(strpos($Result, 'Message accepted for delivery') !== false) $Succ++; // 성공여부판단
$this->_sockPut('QUIT'); // 접속종료
}
}
}
} else $Succ = $this->_localSend(); // 외부 SMTP 서버를 이용하지 않으면 로컬 SMTP를 이용해서 전송한다
return $Succ;
}
protected function _sockPut($str)
{
// 소켓접속시 내용전송 및 결과값 받기
@fputs($this->Socket, $str . "\r\n");
return @fgets($this->Socket, 512);
}
protected function _localSend()
{
$Contents = $this->makeMailBody(); // 메일본문을 작성한다
$Succ = 0;
foreach($this->MailTo as $Email => $Name) {
$toMail = ($Name) ? $Name . ' <' . $Email . '>' : $Email; // 받는메일
$this->addHeader('To', $toMail); // 메일헤더에 받는메일을 추가한다
$header = $this->makeMailHeader(); // 헤더를 작성한다
if(mail($Email, $this->Subject, $Contents, $header)) $Succ++; // 성공여부 판단
}
return $Succ;
}
protected function makeMailBody()
{
// 메일의 본문을 작성한다
$mailbody = "";
if(count($this->Attach) > 0) { // 첨부파일이 있을 경우 본문을 인코딩하여 만든다
$mailbody .= "--" . $this->Boundary . "\r\n";
$mailbody .= "Content-Type: text/html; charset=" . $this->Charset . "\r\n";
$mailbody .= "Content-Transfer-Encoding: base64\r\n\r\n";
$mailbody .= $this->encodingContents($this->MailBody) . "\r\n\r\n";
$mailbody .= "\r\n" . $this->MailAttach();
} else $mailbody = $this->MailBody; // 첨부파일이 없으면 그냥 HTML 형식으로 메일본문을 생성한다
return $mailbody;
}
}
?>
class Sendmail
{
/*
* 메일발송을 위한 클래스
* 외부 SMTP 서버를 지원합니다.
* Author: Gwangsoo, Ryu (piver@ineemail.com)
*/
protected $UseSMTPServer = false; // 다른 SMTP 서버를 이용할 경우
protected $SMTPServer; // SMTP 서버 도메인
protected $SMTPPort = 25; // Port
protected $SMTPAuthUser; // SMTP 인증 사용자
protected $SMTPAuthPasswd; // SMTP 인증 비밀번호
protected $Socket;
protected $MailHeaderArray = array(); // 메일헤더를 담을 배열
protected $MailFrom; // 보내는 사람
protected $ReplyTo; // 회신받을 주소 (기본적으로 보내는 메일주소가 된다)
protected $MailTo = array(); // 받는 사람을 담을 배열
protected $Subject; // 메일제목
protected $MailBody; // 메일본문
protected $Charset = 'EUC-KR'; // 메일기본 캐릭터셋
protected $Attach = array(); // 인코딩된 첨부파일
protected $Boundary; // Bound
public function __construct($charset = 'EUC-KR')
{
$this->Boundary = md5(uniqid(microtime())); // 바운드를 초기화한다
if(!empty($charset)) $this->Charset = $charset; // 캐릭터셋
}
public function setFrom($email, $name = null)
{
// 보내는 메일
$this->setReplyTo($email);
return $this->MailFrom = ($name) ? $name . ' <' . $email . '>' : $email;
}
public function setReplyTo($email)
{
// 회신주소 - 기본적으로 보내는 메일을 회신주소로 셋한다
return $this->ReplyTo = $email;
}
public function setSubject($Subject)
{
// 제목
return $this->Subject = $Subject;
}
public function addTo($email, $name = null)
{
// 받는 메일을 추가한다
return $this->MailTo[$email] = $name;
}
public function addAttach($Filename, $Source)
{
// 첨부파일을 추가한다
$fp = fopen($Source, 'r'); // 소스파일을 연다
if($fp) {
$fBody = fread($fp, filesize($Source)); // 파일의 내용을 읽어온다
@fclose($fp);
$this->Attach[$Filename] = $fBody; // Attach 배열에 담는다
}
}
public function setMailBody($Body, $useHtml = true)
{
if(!$useHtml) { // 메일본문이 HTML 형식이 아니면 HTML 형식으로 바꾸어준다
$Body = '
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=' . $this->Charset . '">
<style type="text/css">
BODY, TH, TD, DIV, SPAN, P, INPUT {
font-size:12px;
line-height:17px;
}
BODY, DIV { text-align:justify; }
</style>
</head>
<body>
' . nl2br($Body) . '
</body>
</html>
';
}
$this->MailBody = $Body; // 메일본문을 셋한다
}
protected function AddBasicHeader()
{
// 메일의 기본 헤더를 작성한다
$this->addHeader('From', $this->MailFrom);
$this->addHeader('User-Agent', 'Dabuilder Mail System');
$this->addHeader('X-Accept-Language', 'ko, en');
$this->addHeader('X-Sender', $this->ReplyTo);
$this->addHeader('X-Mailer', 'PHP');
$this->addHeader('X-Priority', 1);
$this->addHeader('Reply-to', $this->ReplyTo);
$this->addHeader('Return-Path', $this->ReplyTo);
if(count($this->Attach) > 0) { // 첨부파일이 있을 경우의 헤더
$this->addHeader('MIME-Version', '1.0');
$this->addHeader('Content-Type', 'Multipart/mixed; boundary = "' . $this->Boundary . '"');
} else { // 첨부파일이 없는 일반 메일일 경우의 헤더
$this->addHeader('Content-Type', 'text/html; charset=' . $this->Charset);
$this->addHeader('Content-Transfer-Encoding', '8bit');
}
}
protected function addHeader($Content, $Value)
{
// 메일헤더의 내용을 추가한다
$this->MailHeaderArray[$Content] = $Value;
}
protected function MailAttach()
{
// 첨부파일이 있을 경우 메일본문에 첨부파일을 덧붙인다
$arrRet = array();
if(count($this->Attach) > 0) {
foreach($this->Attach as $Filename => $fBody) {
$tmpAttach = "--" . $this->Boundary . "\r\n";
$tmpAttach .= "Content-Type: application/octet-stream\r\n";
$tmpAttach .= "Content-Transfer-Encoding: base64\r\n";
$tmpAttach .= "Content-Disposition: attachment; filename=\"" . $Filename . "\"\r\n\r\n";
$tmpAttach .= $this->encodingContents($fBody) . "\r\n\r\n";
$arrRet[] = $tmpAttach;
}
}
return implode('', $arrRet);
}
public function setUseSMTPServer($boolean = null)
{
// 외부 SMTP 서버를 이용할 것인지를 셋한다
return (is_null($boolean)) ? $this->UseSMTPServer : $this->UseSMTPServer = $boolean;
}
public function setSMTPServer($smtpServer = null, $port = 25)
{
// 외부 SMTP 서버를 이용할 경우 SMTP 서버를 설정한다
$this->SMTPPort = $port;
return (is_null($smtpServer)) ? $this->SMTPServer : $this->SMTPServer = $smtpServer;
}
public function setSMTPUser($User = null)
{
// 외부 SMTP 서버를 이용할 경우 로그인 사용자를 설정한다
return (is_null($User)) ? $this->SMTPAuthUser : $this->SMTPAuthUser = $User;
}
public function setSMTPPasswd($Passwd = null)
{
// 외부 SMTP 서버를 이용할 경우 로그인 비밀번호를 설정한다
return (is_null($Passwd)) ? $this->SMTPAuthPasswd : $this->SMTPAuthPasswd = $Passwd;
}
protected function encodingContents($contets)
{
// 메일본문을 인코딩하는 역할을 한다
return chunk_split(base64_encode($contets));
}
protected function makeMailHeader()
{
// 보낼 메일의 헤더를 작성한다
$header = "";
foreach($this->MailHeaderArray as $Key => $Val)
$header .= $Key . ": " . $Val . "\r\n";
return $header . "\r\n";
}
public function send()
{
// 메일을 전송한다
$this->AddBasicHeader(); // 메일의 기본헤더를 생성한다
if($this->UseSMTPServer) return $this->_SMTPSend(); // 외부 SMTP 서버를 이용할 경우
else return $this->_localSend(); // 로컬 SMTP 를 이용할 경우
}
protected function _SMTPSend()
{
/*
* 외부 SMTP 서버를 이용할 경우 소켓접속을 통해서 메일을 전송한다
*/
$Succ = 0;
if($this->SMTPServer) {
$this->addHeader('Subject', $this->Subject); // 메일헤더에 제목을 추가한다
$MailBody = $this->makeMailBody(); // 메일본문을 생성한다
if(count($this->MailTo) > 0) { // 받는 메일이 있으면 다음 작업을 반복한다
foreach($this->MailTo as $Email => $Name) {
$mailTo = ($Name) ? $Name . ' <' . $Email . '>' : $Email; // 받는사람
$this->addHeader('To', $mailTo); // 메일헤더에 받는사람을 추가한다
$Contents = $this->makeMailHeader() . "\r\n" . $MailBody; // 메일헤더와 본문을 이용해 전송할 메일을 생성한다
$this->Socket = fsockopen($this->SMTPServer, $this->SMTPPort); // 소켓접속한다
if($this->Socket) {
$this->_sockPut('HELO ' . $this->SMTPServer);
if($this->SMTPAuthUser) { // SMTP 인증
$this->_sockPut('AUTH LOGIN');
$this->_sockPut(base64_encode($this->SMTPAuthUser));
$this->_sockPut(base64_encode($this->SMTPAuthPasswd));
}
$this->_sockPut('MAIL From:' . $this->ReplyTo); // 보내는 메일
$this->_sockPut('RCPT To:' . $Email); // 받는메일
$this->_sockPut('DATA');
$this->_sockPut($Contents); // 메일내용
$Result = $this->_sockPut('.'); // 전송완료
if(strpos($Result, 'Message accepted for delivery') !== false) $Succ++; // 성공여부판단
$this->_sockPut('QUIT'); // 접속종료
}
}
}
} else $Succ = $this->_localSend(); // 외부 SMTP 서버를 이용하지 않으면 로컬 SMTP를 이용해서 전송한다
return $Succ;
}
protected function _sockPut($str)
{
// 소켓접속시 내용전송 및 결과값 받기
@fputs($this->Socket, $str . "\r\n");
return @fgets($this->Socket, 512);
}
protected function _localSend()
{
$Contents = $this->makeMailBody(); // 메일본문을 작성한다
$Succ = 0;
foreach($this->MailTo as $Email => $Name) {
$toMail = ($Name) ? $Name . ' <' . $Email . '>' : $Email; // 받는메일
$this->addHeader('To', $toMail); // 메일헤더에 받는메일을 추가한다
$header = $this->makeMailHeader(); // 헤더를 작성한다
if(mail($Email, $this->Subject, $Contents, $header)) $Succ++; // 성공여부 판단
}
return $Succ;
}
protected function makeMailBody()
{
// 메일의 본문을 작성한다
$mailbody = "";
if(count($this->Attach) > 0) { // 첨부파일이 있을 경우 본문을 인코딩하여 만든다
$mailbody .= "--" . $this->Boundary . "\r\n";
$mailbody .= "Content-Type: text/html; charset=" . $this->Charset . "\r\n";
$mailbody .= "Content-Transfer-Encoding: base64\r\n\r\n";
$mailbody .= $this->encodingContents($this->MailBody) . "\r\n\r\n";
$mailbody .= "\r\n" . $this->MailAttach();
} else $mailbody = $this->MailBody; // 첨부파일이 없으면 그냥 HTML 형식으로 메일본문을 생성한다
return $mailbody;
}
}
?>
[이 게시물은 관리자님에 의해 2011-10-31 17:12:10 PHP & HTML에서 이동 됨]
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8230 | 9년 전 | 53 | ||
| 8229 | 9년 전 | 52 | ||
| 8228 |
커네드커네드
|
9년 전 | 99 | |
| 8227 | 9년 전 | 108 | ||
| 8226 | 9년 전 | 145 | ||
| 8225 | 9년 전 | 134 | ||
| 8224 | 9년 전 | 132 | ||
| 8223 | 9년 전 | 92 | ||
| 8222 |
|
9년 전 | 162 | |
| 8221 | 9년 전 | 77 | ||
| 8220 | 9년 전 | 76 | ||
| 8219 | 9년 전 | 83 | ||
| 8218 | 9년 전 | 112 | ||
| 8217 |
star3840
|
9년 전 | 99 | |
| 8216 | 9년 전 | 137 | ||
| 8215 | 9년 전 | 89 | ||
| 8214 | 9년 전 | 208 | ||
| 8213 | 9년 전 | 142 | ||
| 8212 | 9년 전 | 57 | ||
| 8211 | 9년 전 | 223 | ||
| 8210 | 9년 전 | 220 | ||
| 8209 | 9년 전 | 320 | ||
| 8208 | 9년 전 | 189 | ||
| 8207 | 9년 전 | 202 | ||
| 8206 |
|
9년 전 | 166 | |
| 8205 | 9년 전 | 147 | ||
| 8204 | 9년 전 | 110 | ||
| 8203 | 9년 전 | 203 | ||
| 8202 | 9년 전 | 128 | ||
| 8201 | 9년 전 | 166 | ||
| 8200 | 9년 전 | 137 | ||
| 8199 | 9년 전 | 188 | ||
| 8198 | 9년 전 | 156 | ||
| 8197 | 9년 전 | 137 | ||
| 8196 | 9년 전 | 521 | ||
| 8195 | 9년 전 | 136 | ||
| 8194 | 9년 전 | 268 | ||
| 8193 | 9년 전 | 136 | ||
| 8192 | 9년 전 | 164 | ||
| 8191 | 9년 전 | 116 | ||
| 8190 | 9년 전 | 113 | ||
| 8189 | 9년 전 | 167 | ||
| 8188 | 9년 전 | 115 | ||
| 8187 | 9년 전 | 121 | ||
| 8186 | 9년 전 | 131 | ||
| 8185 | 9년 전 | 293 | ||
| 8184 | 9년 전 | 87 | ||
| 8183 | 9년 전 | 309 | ||
| 8182 | 9년 전 | 142 | ||
| 8181 | 9년 전 | 114 | ||
| 8180 | 9년 전 | 678 | ||
| 8179 | 9년 전 | 474 | ||
| 8178 | 9년 전 | 278 | ||
| 8177 |
kiplayer
|
9년 전 | 287 | |
| 8176 | 9년 전 | 331 | ||
| 8175 | 9년 전 | 204 | ||
| 8174 | 9년 전 | 211 | ||
| 8173 | 9년 전 | 324 | ||
| 8172 | 9년 전 | 170 | ||
| 8171 | 9년 전 | 162 | ||
| 8170 | 9년 전 | 280 | ||
| 8169 |
커네드커네드
|
9년 전 | 246 | |
| 8168 | 9년 전 | 299 | ||
| 8167 | 9년 전 | 305 | ||
| 8166 | 9년 전 | 218 | ||
| 8165 | 9년 전 | 148 | ||
| 8164 | 9년 전 | 280 | ||
| 8163 | 9년 전 | 266 | ||
| 8162 | 9년 전 | 274 | ||
| 8161 | 9년 전 | 272 | ||
| 8160 |
|
9년 전 | 468 | |
| 8159 | 9년 전 | 390 | ||
| 8158 | 9년 전 | 207 | ||
| 8157 | 9년 전 | 345 | ||
| 8156 | 9년 전 | 257 | ||
| 8155 | 9년 전 | 233 | ||
| 8154 |
00년생용띠
|
9년 전 | 578 | |
| 8153 | 9년 전 | 209 | ||
| 8152 |
|
9년 전 | 386 | |
| 8151 | 9년 전 | 387 | ||
| 8150 | 9년 전 | 479 | ||
| 8149 |
Jangfolk
|
9년 전 | 316 | |
| 8148 | 9년 전 | 149 | ||
| 8147 | 9년 전 | 358 | ||
| 8146 | 9년 전 | 412 | ||
| 8145 | 9년 전 | 344 | ||
| 8144 | 9년 전 | 312 | ||
| 8143 | 9년 전 | 165 | ||
| 8142 | 9년 전 | 412 | ||
| 8141 | 9년 전 | 360 | ||
| 8140 | 9년 전 | 908 | ||
| 8139 | 9년 전 | 233 | ||
| 8138 |
전갈자리남자
|
9년 전 | 377 | |
| 8137 | 9년 전 | 359 | ||
| 8136 | 9년 전 | 718 | ||
| 8135 |
|
9년 전 | 770 | |
| 8134 |
PlayPixel
|
9년 전 | 482 | |
| 8133 |
|
9년 전 | 420 | |
| 8132 | 9년 전 | 431 | ||
| 8131 | 9년 전 | 788 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기