심히 걱정된다 - 2008/05/20 16:56

class JMail
{
      VAR $sp;                                                              // SMTP connection
      VAR $body;                                                              // Mail body
      VAR $host;                                                              // SMTP host
      VAR $port;                                                              // SMTP port
      VAR $sMail;                                                              // Sender mail address;
      VAR $sName;                                                              // Sender Name;
      VAR $rMail;                                                              // Receiver mail address;
      VAR $rName;                                                              // Receiver Name;
      VAR $returnpath;                                                // Return path;
      VAR $subject;                                                        // Mail subject
      VAR $content_type;                                                // Mail content type
      VAR $charset="euc-kr";                                          // Charset
      VAR $enctype="8bit";                                          // Character encoding type
      VAR $attach;                                                        // Attached file
      VAR $attach_name;                                                // Attached file name
      VAR $attach_type;                                                // Attached file type
      VAR $attach_size;                                                // Attached file size
      VAR $boundary="----=_NextPart_JMailer_";      // Default Boundary

      VAR $msg="

JMail Error Message

";

      /*
      Constructor
      */
      function JMail($host="xxx.xxx.xxx.xxx", $port="25")
      {
              $this->host=$host;
              $this->port=$port;
      }

      /*
      Connect SMTP Server
      */
      function connect()
      {
              if ($this->sp)
              {
                    fclose($this->sp);
              }
              else
              {
                    if (!$this->sp=fsockopen($this->host, $this->port, $errno, $errstr, 30))
                    {
                            $msg="Error no : $errno

Error Message :
$errstr";
                            $this->error($msg);
                    }
              }
      }
     
      /*
      Set Sender Address
      */
      function setFrom($email, $name)
      {
              if ($this->checkMail($email))
              {
                    $this->sMail=$email;
              }
              else
              {
                    $this->error("보내는 사람의 메일주소가 형식에 어긋납니다.");
              }

              if ($this->checkSpace($name))
              {
                    $this->sName=$name;
              }
              else
              {
                    $this->error("보내는 사람의 이름이 형식에 어긋납니다.");
              }
      }

      /*
      Set Receiver Address
      */
      function setReceive($email, $name)
      {
              if ($this->checkMail($email))
              {
                    $this->rMail[count($this->rMail)+1]=$email;
              }
              else
              {
                    $this->error("받는 사람의 메일주소가 형식에 어긋납니다.");
              }

              if ($this->checkSpace($name))
              {
                    $this->rName[count($this->rName)+1]=$name;
              }
              else
              {
                    $this->error("받는 사람의 이름이 형식에 어긋납니다.");
              }
      }

      /*
      Set Attach file
      */
      function setFile(&$attach, $filename, $filetype, $filesize)
      {
              $this->attach=$attach;
              $this->attach_name=$filename;
              $this->attach_type=$filetype;
              $this->attach_size=$filesize;
      }

      /*
      Set mail message
      */
      function setBody($message)
      {
              $this->body=$message;
      }

      /*
      Set Return path
      */
      function setReturnPath($email)
      {
              if ($this->checkMail($email))
              {
                    $this->returnpath=$email;
              }
              else
              {
                    $this->error("Return path의 메일주소가 형식에 어긋납니다.");
              }
      }

      /*
      Set Content type
      */
      function setContent($type)
      {
              if ($type == "text")
              {
                    $this->content_type="text/plain";
              }
              else if ($type == "html")
              {
                    $this->content_type="text/html";
              }
              else
              {
                    $this->error("Content-type 은 text, html 중 선택해 주세요");
              }
      }

      /*
      Set Subject
      */
      function setSubject($subject)
      {
              if ($this->checkSpace($subject))
              {
                    $this->subject=$subject;
              }
              else
              {
                    $this->error("메일 제목이 형식에 어긋납니다.");
              }
      }

      /*
      Set Charset
      */
      function setCharset($set)
      {
              $this->charset=$set;
      }

      /*
      Set Encoding type
      */
      function setEncoding($type)
      {
              $this->enctype=$type;
      }

      /*
      Set Boundary
      */
      function setBoundary()
      {
              $this->boundary.=time();
      }

      /*
      Return Attached file type
      */     
      function getAttachedFileType()
      {
              if (!$this->attach_type)
              {
                    return "application/octet-stream";
              }
              return $this->attach_type;
      }

      /*
      Return Encoded Attached file
      */     
      function getAttachedFile()
      {
              $fp=fopen($this->attach,"r");
              $encoded=fread($fp, $this->attach_size);
              $encoded=chunk_split(base64_encode($encoded));
              fclose($fp);

              return $encoded;
      }
     
      function putRcpt()
      {
              for ($i=1; $irMail)+1; $i++)
              {
                    fputs($this->sp, "rcpt to: <".$this->rMail[$i].">rn");
                    $retval = fgets($this->sp, 128);
              }

              return $retval;
      }

      function putTo()
      {
              fputs($this->sp, "To: ");
              for ($i=1; $irMail)+1; $i++)
              {

                    $str.=""".$this->rName[$i]."""." <".$this->rMail[$i].">";
                    if ($i < sizeof($this->rMail))
                    {
                            $str=$str.",rnt";
                    }
                    else
                    {
                            $str.="rn";
                    }
              }

              fputs($this->sp, $str);
      }

      /*
      Close SMTP Connection
      */
      function close()
      {
              fclose($this->sp);
      }

      function sendBody()
      {
              //if ($this->content_type == "text/html")
              //{
                    //$this->body=str_replace("n", "
", $this->body);
              //}
              fputs($this->sp, "Content-Type: ".$this->content_type.";rn");
              fputs($this->sp, "      charset="".$this->charset.""rn");
              fputs($this->sp, "Content-Transfer-Encoding: ".$this->enctype."rnrn");
              fputs($this->sp, $this->body);
              fputs($this->sp, "rn");
      }

      /*
      Send mail
      */
      function send()
      {
              //만약 return path가 설정이 되지 않으면 보내는 사람 메일주소를 입력한다.
              if (!$this->returnpath)      $this->returnpath=$this->sMail;
              if (!$this->content_type)      $this->content_type="text/html";
              if (!$this->sName) $this->error("보내는 사람의 이름이 지정되지 않았습니다.");
              if (!$this->sMail) $this->error("보내는 사람의 메일이 지정되지 않았습니다.");
              if (!$this->rName) $this->error("받을 사람의 이름이 지정되지 않았습니다.");
              if (!$this->rMail) $this->error("받을 사람의 메일이 지정되지 않았습니다.");
              if (!$this->subject) $this->error("메일 제목이 지정되지 않았습니다.");
              if (!$this->body) $this->error("메일 본문이 지정되지 않았습니다.");

              $this->connect();
              $this->setBoundary();

              fgets($this->sp, 128);
              fputs($this->sp, "helo JMailerrn"); fgets($this->sp, 128);
              fputs($this->sp, "mail from: <".$this->sMail.">rn");      $retval[0] = fgets($this->sp, 128);
              $this->putRcpt();
              fputs($this->sp, "datarn");                                                fgets($this->sp, 128);
              fputs($this->sp, "Return-Path: <".$this->returnpath.">rn");
              fputs($this->sp, "From: ".$this->sName."<".$this->sMail.">rn");
              $this->putTo();
              fputs($this->sp, "Subject: ".$this->subject."rn");
              fputs($this->sp, "MIME-Version: 1.0rn");

              if ($this->attach)
              {
                    fputs($this->sp, "Content-Type: multipart/mixed;rn");
                    fputs($this->sp, "      boundary="".$this->boundary.""rn");
                    fputs($this->sp, "rnThis is a multi-part message in MIME formatrnrn");
                    fputs($this->sp, "--".$this->boundary."rn");
                    $this->sendBody();

                    fputs($this->sp, "--".$this->boundary."rn");
                    fputs($this->sp, "Content-Type: ".$this->getAttachedFileType().";rn");
                    fputs($this->sp, "      name="".$this->attach_name.""rn");
                    fputs($this->sp, "Content-Transfer-Encoding: base64rn");
                    fputs($this->sp, "Content-Disposition: attachment;rn");
                    fputs($this->sp, "      filename="".$this->attach_name.""rnrn");
                    fputs($this->sp, $this->getAttachedFile());
                    fputs($this->sp, "rn");
                    fputs($this->sp, "--".$this->boundary."--rn");
              }
              else
              {
                    $this->sendBody();
              }

              fputs($this->sp, "rn.rn");
              $retval[1]=fgets($this->sp, 128);
              $this->close();

              if (!ereg("^250", $retval[0]) || !ereg("^250", $retval[1]))
              {
                    $errormsg="메일을 보내지 못하였습니다.
";
                    $errormsg.=$retval[0]."
".$retval[1]."


";
                    $this->error($errormsg);
              }
      }

      /*
      Check Mail address with regular expression
      */
      function checkMail($email)
      {
              if ($this->checkSpace($email))
              {
                    if (!ereg("(^[_0-9a-zA-Z-]+(.[_0-9a-zA-Z-]+)*@[0-9a-zA-Z-]+(.[0-9a-zA-Z-]+)*$)",trim($email)))
                    {
                            return false;
                    }
              }
              else
              {
                    return false;
              }

              return true;
      }

      /*
      Check space with regular expression
      */
      function checkSpace($str)
      {
              if (!ereg("([^[:space:]]+)",trim($str)))
              {
                    return false;
              }

              return true;
      }
      /*
      Echo error message
      $msg : error message
      */
      function error($msg)
      {
              die("".$this->msg.$msg."");
      }
}
?>

진보블로그 공감 버튼트위터로 리트윗하기페이스북에 공유하기딜리셔스에 북마크
TAG ,

Trackback Address ::

http://blog.jinbo.net/manim/trackback/8
PREV 1 ... 18 19 20 21 22 23 24 25 26 ... 29 NEXT