소켓으로 매일보내기
PHP 2009. 9. 9. 11:34
<?
class JMail
{
VAR $sp;
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;
VAR $subject;
VAR $body; // Mail body
VAR $content_type;
VAR $charset=\"euc-kr\";
VAR $enctype=\"8bit\";
VAR $attach;
VAR $attach_name;
VAR $attach_type;
VAR $attach_size;
VAR $boundary=\"----=_NextPart_JMailer_\";
VAR $msg=\"<p><font face=\'굴림\' size=\'3\'><b>JMail Error Message</b></font></p>\";
/*
Constructor
*/
function JMail($host=\"localhost\", $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<p>Error Message :<br>$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; $i<sizeof($this->rMail)+1; $i++)
{
fputs($this->sp, \"rcpt to: <\".$this->rMail[$i].\">\\r\\n\");
$retval = fgets($this->sp, 128);
}
return $retval;
}
function putTo()
{
fputs($this->sp, \"To: \");
for ($i=1; $i<sizeof($this->rMail)+1; $i++)
{
$str.=\"\\\"\".$this->rName[$i].\"\\\"\".\" <\".$this->rMail[$i].\">\";
if ($i < sizeof($this->rMail))
{
$str=$str.\",\\r\\n\\t\";
}
else
{
$str.=\"\\r\\n\";
}
}
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\", \"<br>\", $this->body);
}
fputs($this->sp, \"Content-Type: \".$this->content_type.\";\\r\\n\");
fputs($this->sp, \" charset=\\\"\".$this->charset.\"\\\"\\r\\n\");
fputs($this->sp, \"Content-Transfer-Encoding: \".$this->enctype.\"\\r\\n\\r\\n\");
fputs($this->sp, $this->body);
fputs($this->sp, \"\\r\\n\");
}
/*
Send mail
*/
function send()
{
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 JMailer\\r\\n\"); fgets($this->sp, 128);
fputs($this->sp, \"mail from: <\".$this->sMail.\">\\r\\n\"); $retval[0] = fgets($this->sp, 128);
$this->putRcpt();
fputs($this->sp, \"data\\r\\n\"); fgets($this->sp, 128);
fputs($this->sp, \"Return-Path: <\".$this->returnpath.\">\\r\\n\");
fputs($this->sp, \"From: \".$this->sName.\"<\".$this->sMail.\">\\r\\n\");
$this->putTo();
fputs($this->sp, \"Subject: \".$this->subject.\"\\r\\n\");
fputs($this->sp, \"MIME-Version: 1.0\\r\\n\");
if ($this->attach)
{
fputs($this->sp, \"Content-Type: multipart/mixed;\\r\\n\");
fputs($this->sp, \" boundary=\\\"\".$this->boundary.\"\\\"\\r\\n\");
fputs($this->sp, \"\\r\\nThis is a multi-part message in MIME format\\r\\n\\r\\n\");
fputs($this->sp, \"--\".$this->boundary.\"\\r\\n\");
$this->sendBody();
fputs($this->sp, \"--\".$this->boundary.\"\\r\\n\");
fputs($this->sp, \"Content-Type: \".$this->getAttachedFileType().\";\\r\\n\");
fputs($this->sp, \" name=\\\"\".$this->attach_name.\"\\\"\\r\\n\");
fputs($this->sp, \"Content-Transfer-Encoding: base64\\r\\n\");
fputs($this->sp, \"Content-Disposition: attachment;\\r\\n\");
fputs($this->sp, \" filename=\\\"\".$this->attach_name.\"\\\"\\r\\n\\r\\n\");
fputs($this->sp, $this->getAttachedFile());
fputs($this->sp, \"\\r\\n\");
fputs($this->sp, \"--\".$this->boundary.\"--\\r\\n\");
}
else
{
$this->sendBody();
}
fputs($this->sp, \"\\r\\n.\\r\\n\");
$retval[1]=fgets($this->sp, 128);
$this->close();
if (!ereg(\"^250\", $retval[0]) || !ereg(\"^250\", $retval[1]))
{
$errormsg=\"메일을 보내지 못하였습니다.<br>\";
$errormsg.=$retval[0].\"<br>\".$retval[1].\"<br><br><br>\";
$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(\"<b>\".$this->msg.$msg.\"</b>\");
}
}
?>
class JMail
{
VAR $sp;
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;
VAR $subject;
VAR $body; // Mail body
VAR $content_type;
VAR $charset=\"euc-kr\";
VAR $enctype=\"8bit\";
VAR $attach;
VAR $attach_name;
VAR $attach_type;
VAR $attach_size;
VAR $boundary=\"----=_NextPart_JMailer_\";
VAR $msg=\"<p><font face=\'굴림\' size=\'3\'><b>JMail Error Message</b></font></p>\";
/*
Constructor
*/
function JMail($host=\"localhost\", $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<p>Error Message :<br>$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; $i<sizeof($this->rMail)+1; $i++)
{
fputs($this->sp, \"rcpt to: <\".$this->rMail[$i].\">\\r\\n\");
$retval = fgets($this->sp, 128);
}
return $retval;
}
function putTo()
{
fputs($this->sp, \"To: \");
for ($i=1; $i<sizeof($this->rMail)+1; $i++)
{
$str.=\"\\\"\".$this->rName[$i].\"\\\"\".\" <\".$this->rMail[$i].\">\";
if ($i < sizeof($this->rMail))
{
$str=$str.\",\\r\\n\\t\";
}
else
{
$str.=\"\\r\\n\";
}
}
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\", \"<br>\", $this->body);
}
fputs($this->sp, \"Content-Type: \".$this->content_type.\";\\r\\n\");
fputs($this->sp, \" charset=\\\"\".$this->charset.\"\\\"\\r\\n\");
fputs($this->sp, \"Content-Transfer-Encoding: \".$this->enctype.\"\\r\\n\\r\\n\");
fputs($this->sp, $this->body);
fputs($this->sp, \"\\r\\n\");
}
/*
Send mail
*/
function send()
{
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 JMailer\\r\\n\"); fgets($this->sp, 128);
fputs($this->sp, \"mail from: <\".$this->sMail.\">\\r\\n\"); $retval[0] = fgets($this->sp, 128);
$this->putRcpt();
fputs($this->sp, \"data\\r\\n\"); fgets($this->sp, 128);
fputs($this->sp, \"Return-Path: <\".$this->returnpath.\">\\r\\n\");
fputs($this->sp, \"From: \".$this->sName.\"<\".$this->sMail.\">\\r\\n\");
$this->putTo();
fputs($this->sp, \"Subject: \".$this->subject.\"\\r\\n\");
fputs($this->sp, \"MIME-Version: 1.0\\r\\n\");
if ($this->attach)
{
fputs($this->sp, \"Content-Type: multipart/mixed;\\r\\n\");
fputs($this->sp, \" boundary=\\\"\".$this->boundary.\"\\\"\\r\\n\");
fputs($this->sp, \"\\r\\nThis is a multi-part message in MIME format\\r\\n\\r\\n\");
fputs($this->sp, \"--\".$this->boundary.\"\\r\\n\");
$this->sendBody();
fputs($this->sp, \"--\".$this->boundary.\"\\r\\n\");
fputs($this->sp, \"Content-Type: \".$this->getAttachedFileType().\";\\r\\n\");
fputs($this->sp, \" name=\\\"\".$this->attach_name.\"\\\"\\r\\n\");
fputs($this->sp, \"Content-Transfer-Encoding: base64\\r\\n\");
fputs($this->sp, \"Content-Disposition: attachment;\\r\\n\");
fputs($this->sp, \" filename=\\\"\".$this->attach_name.\"\\\"\\r\\n\\r\\n\");
fputs($this->sp, $this->getAttachedFile());
fputs($this->sp, \"\\r\\n\");
fputs($this->sp, \"--\".$this->boundary.\"--\\r\\n\");
}
else
{
$this->sendBody();
}
fputs($this->sp, \"\\r\\n.\\r\\n\");
$retval[1]=fgets($this->sp, 128);
$this->close();
if (!ereg(\"^250\", $retval[0]) || !ereg(\"^250\", $retval[1]))
{
$errormsg=\"메일을 보내지 못하였습니다.<br>\";
$errormsg.=$retval[0].\"<br>\".$retval[1].\"<br><br><br>\";
$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(\"<b>\".$this->msg.$msg.\"</b>\");
}
}
?>