ȸ¿ø°¡ÀԡžÆÀ̵ð/ºñ¹øã±â
ȨÀ¸·Î

¸ÞÀÏÀü¼Û Ŭ·¡½º(SMTP Class)
16³â Àü
<?php

class Smtp {

    var $host;
    var $fp;
    var $self;
    var $lastmsg;
    var $parts;
    var $error;
    var $debug;
    var $charset;
    var $ctype;


    function Smtp($host="localhost") {
        if($host == "self") $this->self = true;
        else $this->host = $host;
        $this->parts = array();
        $this->error = array();
        $$this->debug = 0;
        $this->charset = "euc-kr";
        $this->ctype = "text/html";
    }

    // µð¹ö±× ¸ðµå : 1
    function debug($n=1) {
        $this->debug = $n;
    }

    // smtp Åë½ÅÀ» ÇÑ´Ù.
    function dialogue($code, $cmd) {

        fputs($this->fp, $cmd."\r\n");
        $line = fgets($this->fp, 1024);
        ereg("^([0-9]+).(.*)$", $line, &$data);
        $this->lastmsg = $data[0];

        if($this->debug) {
            echo htmlspecialchars($cmd)."<br>".$this->lastmsg."<br>";
            flush();
        }

        if($data[1] != $code) return false;
        return true;

    }

    //  smptp ¼­¹ö¿¡ Á¢¼ÓÀ» ÇÑ´Ù.
    function smtp_connect($host) {

        if($this->debug) {
            echo "SMTP($host) Connecting...<br>";
            flush();
        }

        if(!$host) $host = $this->host;
        if(!$this->fp = fsockopen($host, 25, $errno, $errstr, 10)) {
            $this->lastmsg = "SMTP($host) ¼­¹öÁ¢¼Ó¿¡ ½ÇÆÐÇß½À´Ï´Ù.[$errno:$errstr]";
            return false;
        }

        $line = fgets($this->fp, 1024);
        ereg("^([0-9]+).(.*)$", $line, &$data);
        $this->lastmsg = $data[0];
        if($data[1] != "220") return false;

        if($this->debug) {
            echo $this->lastmsg."<br>";
            flush();
        }

        $this->dialogue(250, "HELO phpmail");
        return true;

    }

    // stmp ¼­¹ö¿ÍÀÇ Á¢¼ÓÀ» ²÷´Â´Ù.
    function smtp_close() {

        $this->dialogue(221, "QUIT");
        fclose($this->fp);
        return true;

    }

    // ¸Þ½ÃÁö¸¦ º¸³½´Ù.
    function smtp_send($email, $from, $data) {

        if(!$mail_from = $this->get_email($from)) return false;
        if(!$rcpt_to = $this->get_email($email)) return false;

        if(!$this->dialogue(250, "MAIL FROM: $mail_from"))
            $this->error[] = $email.":MAIL FROM ½ÇÆÐ($this->lastmsg)";
        if(!$this->dialogue(250, "RCPT TO: $rcpt_to"))
            $this->error[] = $email.":RCPT TO ½ÇÆÐ($this->lastmsg)";
        $this->dialogue(354, "DATA");

        $mime = "Message-ID: <".$this->get_message_id().">\r\n";
        $mime .= "From: $from\r\n";
        $mime .= "To: $email\r\n";

        fputs($this->fp, $mime);
        fputs($this->fp, $data);
        $this->dialogue(250, ".");

    }

    // Message ID ¸¦ ¾ò´Â´Ù.
  function get_message_id() {
    $id = date("YmdHis",time());
    mt_srand((float) microtime() * 1000000);
    $randval = mt_rand();
    $id .= $randval."@phpmail";
    return $id;
  }

    // Boundary °ªÀ» ¾ò´Â´Ù.
  function get_boundary() {
    $uniqchr = uniqid(time());
    $one = strtoupper($uniqchr[0]);
    $two = strtoupper(substr($uniqchr,0,8));
    $three = strtoupper(substr(strrev($uniqchr),0,8));
    return "----=_NextPart_000_000${one}_${two}.${three}";
  }

    // ÷ºÎÆÄÀÏÀÌ ÀÖÀ» °æ¿ì ÀÌ ÇÔ¼ö¸¦ ÀÌ¿ëÇØ ÆÄÀÏÀ» ÷ºÎÇÑ´Ù.
    function attach($message, $name="", $ctype="application/octet-stream") {
        $this->parts[] = array ("ctype" => $ctype, "message" => $message, "name" => $name);
    }

    // Multipart ¸Þ½ÃÁö¸¦ »ý¼º½ÃŲ´Ù.
    function build_message($part) {

        $msg .= "Content-Type: ".$part['ctype'];
        if($part['name']) $msg .= "; name=\"".$part['name']."\"";
        $msg .= "\r\nContent-Transfer-Encoding: base64\r\n";
        $msg .= "Content-Disposition: attachment; filename=\"".$part['name']."\"\r\n\r\n";
        $msg .= chunk_split(base64_encode($part['message']));
        return $msg;

    }

    // SMTP¿¡ º¸³¾ DATA¸¦ »ý¼º½ÃŲ´Ù.
    function build_data($subject, $body) {

        $boundary = $this->get_boundary();

        $mime .= "Subject: $subject\r\n";
        $mime .= "Date: ".date ("D, j M Y H:i:s T",time())."\r\n";
        $mime .= "MIME-Version: 1.0\r\n";
        $mime .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n\r\n".
                 "This is a multi-part message in MIME format.\r\n\r\n";
    $mime .= "--".$boundary."\r\n".
             "Content-Type: ".$this->ctype."; charset=\"".$this->charset."\"\r\n".
             "Content-Transfer-Encoding: base64\r\n\r\n".
             chunk_split(base64_encode($body)).
             "\r\n\r\n--".$boundary;

        $max = count($this->parts);
        for($i=0; $i<$max; $i++) {
            $mime .= "\r\n".$this->build_message($this->parts[$i])."\r\n\r\n--".$boundary;
        }
        $mime .= "--\r\n";

        return $mime;

    }

    // MX °ªÀ» ã´Â´Ù.
    function get_mx_server($email) {
        
        if(!ereg("([\._0-9a-zA-Z-]+)@([0-9a-zA-Z-]+\.[a-zA-Z\.]+)", $email, $reg)) return false;
        getmxrr($reg[2], $host);
        if(!$host) $host[0] = $reg[2];
        return $host;

    }

    // À̸ÞÀÏÀÇ Çü½ÄÀÌ ¸Â´ÂÁö üũÇÑ´Ù.
    function get_email($email) {
        if(!ereg("([\._0-9a-zA-Z-]+)@([0-9a-zA-Z-]+\.[a-zA-Z\.]+)", $email, $reg)) return false;
        return "<".$reg[0].">";
    }


    // ¸ÞÀÏÀ» Àü¼ÛÇÑ´Ù.
    function send($to, $from, $subject, $body) {
        
        if(!is_array($to)) $to = split("[,;]",$to);
        if($this->self) {

            $data = $this->build_data($subject, $body);
            foreach($to as $email) {
                if($host = $this->get_mx_server($email)) {
                    $flag = false; $i = 0;
                    while($flag == false) {
                        if($host[$i]) {
                            $flag = $this->smtp_connect($host[$i]);
                            $i++;
                        } else break;
                    }
                    if($flag) {
                        $this->smtp_send($email, $from, $data);
                        $this->smtp_close();
                    } else {
                        $this->error[] = $email.":SMTP Á¢¼Ó½ÇÆÐ";
                    }
                } else {
                    $this->error[] = $email.":Çü½ÄÀÌ À߸øµÊ";
                }
            }

        } else {

            if(!$this->smtp_connect($this->host)) {
                $this->error[] = "$this->host SMTP Á¢¼Ó½ÇÆÐ";
                return false;
            }
            $data = $this->build_data($subject, $body);
            foreach($to as $email) $this->smtp_send($email, $from, $data);
            $this->smtp_close();

        }

    }

}

/*
$mail = new Smtp("self");  //ÀÚü¹ß¼ÛÀÏ °æ¿ì, ¼­¹ö¸¦ ÁöÁ¤ÇÒ¼öµµ ÀÖÀ½.
$mail->debug();  //µð¹ö±ëÇÒ¶§
$mail->attach("ÆÄÀϹöÆÛ", "ÆÄÀÏÀ̸§", "ÆÄÀÏŸÀÔ");
$mail->send("¹ÞÀ» »ç¶÷¸ÞÀÏÁÖ¼Ò", "º¸³»´Â»ç¶÷ ¸ÞÀÏÁÖ¼Ò", "Á¦¸ñ", "³»¿ë");

$mail = new Smtp("self");
$mail->debug();
$mail->send("photon0@hanmail.net", "hopegiver@whois.co.kr", "ÀÌ ¸ÞÀÏÀº Á¤»óÀÔ´Ï´Ù.", "Á¤»óÀûÀÎ ¸ÞÀÏÀÌ´Ï »èÁ¦ÇÏÁö ¸¶½Ê½Ã¿À.");
*/

?>

ÃßõÃßõ : 256 Ãßõ ¸ñ·Ï
¹øÈ£ Á¦¸ñ
2,885
input ÀÔ·Â ÇÊµå ¾ÕµÚ °ø¹é ½Ç½Ã°£ Á¦°Å
2,884
Placeholder Æ÷Ä¿½º½Ã °¨Ãß±â
2,883
MySQL Áߺ¹µÈ µ¥ÀÌÅ͸¦ »èÁ¦
2,882
MySQL Áߺ¹ µ¥ÀÌÅÍ È®ÀÎ
2,881
sessionStorage.getItem ¿Í sessionStorage.setItem
2,880
Á¦ÀÌÄõ¸® ·£´ýÀ¸·Î ¹è°æ»ö º¯°æ
2,879
preg match¿¡ °üÇÑ Á¤±Ô½Ä
2,878
Stream an audio file with MediaPlayer ¿Àµð¿À ÆÄÀÏ ½ºÆ®¸®¹Ö Çϱâ
2,877
Audio Streaming PHP Code
2,876
PHP $ SERVER ȯ°æ º¯¼ö Á¤¸®
2,875
Vimeo (ºñ¸Þ¿À) API ¸¦ »ç¿ëÇÏ¿© Ç÷¹À̾î ÄÁÆ®·ÑÇϱâ
2,874
iframe »ç¿ë½Ã ÇÏ´Ü¿¡ ¹ß»ýÇÏ´Â °ø¹é Á¦°Å¹æ¹ý
2,873
¾ÆÀÌÇÁ·¹ÀÓ(iframe) Àüüȭ¸é °¡´ÉÇÏ°Ô Çϱâ
2,872
ºÎÆ®½ºÆ®·¦(bootstrapk)¿¡¼­ »ç¿ëÇÏ´Â class¸í Á¤¸®
2,871
ºÎÆ®½ºÆ®·¦ CSS
2,870
Å©·Ò¿¡¼­ ¸¶Áø Á¶Àý
2,869
PHP ÇöÀç ÆäÀÌÁöÀÇ µµ¸ÞÀθíÀ̳ª urlµîÀÇ Á¤º¸ ¾Ë¾Æ¿À±â
2,868
PHP preg match all()
2,867
PHP ·Î À¥ÆäÀÌÁö ±Ü¾î¿À±â ¸ðµç ¹æ¹ý ÃÑÁ¤¸®!
2,866
[PHP] ¿ø°ÝÁö ÆÄÀÏ ÁÖ¼Ò ³ëÃâ ¾ÈÇÏ°í curl·Î ´Ù¿î·Îµå ¹Þ±â
2,865
PHP ÇÔ¼ö Á¤¸®
2,864
¾ÆÀÌÇÁ·¹ÀÓ(iframe) ºñÀ² À¯ÁöÇϸ鼭 Å©±â Á¶ÀýÇÏ´Â ¹æ¹ý
2,863
PHP ¹è¿­¿¡¼­ ¹«ÀÛÀ§·Î Çϳª »Ì¾ÆÁÖ´Â array rand() ÇÔ¼ö
2,862
PHP Á¤±Ô½Ä Á¤¸®
2,861
PHP Á¤±Ô½ÄÀ» È°¿ëÇÑ ÅÂ±× ¹× ƯÁ¤ ¹®ÀÚ¿­ Á¦°Å ¹× ÃßÃâ ¹æ¹ý
2,860
php Å©·Ñ¸µ ¶Ç´Â ÆÄ½Ì ÇÔ¼ö, Á¤±Ô½Ä ¸ðÀ½
2,859
Á¦ÀÌÄõ¸® ±âº» ¸í·É¾î
2,858
À¥ÆäÀÌÁö °¡·Î ¸ðµå¼¼·Î ¸ðµå ÀνÄÇϱâ
2,857
¸ð¹ÙÀÏ À¥ È­¸é °­Á¦ ȸÀü(°¡·Î¸ðµå °íÁ¤)
2,856
[HTML5]¿¡¼­ frameset ´ëü ¹æ¹ý°ú iframe ¼Ó¼º
¸ñ·Ï
¹ÂÁ÷Æ®·ÎÆ® ºÎ»ê±¤¿ª½Ã ºÎ»êÁø±¸ °¡¾ßµ¿ ¤Ó °³ÀÎÁ¤º¸Ãë±Þ¹æħ
Copyright ¨Ï musictrot All rights reserved.