sm.php
<?php
클래스 smtp
{
/* 공개 변수 */
var $smtp_port;
var $time_out;
var $호스트_이름;
var $log_file;
var $relay_host;
var $디버그;
var $auth;
var $사용자;
var $패스;
/* 개인 변수 */
var $양말;
/* 생성자 */
함수 smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->디버그 = 거짓;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //fsockopen()에서 사용됩니다.
#
$this->auth = $auth;//인증
$this->사용자 = $user;
$this->pass = $pass;
#
$this->host_name = "로컬호스트"; //HELO 명령에 사용됩니다.
$this->log_file = "";
$this->sock = 거짓;
}
/* 주요 기능 */
함수 sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(rn))(\.)", " \1.\3 ", $body);
$header .= "MIME 버전:1.0rn";
if($mailtype=="HTML"){
$header .= "콘텐츠 유형:텍스트/htmlrn";
}
$header .= "받는 사람: ".$to."rn";
if ($cc != "") {
$header .= "참조: ".$cc."rn";
}
$header .= "보낸 사람: $from<".$from.">;rn";
$header .= "제목: ".$subject."rn";
$헤더 .= $additional_headers;
$header .= "날짜: ".date("r")."rn";
$header .= "X-Mailer:Redhat 작성(PHP/".phpversion().")rn";
list($msec, $sec) = 폭발(" ", 마이크로타임());
$header .= "메시지 ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">;rn";
$TO = 폭발(",", $this->strip_comment($to));
if ($cc != "") {
$TO = array_merge($TO, 폭발(",", $this->strip_comment($cc)));
}
if ($bcc != "") {
$TO = array_merge($TO, 폭발(",", $this->strip_comment($bcc)));
}
$보냄 = 참;
foreach($TO를 $rcpt_to로) {
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to)) {
$this->log_write("오류: ".$rcpt_to."로 이메일을 보낼 수 없습니다.n");
$보냄 = 거짓;
계속하다;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
$this->log_write("이메일이 <".$rcpt_to.">로 전송되었습니다.n");
} 또 다른 {
$this->log_write("오류: <".$rcpt_to.">에 이메일을 보낼 수 없습니다.n");
$보냄 = 거짓;
}
fclose($this->sock);
$this->log_write("원격 호스트 연결이 끊어졌습니다n");
}
$sent를 반환합니다.
}
/* 개인 함수 */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO", $helo)) {
return $this->smtp_error("HELO 명령을 보내는 중");
}
#인증
if($this->인증){
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
return $this->smtp_error("HELO 명령을 보내는 중");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
return $this->smtp_error("HELO 명령을 보내는 중");
}
}
#
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">;")) {
return $this->smtp_error("MAIL FROM 명령을 보내는 중");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">;")) {
return $this->smtp_error("RCPT TO 명령을 보내는 중");
}
if (!$this->smtp_putcmd("DATA")) {
return $this->smtp_error("DATA 명령 전송 중");
}
if (!$this->smtp_message($header, $body)) {
return $this->smtp_error("메시지 전송 중");
}
if (!$this->smtp_eom()) {
return $this->smtp_error("<CR>;<LF>;.<CR>;<LF>; [EOM] 보내는 중");
}
if (!$this->smtp_putcmd("QUIT")) {
return $this->smtp_error("QUIT 명령을 보내는 중");
}
TRUE를 반환합니다.
}
함수 smtp_sockopen($address)
{
if ($this->relay_host == "") {
$this->smtp_sockopen_mx($address)를 반환합니다.
} 또 다른 {
return $this->smtp_sockopen_relay();
}
}
함수 smtp_sockopen_relay()
{
$this->log_write("".$this->relay_host."를 시도 중입니다.":".$this->smtp_port."n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("오류: 릴레이 호스트 ".$this->relay_host."n")에 연결할 수 없습니다.
$this->log_write("오류: ".$errstr." (".$errno.")n");
거짓을 반환합니다;
}
$this->log_write("릴레이 호스트 ".$this->relay_host에 연결되었습니다."n");
TRUE를 반환합니다.
}
함수 smtp_sockopen_mx($address)
{
$domain = ereg_replace(" ^.+@([^@]+)$ ", " \1 ", $address);
if ( !@getmxrr($domain , $MXHOSTS)) {
$this->log_write("오류: MX "".$domain을 확인할 수 없습니다.""n");
거짓을 반환합니다;
}
foreach($MXHOSTS를 $host로) {
$this->log_write("".$host."를 시도하는 중:".$this->smtp_port."n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok())) {
$this->log_write("경고: mx 호스트 ".$host."에 연결할 수 없습니다.n");
$this->log_write("오류: ".$errstr." (".$errno.")n");
계속하다;
}
$this->log_write("mx 호스트 ".$host."에 연결되었습니다.n");
TRUE를 반환합니다.
}
$this->log_write("오류: 어떤 mx 호스트에도 연결할 수 없습니다(".implode(", ", $MXHOSTS).")n");
거짓을 반환합니다;
}
함수 smtp_message($header, $body)
{
fputs($this->sock, $header."rn".$body);
$this->smtp_debug(">; ".str_replace("rn", "n".">; ", $header."n>; ".$body."n>; ") );
TRUE를 반환합니다.
}
함수 smtp_eom()
{
fputs($this->sock, "rn.rn");
$this->smtp_debug(".[EOM]n");
$this->smtp_ok()를 반환합니다.
}
함수 smtp_ok()
{
$response = str_replace("rn", "", fgets($this->sock, 512));
$this->smtp_debug($response."n");
if (!ereg("^[23]", $response)) {
fputs($this->sock, "QUITrn");
fgets($this->sock, 512);
$this->log_write("오류: 원격 호스트가 "".$response를 반환했습니다.""n");
거짓을 반환합니다;
}
TRUE를 반환합니다.
}
함수 smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
}
fputs($this->sock, $cmd."rn");
$this->smtp_debug(">; ".$cmd."n");
$this->smtp_ok()를 반환합니다.
}
함수 smtp_error($string)
{
$this->log_write("오류: ".$string." 동안 오류가 발생했습니다.n");
거짓을 반환합니다;
}
함수 log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "") {
TRUE를 반환합니다.
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if ( !@file_exists($this->log_file ) || !($fp = @fopen($this->log_file, "a"))) {
$this->smtp_debug("경고: 로그 파일 "".$this->log_file.""n")을 열 수 없습니다.
거짓을 반환합니다;
}
무리($fp, LOCK_EX);
fputs($fp, $메시지);
fclose($fp);
TRUE를 반환합니다.
}
함수 Strip_comment($address)
{
$comment = " \([^()]*\ )";
while (ereg($comment, $address)) {
$address = ereg_replace($comment, "", $address);
}
$주소를 반환합니다.
}
함수 get_address($address)
{
$address = ereg_replace("([ trn])+", "", $address);
$address = ereg_replace("^.*<(.+)>;.*$", " \1 ", $address);
$address를 반환합니다.
}
함수 smtp_debug($message)
{
if ($this->debug) {
에코 $ 메시지;
}
}
}
?>
mail.php
<?php
require("sm.php");
#######################################
$smtpserver = "mail.asdf.com";//SMTP服务器
$smtpserverport =25;//SMTP服务器端口
$smtpusermail = " [email protected]";//SMTP 사용 용도
$smtpemailto = " [email protected]";//发送给谁
$smtpuser = " [email protected]";//SMTP가 사용 하는 용도
$smtppass = "asdf";//SMTP 사용 용도
$mailsubject = "테스트 제목";//주주님
$mailbody = "<h1>;테스트 메일입니다</h1>;";//邮件内容
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
#######################################
$smtp = 새로운 smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//여기서의 인터페이스는 true입니다.
$smtp->debug = TRUE;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);