Class
- Tip&Tech
[ÇÔ¼ö] °£´ÜÇÏ°Ô ¸¸µé¾îº» ¸ÞÀÏÀü¼Û Ŭ·¡½º | |||||
±Û¾´ÀÌ | À½¾Ç»ç¶û | ³¯ Â¥ | 08-08-05 20:33 | Á¶ ȸ | 12475 |
---|---|---|---|---|---|
°£ÆíURL |
https://www.phpschool.com/link/tipntech/62435
|
||||
<?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'; // ¸ÞÀϱ⺻ ij¸¯Åͼ protected $Attach = array(); // ÀÎÄÚµùµÈ ÷ºÎÆÄÀÏ protected $Boundary; // Bound public function __construct($charset = 'EUC-KR') { $this->Boundary = md5(uniqid(microtime())); // ¹Ù¿îµå¸¦ ÃʱâÈÇÑ´Ù if(!empty($charset)) $this->Charset = $charset; // ij¸¯Åͼ } 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; } } ?> |
|||||
|
Àüü´ñ±Û¼ö 17
-
À½¾Ç»ç¶û 08-08-05 20:38
¼Ò½ºº¸±â
-
»ç¿ë¿¹ÀÔ´Ï´Ù.
$dMail = new Sendmail;
$dMail->setUseSMTPServer(true);
$dMail->setSMTPServer('mail.test.com');
$dMail->setSMTPUser('tester');
$dMail->setSMTPPasswd('testerpassword');
$dMail->setFrom('xxx@test.com', 'º¸³»´Â»ç¶÷');
$dMail->setSubject('Å×½ºÆ® ¸ÞÀÏÀü¼Û');
$dMail->addAttach($_FILES['upfile']['name'], $_FILES['upfile']['tmp_name']);
$dMail->setMailBody('¸ÞÀÏÀü¼ÛÀ» Å×½ºÆ®ÇÕ´Ï´Ù', false);
$dMail->addTo('aaaa@aaa.com', '¹Þ´Â»ç¶÷');
echo $dMail->send();
-
º£¸£»çü 08-08-06 11:07
¼Ò½ºº¸±â
-
PHP ¹öÀüµµ ¸í½Ã¸¦ ÇØÁáÀ¸¸é ÁÁ°Ú¾î¿ä. 4.0.x µ¿ÀÛ¾ÈÇÏ°í¿ä.. 5.2.x µ¿ÀÛÇÏ°í¿ä..
¾î´À ¹öÀü ºÎÅÍ Áö¿øÇÏÁÒ?
Ŭ·¡½ºÀÇ ÇÁ·ÎÅØƼµå ¼Ó¼ºÀ̸é...? -
Æë±Ï¢âÀ̽½¾ð´Ï 08-08-06 11:53
¼Ò½ºº¸±â
-
5.X ºÎÅÍ Áö¿øµÇ´øµ¥¿ä.
4.X ¾È¸ÔÈ÷´øµ¥¿ä
-
¨ä¶ò°¡¶ò°¡»× 08-08-07 02:49
¼Ò½ºº¸±â
-
°£´ÜÇÏ°Ô ÀÛ¼ºÇÏ½Å°Ç ¾Æ´Ñ°Í°°±º¿ä....^^;
ÁÁÀº ÀÚ·á °¨»çÇÕ´Ï´Ù.......... -
À½¾Ç»ç¶û 08-08-08 10:09
¼Ò½ºº¸±â
-
¾ÆÇÏ! °£´ÜÇÏ°Ô.. ÀÌ ¸»ÀÌ ¿ÀÇØÀÇ ¼ÒÁö°¡ Àֳ׿ä^^
Á¦°¡ ¹¹.. õÀç¶óµµ µÇ¾î¼ ½É½ÉÇ®ÀÌ·Î ½±°Ô ¸¸µé¾ú´Ù´Â ¶æÀÌ ¾Æ´Ï±¸¿ä..
¸ÞÀÏÀ» Àü¼ÛÇϴµ¥ ±âº»ÀûÀ¸·Î ÇÊ¿äÇÏ´Ù°í »ý°¢µÇ¾îÁö´Â °Íµé¸¸À¸·Î ±¸¼ºÇÏ¿´´Ù°í Çؼ °£´ÜÇÏ°Ô ¶ó°í Ç¥ÇöÇÏ¿´½À´Ï´Ù. ºÎ°¡ÀûÀÎ ±â´ÉµéÀº ¸ðµÎ ºüÁ³À¸´Ï±î¿ä...
-
¾Æ¸£Æä³Ä 08-08-19 11:55
¼Ò½ºº¸±â
- ¾Æ¹«¸® Çì´õ Àß ½áµµ ¸¹ÀÌ ÇÊÅ͸µ µÇ±¸ Æû ±úÁö°í ÇÏ´øµ¥¿ä!! ÀÌ°Ç 4.x À̶ó Å×½ºÆ® ¸øÇغ¼°Í °°±¸...
-
À¯È´Ï 08-10-31 13:41
¼Ò½ºº¸±â
- protected var·Î º¯°æÇÏ°í ÇÔ¼ö ¾Õ¿¡ ÀÖ´Â public »©¸é 4.x ´ëµµ µË´Ï´Ù..
-
¼ÕÀÚ¼ 08-11-25 19:31
¼Ò½ºº¸±â
-
À߸¸µç ÇÔ¼ö °¨»çÇÕ´Ï´Ù.
³ëÆĽÉÀÌÁö¸¸,,
protected function makeMailHeader()
{
// º¸³¾ ¸ÞÀÏÀÇ Çì´õ¸¦ ÀÛ¼ºÇÑ´Ù
$header = "";
foreach($this->MailHeaderArray as $Key => $Val)
$header .= $Key . ": " . $Val . "\r\n";
return $header . "\r\n";
}
¿¡¼
$header .= $Key . ": " . $Val . "\r\n";
¶§¹®¿¡ qmail ¿¡¼ header °¡ ±úÁ® ƯÈ÷ ¾Æ¿ô·è¿¡¼ Á¦´ë·Î Àü¼ÛÀÌ ¾ÈµÇ´Â°Í °°³×¿ë
$header .= $Key . ": " . $Val . "\n";
·Î ±³Ã¼ÇØÁÖ¸é ¾Æ¿ô·è¿¡¼µµ Àß º¸ÀÔ´Ï´ç ^^
1 2 3 4 5 6 7 8 9 10