[PHP] Naver,Google Smtp를 이용한 메일보내기[PHP] Naver,Google Smtp를 이용한 메일보내기

Posted at 2015. 3. 22. 18:22 | Posted in IT/php
홈페이제작업체 NuGuWeb

<h2>Naver,Google Smtp를 이용한 메일보내기</h2>


php smpt메일


얼마 전 제작했던 사이트에서 메일 발송이 되지않는다는 문제를 제기 해왔다 ㅡ.ㅡ


한 서버에서 여러명의 가상호스팅 사용자들이 메일을 사용하니 차단 먹는건 당연하게 생각한다..


Kisa에 White Domain을 신청할 경우 2주 정도의 시간이 걸린다 심사를 거치기 때문에 (Max 2주인듯)


클라가 깽판치는 상황에 White Domain 신청은 배제.. 


예상했던 일이 었지만 해결방법은 Google || Naver SMTP를 이용해 메일을 보내는거다..


보편화(?) 된 Gmail보다 Naver Smtp를 이용해서 메일을 보내겠다..


이전에 ASP.NET으로 Gmail Smtp를 이용해 메일 보내는 작업 경험 때문인지 1시간이면 될 줄 알았다.


너무 얕잡아봤다 ㅡㅡ


이 문제를 해결하려했던 과정에만 7시간이나 잡아먹었다. (깊은빡침) 


검색에서도 많은 내용이 나오지만 이전에는 되었었겠지만(?) 현재 내가 지금 필요한 시기에는 복붙신공으로 했었으나


안되따. 개발자는 시간이 생명이기에 나와 같은 다른 분들을 대비해 포스팅한다.


 1. 먼저 네이버에 로그인 후 메일 하단에 있는 환경설정 부분을 클릭한다.



 2. 환경설정의 메뉴 POP3/MAP 설정으로 이동 후 아래에 나타난 탭에서 IMAP/SMTP 설정을 클릭한다. 


하단의 정보의 경우 Php에서 메일을 보낼때의 정보로 사용된다.

이리해 PHP에서 메일 발송을 위한 셋팅은 모두 끝났다? 아니다

메일을 전송할때 우리는 PHPMailer Class를 이용해 보내겠다


추후에 버전 문제가 있을지 모르므로 현재 내가 구현된 버전을 올리겠다.


PHPMailer-master.zip

 


 3. PhpMailer 클래스 사용 



SMTPSecure = 'ssl';

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.naver.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;

//Set the encryption system to use - ssl (deprecated) or tls

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "ekfqkqhd@naver.com";

//Password to use for SMTP authentication
$mail->Password = "패스워드";

//Set who the message is to be sent from
$mail->setFrom('ekfqkqhd@naver.com', 'First Last');

//Set an alternative reply-to address
$mail->addReplyTo('ekfqkqhd@naver.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('ekfqkqhd@naver.com', 'John Doe');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("abcd", dirname(__FILE__));

//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//Attach an image file

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

기본적인 셋팅은 끝났다 메일 발송할때 로컬 환경에서는 

stream_socket_enable_crypto() 라는 에러 메시지가 

나타날 경우 PHP_INI 파일에서 open_ssl에 주석을 제거해준다.

이상 즐프~



'IT > php' 카테고리의 다른 글

[PHP] json_encode() 한글깨짐  (4) 2015.03.30
[PHP] 날짜 및 시간 관련 함수 모음  (0) 2015.03.29
[PHP] Serial 통신 - 쓰기  (0) 2015.01.22
[PHP] - $_SERVER 변수 정리  (0) 2014.09.21
cafe24 호스팅시 코드이그나이터 에러  (0) 2014.08.18
//