{"id":547,"date":"2002-09-06T02:01:37","date_gmt":"2002-09-06T02:01:37","guid":{"rendered":"http:\/\/dev.jblove.net\/?p=547"},"modified":"2002-09-06T02:01:37","modified_gmt":"2002-09-06T02:01:37","slug":"smtp","status":"publish","type":"post","link":"https:\/\/jblove.net\/?p=547","title":{"rendered":"SMTP ?????"},"content":{"rendered":"<p><?php\n\n\n\/***************************************\/<br \/>\n\/* \ud30c\uc77c\uba85 : class.Smtp.php<br \/>\n\/* \uc81c\uc791\uc77c : 2002-08-30<br \/>\n\/* \uc81c\uc791\uc790 : \ud558\uadfc\ud638(hopegiver@korea.com)<br \/>\n\/***************************************\/<\/p>\n<p>class Smtp {<\/p>\n<p>    var $host;<br \/>\n    var $fp;<br \/>\n    var $self;<br \/>\n    var $lastmsg;<br \/>\n    var $parts;<br \/>\n    var $error;<br \/>\n    var $debug;<br \/>\n    var $charset;<br \/>\n    var $ctype;<\/p>\n<p>\n    function Smtp($host=&#8221;localhost&#8221;) {<br \/>\n        if($host == &#8220;self&#8221;) $this->self = true;<br \/>\n        else $this->host = $host;<br \/>\n        $this->parts = array();<br \/>\n        $this->error = array();<br \/>\n        $$this->debug = 0;<br \/>\n        $this->charset = &#8220;euc-kr&#8221;;<br \/>\n        $this->ctype = &#8220;text\/html&#8221;;<br \/>\n    }<\/p>\n<p>    \/\/ \ub514\ubc84\uadf8 \ubaa8\ub4dc : 1<br \/>\n    function debug($n=1) {<br \/>\n        $this->debug = $n;<br \/>\n    }<\/p>\n<p>    \/\/ smtp \ud1b5\uc2e0\uc744 \ud55c\ub2e4.<br \/>\n    function dialogue($code, $cmd) {<\/p>\n<p>        fputs($this->fp, $cmd.&#8221;<br \/>\n&#8220;);<br \/>\n        $line = fgets($this->fp, 1024);<br \/>\n        ereg(&#8220;^([0-9]+).(.*)$&#8221;, $line, &#038;$data);<br \/>\n        $this->lastmsg = $data[0];<\/p>\n<p>        if($this->debug) {<br \/>\n            echo htmlspecialchars($cmd).&#8221;<br \/>&#8220;.$this->lastmsg.&#8221;<br \/>&#8220;;<br \/>\n            flush();<br \/>\n        }<\/p>\n<p>        if($data[1] != $code) return false;<br \/>\n        return true;<\/p>\n<p>    }<\/p>\n<p>    \/\/  smptp \uc11c\ubc84\uc5d0 \uc811\uc18d\uc744 \ud55c\ub2e4.<br \/>\n    function smtp_connect($host) {<\/p>\n<p>        if($this->debug) {<br \/>\n            echo &#8220;SMTP($host) Connecting&#8230;<br \/>&#8220;;<br \/>\n            flush();<br \/>\n        }<\/p>\n<p>        if(!$host) $host = $this->host;<br \/>\n        if(!$this->fp = fsockopen($host, 25, $errno, $errstr, 10)) {<br \/>\n            $this->lastmsg = &#8220;SMTP($host) \uc11c\ubc84\uc811\uc18d\uc5d0 \uc2e4\ud328\ud588\uc2b5\ub2c8\ub2e4.[$errno:$errstr]&#8221;;<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        $line = fgets($this->fp, 1024);<br \/>\n        ereg(&#8220;^([0-9]+).(.*)$&#8221;, $line, &#038;$data);<br \/>\n        $this->lastmsg = $data[0];<br \/>\n        if($data[1] != &#8220;220&#8221;) return false;<\/p>\n<p>        if($this->debug) {<br \/>\n            echo $this->lastmsg.&#8221;<br \/>&#8220;;<br \/>\n            flush();<br \/>\n        }<\/p>\n<p>        $this->dialogue(250, &#8220;HELO phpmail&#8221;);<br \/>\n        return true;<\/p>\n<p>    }<\/p>\n<p>    \/\/ stmp \uc11c\ubc84\uc640\uc758 \uc811\uc18d\uc744 \ub04a\ub294\ub2e4.<br \/>\n    function smtp_close() {<\/p>\n<p>        $this->dialogue(221, &#8220;QUIT&#8221;);<br \/>\n        fclose($this->fp);<br \/>\n        return true;<\/p>\n<p>    }<\/p>\n<p>    \/\/ \uba54\uc2dc\uc9c0\ub97c \ubcf4\ub0b8\ub2e4.<br \/>\n    function smtp_send($email, $from, $data) {<\/p>\n<p>        if(!$mail_from = $this->get_email($from)) return false;<br \/>\n        if(!$rcpt_to = $this->get_email($email)) return false;<\/p>\n<p>        if(!$this->dialogue(250, &#8220;MAIL FROM: $mail_from&#8221;)) <br \/>\n            $this->error[] = $email.&#8221;:MAIL FROM \uc2e4\ud328($this->lastmsg)&#8221;;<br \/>\n        if(!$this->dialogue(250, &#8220;RCPT TO: $rcpt_to&#8221;))<br \/>\n            $this->error[] = $email.&#8221;:RCPT TO \uc2e4\ud328($this->lastmsg)&#8221;;<br \/>\n        $this->dialogue(354, &#8220;DATA&#8221;);<\/p>\n<p>        $mime = &#8220;Message-ID: <\".$this->get_message_id().&#8221;><br \/>\n&#8220;;<br \/>\n        $mime .= &#8220;From: $from<br \/>\n&#8220;;<br \/>\n        $mime .= &#8220;To: $email<br \/>\n&#8220;;<\/p>\n<p>        fputs($this->fp, $mime);<br \/>\n        fputs($this->fp, $data);<br \/>\n        $this->dialogue(250, &#8220;.&#8221;);<\/p>\n<p>    }<\/p>\n<p>    \/\/ Message ID \ub97c \uc5bb\ub294\ub2e4.<br \/>\n  function get_message_id() {<br \/>\n    $id = date(&#8220;YmdHis&#8221;,time());<br \/>\n    mt_srand((float) microtime() * 1000000);<br \/>\n    $randval = mt_rand();<br \/>\n    $id .= $randval.&#8221;@phpmail&#8221;;<br \/>\n    return $id;<br \/>\n  }<\/p>\n<p>    \/\/ Boundary \uac12\uc744 \uc5bb\ub294\ub2e4.<br \/>\n  function get_boundary() {<br \/>\n    $uniqchr = uniqid(time());<br \/>\n    $one = strtoupper($uniqchr[0]);<br \/>\n    $two = strtoupper(substr($uniqchr,0,8));<br \/>\n    $three = strtoupper(substr(strrev($uniqchr),0,8));<br \/>\n    return &#8220;&#8212;-=_NextPart_000_000${one}_${two}.${three}&#8221;;<br \/>\n  }<\/p>\n<p>    \/\/ \ucca8\ubd80\ud30c\uc77c\uc774 \uc788\uc744 \uacbd\uc6b0 \uc774 \ud568\uc218\ub97c \uc774\uc6a9\ud574 \ud30c\uc77c\uc744 \ucca8\ubd80\ud55c\ub2e4.<br \/>\n    function attach($message, $name=&#8221;&#8221;, $ctype=&#8221;application\/octet-stream&#8221;) {<br \/>\n        $this->parts[] = array (&#8220;ctype&#8221; => $ctype, &#8220;message&#8221; => $message, &#8220;name&#8221; => $name);<br \/>\n    }<\/p>\n<p>    \/\/ Multipart \uba54\uc2dc\uc9c0\ub97c \uc0dd\uc131\uc2dc\ud0a8\ub2e4.<br \/>\n    function build_message($part) {<\/p>\n<p>        $msg .= &#8220;Content-Type: &#8220;.$part[&#8216;ctype&#8217;];<br \/>\n        if($part[&#8216;name&#8217;]) $msg .= &#8220;; name=&#8221;&#8221;.$part[&#8216;name&#8217;].&#8221;&#8221;&#8221;;<br \/>\n        $msg .= &#8220;<br \/>\nContent-Transfer-Encoding: base64<br \/>\n&#8220;;<br \/>\n        $msg .= &#8220;Content-Disposition: attachment; filename=&#8221;&#8221;.$part[&#8216;name&#8217;].&#8221;&#8221;<\/p>\n<p>&#8220;;<br \/>\n        $msg .= chunk_split(base64_encode($part[&#8216;message&#8217;]));<br \/>\n        return $msg;<\/p>\n<p>    }<\/p>\n<p>    \/\/ SMTP\uc5d0 \ubcf4\ub0bc DATA\ub97c \uc0dd\uc131\uc2dc\ud0a8\ub2e4.<br \/>\n    function build_data($subject, $body) {<\/p>\n<p>        $boundary = $this->get_boundary();<\/p>\n<p>        $mime .= &#8220;Subject: $subject<br \/>\n&#8220;;<br \/>\n        $mime .= &#8220;Date: &#8220;.date (&#8220;D, j M Y H:i:s T&#8221;,time()).&#8221;<br \/>\n&#8220;;<br \/>\n        $mime .= &#8220;MIME-Version: 1.0<br \/>\n&#8220;;<br \/>\n        $mime .= &#8220;Content-Type: multipart\/mixed; boundary=&#8221;&#8221;.$boundary.&#8221;&#8221;<\/p>\n<p>&#8220;.<br \/>\n                 &#8220;This is a multi-part message in MIME format.<\/p>\n<p>&#8220;;<br \/>\n    $mime .= &#8220;&#8211;&#8220;.$boundary.&#8221;<br \/>\n&#8220;.<br \/>\n             &#8220;Content-Type: &#8220;.$this->ctype.&#8221;; charset=&#8221;&#8221;.$this->charset.&#8221;&#8221;<br \/>\n&#8220;.<br \/>\n             &#8220;Content-Transfer-Encoding: base64<\/p>\n<p>&#8220;.<br \/>\n             chunk_split(base64_encode($body)).<br \/>\n             &#8220;<\/p>\n<p>&#8211;&#8220;.$boundary;<\/p>\n<p>        $max = count($this->parts);<br \/>\n        for($i=0; $i<$max; $i++) {<br \/>\n            $mime .= &#8220;<br \/>\n&#8220;.$this->build_message($this->parts[$i]).&#8221;<\/p>\n<p>&#8211;&#8220;.$boundary;<br \/>\n        }<br \/>\n        $mime .= &#8220;&#8211;<br \/>\n&#8220;;<\/p>\n<p>        return $mime;<\/p>\n<p>    }<\/p>\n<p>    \/\/ MX \uac12\uc744 \ucc3e\ub294\ub2e4.<br \/>\n    function get_mx_server($email) {<\/p>\n<p>        if(!ereg(&#8220;([._0-9a-zA-Z-]+)@([0-9a-zA-Z-]+.[a-zA-Z.]+)&#8221;, $email, $reg)) return false;<br \/>\n        getmxrr($reg[2], $host);<br \/>\n        if(!$host) $host[0] = $reg[2];<br \/>\n        return $host;<\/p>\n<p>    }<\/p>\n<p>    \/\/ \uc774\uba54\uc77c\uc758 \ud615\uc2dd\uc774 \ub9de\ub294\uc9c0 \uccb4\ud06c\ud55c\ub2e4.<br \/>\n    function get_email($email) {<br \/>\n        if(!ereg(&#8220;([._0-9a-zA-Z-]+)@([0-9a-zA-Z-]+.[a-zA-Z.]+)&#8221;, $email, $reg)) return false;<br \/>\n        return &#8220;<\".$reg[0].\">&#8220;;<br \/>\n    }<\/p>\n<p>\n    \/\/ \uba54\uc77c\uc744 \uc804\uc1a1\ud55c\ub2e4.<br \/>\n    function send($to, $from, $subject, $body) {<\/p>\n<p>        if(!is_array($to)) $to = split(&#8220;[,;]&#8221;,$to);<br \/>\n        if($this->self) {<\/p>\n<p>            $data = $this->build_data($subject, $body);<br \/>\n            foreach($to as $email) {<br \/>\n                if($host = $this->get_mx_server($email)) {<br \/>\n                    $flag = false; $i = 0;<br \/>\n                    while($flag == false) {<br \/>\n                        if($host[$i]) {<br \/>\n                            $flag = $this->smtp_connect($host[$i]);<br \/>\n                            $i++;<br \/>\n                        } else break;<br \/>\n                    }<br \/>\n                    if($flag) {<br \/>\n                        $this->smtp_send($email, $from, $data);<br \/>\n                        $this->smtp_close();<br \/>\n                    } else {<br \/>\n                        $this->error[] = $email.&#8221;:SMTP \uc811\uc18d\uc2e4\ud328&#8221;;<br \/>\n                    }<br \/>\n                } else {<br \/>\n                    $this->error[] = $email.&#8221;:\ud615\uc2dd\uc774 \uc798\ubabb\ub428&#8221;;<br \/>\n                }<br \/>\n            }<\/p>\n<p>        } else {<\/p>\n<p>            if(!$this->smtp_connect($this->host)) {<br \/>\n                $this->error[] = &#8220;$this->host SMTP \uc811\uc18d\uc2e4\ud328&#8221;;<br \/>\n                return false;<br \/>\n            }<br \/>\n            $data = $this->build_data($subject, $body);<br \/>\n            foreach($to as $email) $this->smtp_send($email, $from, $data);<br \/>\n            $this->smtp_close();<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>\/* \uc0ac\uc6a9\ubc95<br \/>\n$mail = new Smtp(&#8220;self&#8221;);<br \/>\n$mail->debug(); <br \/>\n$mail->send(&#8220;photon0@hanmail.net&#8221;, &#8220;hopegiver@whois.co.kr&#8221;, &#8220;\uc774 \uba54\uc77c\uc740 \uc815\uc0c1\uc785\ub2c8\ub2e4.&#8221;, &#8220;\uc815\uc0c1\uc801\uc778 \uba54\uc77c\uc774\ub2c8 \uc0ad\uc81c\ud558\uc9c0 \ub9c8\uc2ed\uc2dc\uc624.&#8221;);<br \/>\n*\/<\/p>\n<p>?><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[51],"tags":[],"class_list":["post-547","post","type-post","status-publish","format-standard","hentry","category-papa-php"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/jblove.net\/index.php?rest_route=\/wp\/v2\/posts\/547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jblove.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jblove.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jblove.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jblove.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=547"}],"version-history":[{"count":0,"href":"https:\/\/jblove.net\/index.php?rest_route=\/wp\/v2\/posts\/547\/revisions"}],"wp:attachment":[{"href":"https:\/\/jblove.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jblove.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jblove.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}