PHP [네이버] 외부 메일로 네이버 메일 사용하기
페이지 정보

본문
네이버 이메일을 웹페이지에 SMTP 외부메일 설정에서 사용하는 방법입니다.
네이버 2단계 인증관리 > 애플리케이션 등록
https://help.naver.com/service/5640/contents/8584?lang=ko
1. 구조도
├ lib
│ └ mailer.lib.php
├ plugin
│ └ PHPMailer-6.9.3
│ └ src
│ ├ DSNConfigurator.php
│ ├ Exception.php
│ ├ OAuth.php
│ ├ OAuthTokenProvider.php
│ ├ PHPMailer.php
│ ├ POP3.php
│ └ SMTP.php
└ test_mail.php
2. 네이버 메일에서 SMTP 사용으로 변경
네이버 메일 > 환경설정 > POS3/IMAP 설정 > IMAP/SMTP 설정 페이지에서
POS3/SMTP 사용 > 사용함 으로 변경
3. PHPMailer 폴더 업로드
PHPMailer 다운로드 후 plugin 폴더에 업로드
PHPMailer 최신버전을 다운로드 : https://github.com/PHPMailer/PHPMailer
4. lib / mailer_naver.lib.php 생성
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
function mailer_naver($to_email, $to_name, $from_email, $from_name, $subject, $content, $is_html = true) {
require_once('/절대경로/plugin/PHPMailer-6.9.3/src/PHPMailer.php');
require_once('/절대경로/plugin/PHPMailer-6.9.3/src/SMTP.php');
require_once('/절대경로/plugin/PHPMailer-6.9.3/src/Exception.php');
$mail = new PHPMailer(true);
try {
// SMTP 설정
$mail->isSMTP();
$mail->Host = 'smtp.naver.com';
$mail->SMTPAuth = true;
$mail->Username = '네이버_아이디@naver.com'; // 네이버 이메일 주소
$mail->Password = '네이버_앱 _비밀번호'; // 네이버 앱 비밀번호 (2단계 인증 앱비밀번호)
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL
$mail->Port = '465'; // 네이버 SMTP 포트
// 한글 깨짐 방지
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
// 보내는 사람 설정
$mail->setFrom($from_email, '=?UTF-8?B?'.base64_encode($from_name).'?=');
$mail->addAddress($to_email, '=?UTF-8?B?'.base64_encode($to_name).'?=');
// 메일 제목 한글 깨짐 방지
$mail->Subject = '=?UTF-8?B?'.base64_encode($subject).'?=';
// HTML 메일 여부 설정
$mail->isHTML($is_html);
$mail->Body = $content;
// 메일 전송
return $mail->send();
} catch (Exception $e) {
error_log('메일 발송 실패: ' . $mail->ErrorInfo);
return false;
}
}
5. test_mail.php 만들어서 웹브라우저로 실행
<?php
$to_email = '받는_이메일주소'; // 받을 이메일 주소
$to_name = '받는사람_이름'; // 받는 이름
$from_email = '네이버_아이디@naver.com'; // mailer_naver.lib.php 에 등록정보와 일치해야합니다
$from_name = '보내는사람_이름'; // 보내는사람_이름
$subject = 'Title 네이버 SMTP 테스트 메일'; // 이메일 제목
$content = '<p>안녕하세요! 이 메일은 한글 인코딩 테스트입니다.</p>';
include_once("./lib/mailer_naver.lib.php");
if (mailer_naver($to_email, $to_name, $from_email, $from_name, $subject, $content)) {
echo '이메일이 정상적으로 발송되었습니다.';
} else {
echo '이메일 발송에 실패했습니다.';
}
네이버 2단계 인증관리 > 애플리케이션 등록
https://help.naver.com/service/5640/contents/8584?lang=ko
1. 구조도
├ lib
│ └ mailer.lib.php
├ plugin
│ └ PHPMailer-6.9.3
│ └ src
│ ├ DSNConfigurator.php
│ ├ Exception.php
│ ├ OAuth.php
│ ├ OAuthTokenProvider.php
│ ├ PHPMailer.php
│ ├ POP3.php
│ └ SMTP.php
└ test_mail.php
2. 네이버 메일에서 SMTP 사용으로 변경
네이버 메일 > 환경설정 > POS3/IMAP 설정 > IMAP/SMTP 설정 페이지에서
POS3/SMTP 사용 > 사용함 으로 변경
3. PHPMailer 폴더 업로드
PHPMailer 다운로드 후 plugin 폴더에 업로드
PHPMailer 최신버전을 다운로드 : https://github.com/PHPMailer/PHPMailer
4. lib / mailer_naver.lib.php 생성
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
function mailer_naver($to_email, $to_name, $from_email, $from_name, $subject, $content, $is_html = true) {
require_once('/절대경로/plugin/PHPMailer-6.9.3/src/PHPMailer.php');
require_once('/절대경로/plugin/PHPMailer-6.9.3/src/SMTP.php');
require_once('/절대경로/plugin/PHPMailer-6.9.3/src/Exception.php');
$mail = new PHPMailer(true);
try {
// SMTP 설정
$mail->isSMTP();
$mail->Host = 'smtp.naver.com';
$mail->SMTPAuth = true;
$mail->Username = '네이버_아이디@naver.com'; // 네이버 이메일 주소
$mail->Password = '네이버_앱 _비밀번호'; // 네이버 앱 비밀번호 (2단계 인증 앱비밀번호)
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL
$mail->Port = '465'; // 네이버 SMTP 포트
// 한글 깨짐 방지
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
// 보내는 사람 설정
$mail->setFrom($from_email, '=?UTF-8?B?'.base64_encode($from_name).'?=');
$mail->addAddress($to_email, '=?UTF-8?B?'.base64_encode($to_name).'?=');
// 메일 제목 한글 깨짐 방지
$mail->Subject = '=?UTF-8?B?'.base64_encode($subject).'?=';
// HTML 메일 여부 설정
$mail->isHTML($is_html);
$mail->Body = $content;
// 메일 전송
return $mail->send();
} catch (Exception $e) {
error_log('메일 발송 실패: ' . $mail->ErrorInfo);
return false;
}
}
5. test_mail.php 만들어서 웹브라우저로 실행
<?php
$to_email = '받는_이메일주소'; // 받을 이메일 주소
$to_name = '받는사람_이름'; // 받는 이름
$from_email = '네이버_아이디@naver.com'; // mailer_naver.lib.php 에 등록정보와 일치해야합니다
$from_name = '보내는사람_이름'; // 보내는사람_이름
$subject = 'Title 네이버 SMTP 테스트 메일'; // 이메일 제목
$content = '<p>안녕하세요! 이 메일은 한글 인코딩 테스트입니다.</p>';
include_once("./lib/mailer_naver.lib.php");
if (mailer_naver($to_email, $to_name, $from_email, $from_name, $subject, $content)) {
echo '이메일이 정상적으로 발송되었습니다.';
} else {
echo '이메일 발송에 실패했습니다.';
}
댓글목록
등록된 댓글이 없습니다.