반응형

안녕하세요 가야태자 @talkit 입니다. 

일단 NGINX 관련 설정을 제외하고는 거의 끝났습니다. 

이제 Email 알림을 동작하도록 해보겠습니다. 

우선 Email은 메일서버가 있어야지 구동이 가능합니다. 

그래서 SMTP서버를 구축을 할 수도 있겠지만, SMTP서버를 구축 하려면, 

1. 고정IP

2. 도메인

3. 서버설정

4. 화이트리스트 등록

등을 진행 해야해서 패스하고, 단 회사에서 SMTP 서버를 미리 운영하고 계신다면 가능 합니다. ^^

여기서는 Google SMTP 서버를 활용하는 방법을 알려드리겠습니다. 

일단 System Console에 접속합니다. 

ENVRIONMENT에서 SMTP를 클릭해서 보시면 위와 비슷하지만, 다르게 되어 있을 겁니다. 

1) SMTP서버는 smtp.gmail.com으로 변경 합니다.

2) 포트는 587로 변경 합니다. 

3) Enable SMTP Authenication 을 true 로 당연히 변경 하셔야 하구요.

4) SMTP Server Username 을 gmail 아이디를 넣습니다. 

5) SMTP Server Password는 gmail 앱 비밀번호를 넣어야 하는데 잠시 뒤에 설명해 드리겠습니다. 

6) Connection Security는 STARTTLS를 선택 합니다. 

여기까지 선택하시면 SMTP 설정은 끝났습니다. 

Gmail 초창기에는 Gmail 패스워드로 밖에서 SMTP를 이용할 수 있었는데요. 지금은

안됩니다. 

Gmail SMTP 앱 비밀번호 설정하기

https://myaccount.google.com/

 

Google 계정

Google은 사용자마다 원하는 개인정보 보호 설정이 다르다는 사실을 인지하고 있습니다. 그래서 Google 계정에서는 사용이 간편한 컨트롤과 개인정보 보호 진단 같은 도구를 제공합니다. 이런 도구

www.google.com

Google 계정에 접속을 합니다. 

접속하셔서 왼쪽에 보안을 누르십시오.

Google에 로그인을 찾으셔서 앱비밀번호를 클릭 하십시오.

그러면 계정 비밀번호를 묻습니다.

로그인 하시구요.

저는 이미 생성해서 생성했다가 지워 버릴 것입니다. ^^

앱 선택이라고 되어 있는 콤보박스에서 메일,  기기선택에서 기타(맞춤기기)를 선택하시구요. 

생성 버튼을 누릅니다.

저기 안에 노란색 상자 안에 비밀번호가 있습니다. 복사를 하십시오.

System Console의 SMTP Server Password 란에 붙여 넣으시면 됩니다. 

이게 16자리 비밀번호 이고 16자라가 다 붙어 있습니다. 복사하면 구글에서 자동으로 해주는 것 같습니다. 

그리고 저장하신 다음에 Test Connection을 눌러 보시면 저는 잘 접속이 되었습니다. 

그러면 된 것입니다. 

 알람 설정하기

이제 System Console의 SITE CONFIGURATION 메뉴에서 Notifications를 클릭하십시오.

위 그림과 같이 만들어야 합니다. 

1) Show 블라블라는  true

2) Enable Email Notification 은 true 당연하겠지만, 위의 SMTP설정을 하지 않으면 불가능 합니다. 

3) Notification Display Name 은 저는 그냥 소프트웨어 이름 + Notification으로 했습니다. 

4) Address 로 끝나는 아이들은 전부 제 개인메일로 설정 했습니다. 지인 또는 가족들하고 쓸꺼라서요 ^^

Push Notification Contents는 Full 블라블라로 선택해두었습니다. 

그리고 Save 해주시면 됩니다. 

설정을 하고 저렇게 쳤습니다. ^^

오 예쁘게 메일이 왔습니다. ^^

그런데 ,^^  Footer 부분을 조금 손을 대야 할 것 같습니다. 

저주소를 바꾸는 부분이 없네요 조금 살펴 봐야 할 것 같습니다. 

여기까지 되셨으면 잘 된겁니다. 

감사합니다. 

 

반응형
반응형
<?php

/***************************************/
/* 파일명 : class.Smtp.php
/* 제작일 : 2002-08-30
/* 제작자 : 하근호(hopegiver@korea.com)
/***************************************/

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($path, $name="", $ctype="application/octet-stream"
) {
       if(
file_exists($path
)) {
           
$fp = fopen($path, "r"
);
           
$message = fread($fp, filesize($path
));
           
fclose($fp
);
           
$this->parts[] = array ("ctype" => $ctype, "message" => $message, "name" => $name
);
       } else return
false
;
   }

   
// 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->send("photon0@hanmail.net", "hopegiver@whois.co.kr", "이 메일은 정상입니다.", "정상적인 메일이니 삭제하지 마십시오.");
*/

?>
반응형

+ Recent posts