그누보드 [G5] 외부 메일로 네이버 메일 사용하기
페이지 정보
![profile_image](https://www.happyjung.com/img/no_profile.gif)
본문
네이버 이메일을 그누보드 SMTP 외부메일 설정에서 사용하는 방법입니다.
환경설정 > 관리자메일 주소는 config.php 에 등록하는 네이버 메일주소와 일치해야 합니다.
네이버 2단계 인증관리 > 애플리케이션 등록
https://help.naver.com/service/5640/contents/8584?lang=ko
1. /config.php 173 번째 줄에 있는
define('G5_SMTP', '127.0.0.1');
define('G5_SMTP_PORT', '25');
를 아래와 같이 수정합니다.
define('G5_SMTP', 'smtp.naver.com');
define('G5_SMTP_PORT', '587');
define('G5_SMTP_SECURE', 'TLS');
define('G5_SMTP_USER', 'yourid@naver.com');
define('G5_SMTP_PW', 'yourpassword'); // 네이버 앱 비밀번호 (2단계 인증 앱비밀번호)
2. /lib/mailer.lib.php 19번째 줄
$mail = new PHPMailer(); // defaults to using php "mail()"
if (defined('G5_SMTP') && G5_SMTP) {
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = G5_SMTP; // SMTP server
if(defined('G5_SMTP_PORT') && G5_SMTP_PORT)
$mail->Port = G5_SMTP_PORT;
}
를 아래와 같이 수정합니다.
$mail = new PHPMailer(); // defaults to using php "mail()"
if (defined('G5_SMTP') && G5_SMTP) {
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = G5_SMTP; // SMTP server
//if(defined('G5_SMTP_PORT') && G5_SMTP_PORT)
// $mail->Port = G5_SMTP_PORT;
if(defined('G5_SMTP_PORT') && G5_SMTP_PORT) {
$mail->Port = G5_SMTP_PORT;
}
$mail->SMTPAuth = true;
$mail->AuthType = "LOGIN";
$mail->SMTPSecure = G5_SMTP_SECURE;
$mail->Username = G5_SMTP_USER;
$mail->Password = G5_SMTP_PW;
}
3. 일반 페이지에서 사용하기
일반페이지에서 사용할때는 별도로 만든 mailer 를 사용하는것이 좋습니다.
3-1. lib / mailer_naver.lib.php 파일 생성
<?php
if (!defined('_GNUBOARD_')) exit;
include_once("_common.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 G5_PLUGIN_PATH . '/PHPMailer-6.9.3/src/PHPMailer.php';
require_once G5_PLUGIN_PATH . '/PHPMailer-6.9.3/src/SMTP.php';
require_once G5_PLUGIN_PATH . '/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 = 'yourid@naver.com'; // 네이버 이메일 주소
//$mail->Password = 'yourpassword'; // 네이버 앱 비밀번호 (2단계 인증 앱비밀번호)
$mail->Username = G5_SMTP_USER; // 네이버 이메일 주소
$mail->Password = G5_SMTP_PW; // 네이버 앱 비밀번호 (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;
}
}
3-2. test_mail.php 파일 생성
웹브라우저로 아래 파일 실행하면 메일이 발송됩니다.
<?php
include_once('./common.php');
//include_once(G5_LIB_PATH.'/mailer.lib.php');
include_once(G5_LIB_PATH.'/mailer_naver.lib.php');
// lib 폴더에 PHPMailer 업로드
// PHPMailer 다운로드 : https://github.com/PHPMailer/PHPMailer
$from_email = $config['cf_admin_email']; // 환경설정에 등록된 이메일주소
$from_name = $config['cf_admin_email_name']; // 환경설정에 등록된 이름
$to_email = '받는_이메일주소'; // 받을 이메일 주소
$to_name = '받는_이름'; // 받는 이름
$mail_subject = 'Title 네이버 SMTP 테스트 메일'; // 이메일 제목
$mail_content = '<p>Message 이메일이 정상적으로 발송되었습니다.</p>'; // 이메일 내용
if (mailer_naver($to_email, $to_name, $from_email, $from_name, $subject, $content)) {
echo '이메일이 정상적으로 발송되었습니다.';
} else {
echo '이메일 발송에 실패했습니다.';
}
관련자료
https://sir.kr/g5_tip/22471
환경설정 > 관리자메일 주소는 config.php 에 등록하는 네이버 메일주소와 일치해야 합니다.
네이버 2단계 인증관리 > 애플리케이션 등록
https://help.naver.com/service/5640/contents/8584?lang=ko
1. /config.php 173 번째 줄에 있는
define('G5_SMTP', '127.0.0.1');
define('G5_SMTP_PORT', '25');
를 아래와 같이 수정합니다.
define('G5_SMTP', 'smtp.naver.com');
define('G5_SMTP_PORT', '587');
define('G5_SMTP_SECURE', 'TLS');
define('G5_SMTP_USER', 'yourid@naver.com');
define('G5_SMTP_PW', 'yourpassword'); // 네이버 앱 비밀번호 (2단계 인증 앱비밀번호)
2. /lib/mailer.lib.php 19번째 줄
$mail = new PHPMailer(); // defaults to using php "mail()"
if (defined('G5_SMTP') && G5_SMTP) {
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = G5_SMTP; // SMTP server
if(defined('G5_SMTP_PORT') && G5_SMTP_PORT)
$mail->Port = G5_SMTP_PORT;
}
를 아래와 같이 수정합니다.
$mail = new PHPMailer(); // defaults to using php "mail()"
if (defined('G5_SMTP') && G5_SMTP) {
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = G5_SMTP; // SMTP server
//if(defined('G5_SMTP_PORT') && G5_SMTP_PORT)
// $mail->Port = G5_SMTP_PORT;
if(defined('G5_SMTP_PORT') && G5_SMTP_PORT) {
$mail->Port = G5_SMTP_PORT;
}
$mail->SMTPAuth = true;
$mail->AuthType = "LOGIN";
$mail->SMTPSecure = G5_SMTP_SECURE;
$mail->Username = G5_SMTP_USER;
$mail->Password = G5_SMTP_PW;
}
3. 일반 페이지에서 사용하기
일반페이지에서 사용할때는 별도로 만든 mailer 를 사용하는것이 좋습니다.
3-1. lib / mailer_naver.lib.php 파일 생성
<?php
if (!defined('_GNUBOARD_')) exit;
include_once("_common.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 G5_PLUGIN_PATH . '/PHPMailer-6.9.3/src/PHPMailer.php';
require_once G5_PLUGIN_PATH . '/PHPMailer-6.9.3/src/SMTP.php';
require_once G5_PLUGIN_PATH . '/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 = 'yourid@naver.com'; // 네이버 이메일 주소
//$mail->Password = 'yourpassword'; // 네이버 앱 비밀번호 (2단계 인증 앱비밀번호)
$mail->Username = G5_SMTP_USER; // 네이버 이메일 주소
$mail->Password = G5_SMTP_PW; // 네이버 앱 비밀번호 (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;
}
}
3-2. test_mail.php 파일 생성
웹브라우저로 아래 파일 실행하면 메일이 발송됩니다.
<?php
include_once('./common.php');
//include_once(G5_LIB_PATH.'/mailer.lib.php');
include_once(G5_LIB_PATH.'/mailer_naver.lib.php');
// lib 폴더에 PHPMailer 업로드
// PHPMailer 다운로드 : https://github.com/PHPMailer/PHPMailer
$from_email = $config['cf_admin_email']; // 환경설정에 등록된 이메일주소
$from_name = $config['cf_admin_email_name']; // 환경설정에 등록된 이름
$to_email = '받는_이메일주소'; // 받을 이메일 주소
$to_name = '받는_이름'; // 받는 이름
$mail_subject = 'Title 네이버 SMTP 테스트 메일'; // 이메일 제목
$mail_content = '<p>Message 이메일이 정상적으로 발송되었습니다.</p>'; // 이메일 내용
if (mailer_naver($to_email, $to_name, $from_email, $from_name, $subject, $content)) {
echo '이메일이 정상적으로 발송되었습니다.';
} else {
echo '이메일 발송에 실패했습니다.';
}
관련자료
https://sir.kr/g5_tip/22471
댓글목록
등록된 댓글이 없습니다.