90 lines
3.6 KiB
JavaScript
90 lines
3.6 KiB
JavaScript
/**
|
|
* 파일명: universal-mailer.js
|
|
* 설명: 사이트 전체에서 사용할 수 있는 범용 메일 발송 클라이언트
|
|
*/
|
|
(function(window) {
|
|
'use strict';
|
|
|
|
function UniversalMailer() {}
|
|
|
|
/**
|
|
* AJAX를 통해 지정된 템플릿으로 이메일을 발송합니다.
|
|
* @param {object} options - 메일 발송 옵션 객체
|
|
* @param {string} options.template_code - 사용할 메일 템플릿의 고유 코드
|
|
* @param {object} options.variables - 템플릿에 치환될 변수 객체 (예: {name: "홍길동"})
|
|
* @param {string|string[]} [options.to_email] - 수신자 이메일. (문자열 또는 배열)
|
|
* @param {string|string[]} [options.cc_email] - 참조 이메일. (문자열 또는 배열)
|
|
* @param {string|string[]} [options.bcc_email] - 숨은 참조 이메일. (문자열 또는 배열)
|
|
* @param {function} [options.onSuccess] - 발송 성공 시 실행될 콜백 함수
|
|
* @param {function} [options.onError] - 발송 실패 시 실행될 콜백 함수
|
|
* @param {function} [options.onComplete] - 성공/실패와 관계없이 마지막에 실행될 콜백 함수
|
|
*/
|
|
UniversalMailer.prototype.send = function(options) {
|
|
const formData = new FormData();
|
|
formData.append('template_code', options.template_code);
|
|
|
|
// 수신자 처리 (배열 또는 문자열)
|
|
if (options.to_email) {
|
|
if (Array.isArray(options.to_email)) {
|
|
options.to_email.forEach(email => formData.append('to_email[]', email));
|
|
} else {
|
|
formData.append('to_email', options.to_email);
|
|
}
|
|
}
|
|
|
|
// 참조자 처리 (배열 또는 문자열)
|
|
if (options.cc_email) {
|
|
if (Array.isArray(options.cc_email)) {
|
|
options.cc_email.forEach(email => formData.append('cc_email[]', email));
|
|
} else {
|
|
formData.append('cc_email', options.cc_email);
|
|
}
|
|
}
|
|
|
|
// 숨은 참조자 처리 (배열 또는 문자열)
|
|
if (options.bcc_email) {
|
|
if (Array.isArray(options.bcc_email)) {
|
|
options.bcc_email.forEach(email => formData.append('bcc_email[]', email));
|
|
} else {
|
|
formData.append('bcc_email', options.bcc_email);
|
|
}
|
|
}
|
|
|
|
if (options.variables) {
|
|
for (const key in options.variables) {
|
|
formData.append(`variables[${key}]`, options.variables[key]);
|
|
}
|
|
}
|
|
|
|
// GnuBoard의 g5_url 변수를 사용하여 AJAX 요청 URL을 생성합니다.
|
|
const ajax_url = (typeof g5_url !== 'undefined' ? g5_url : '') + '/adm/mail_manage/ajax_universal_send.php';
|
|
|
|
fetch(ajax_url, {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => {
|
|
if (!response.ok) throw new Error('서버와의 통신에 실패했습니다.');
|
|
return response.json();
|
|
})
|
|
.then(data => {
|
|
if (data.success) {
|
|
if (typeof options.onSuccess === 'function') options.onSuccess(data);
|
|
} else {
|
|
throw new Error(data.message || '알 수 없는 오류가 발생했습니다.');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
if (typeof options.onError === 'function') options.onError(error.message);
|
|
})
|
|
.finally(() => {
|
|
if (typeof options.onComplete === 'function') options.onComplete();
|
|
});
|
|
|
|
|
|
};
|
|
|
|
// mailer 객체를 window 전역 객체에 등록하여 어디서든 접근 가능하게 합니다.
|
|
window.universalMailer = new UniversalMailer();
|
|
|
|
})(window); |