Add: apply nCloud Outbound Mailer api

This commit is contained in:
2018-10-02 18:37:39 +09:00
parent c6e30fccb7
commit 0f2a2352b1
7 changed files with 379 additions and 52 deletions
+5 -1
View File
@@ -111,11 +111,15 @@
function getMaestroData() { function getMaestroData() {
var maestroID = 1; var maestroID = 1;
var maestroName = "삼화초";
var email = "jisangs@daum.net";
var accountType = 1;
sendRequestServer( //url, sendParams, onSuccess, onFail, isDebugMode); sendRequestServer( //url, sendParams, onSuccess, onFail, isDebugMode);
// "./../server/mail/send_mail_bank_info.php", // "./../server/mail/send_mail_bank_info.php",
"./../server/mail/send_mail_test.php", "./../server/mail/send_mail_test.php",
"type=get_maestro_data" + "&maestro_id=" + maestroID, // "type=get_maestro_data" + "&maestro_id=" + maestroID,
"type=get_maestro_data" + "&maestro_id=" + maestroID + "&maestro_name=" + maestroName + "&email=" + email + "&account_type=" + accountType,
function(jsonData) { function(jsonData) {
console.log(jsonData); console.log(jsonData);
+2
View File
@@ -4,9 +4,11 @@ header("Content-Type: application/json");
include "./../setup/connect_db.php"; include "./../setup/connect_db.php";
include "./../lib/send_reply_json.php"; include "./../lib/send_reply_json.php";
include "./../lib/maestro_account_info.php"; include "./../lib/maestro_account_info.php";
include "./../lib/db_maestro.php";
include "./../mail/mail_setting.php"; include "./../mail/mail_setting.php";
include "./../mail/send_mail.php"; include "./../mail/send_mail.php";
$maestro_name = $_POST["maestro_name"]; $maestro_name = $_POST["maestro_name"];
$password = $_POST["password"]; $password = $_POST["password"];
$email = $_POST["email"]; $email = $_POST["email"];
+1 -1
View File
@@ -1,8 +1,8 @@
<?php <?php
header("Content-Type: application/json"); header("Content-Type: application/json");
include "./../lib/send_reply_json.php";
include "./../setup/connect_db.php"; include "./../setup/connect_db.php";
include "./../lib/send_reply_json.php";
$maestroName = $_POST["maestro_name"]; $maestroName = $_POST["maestro_name"];
$password = $_POST["password"]; $password = $_POST["password"];
@@ -0,0 +1,97 @@
<?php
$hostNameUrl = "https://mail.apigw.ntruss.com"; // 호스트 URL
$requestUrl= "/api/v1/mails"; // 요청 URI
$accessKey = "YOUR_ACCESS_KEY"; // 네이버 클라우드 플랫폼 회원에게 발급되는 개인 인증키
$secretKey = "YOUR_SECRET_KEY"; // 2차 인증을 위해 서비스마다 할당되는 service secret
$apiKey = "YOU_API_KEY"; // API Gateway에서 발급받은 키 (primary key 또는 secondary key)
$method = "POST"; // 요청 method mail 발송 : POST, 발송 확인 GET
$requestId = ""; // 요청 ID
try {
$timestamp = "";
$microtime = "";
list($microtime,$timestamp) = explode(' ',microtime());
$timestamp = $timestamp.substr($microtime, 2, 3);
$is_post = false;
if($method == "POST") {
$postParam = array(
"advertising" => false, // 광고메일여부 default: false
"attachFileIds" => array_map(), // 첨부파일ID
"body" => "", // Email 본문
"individual" => true, // 개인발송여부 (개인발송 시 참조인, 숨은참조 무시됨) default: true
"parameters" => array_map(), // 치환 파라미터 (전체 수신자에게 적용), ‘치환 ID’ 를 key로, ‘치환 ID에 맵핑되는 값’ 을 value로 가지는 Map 형태의 Object
"recipients" => array( // 수신자목록
array(
"address" => "", // 수신자 Email 주소
"name" => "", // 수신자 명
"type" => "", // 수신자 유형 (R: 수신자, C: 참조인, B: 숨은참조) default: R
"parameters" => array_map(), // 치환 파라미터 (수신자별로 적용), ‘치환 ID’ 를 key로, ‘치환 ID에 맵핑되는 값’ 을 value로 가지는 Map 형태의 Object
),
),
"referencesHeader" => "", // 특정 메일을 모아서 보기 위해 네이버 메일에서 지원하는 기능, 해당 필드에 동일한 값을 입력한 메일들을 모아서 볼 수 있다. (다음의 형태가 되어야 함 : <unique_id@domain.com> )
"reservationUtc" => "", // 예약 발송 일시 ( 1970년 1월 1일 00:00:00 협정 세계시(UTC) 부터의 경과 시간을 1/1000초로 환산한 정수 ), reservationDateTime 값보다 이 값이 우선 적용된다.
"reservationDateTime" => "", // 다음과 같은 형태의 예약 발송 일시 (yyyy-MM-dd HH:mm UTC+09:00), reservationUtc 값이 우선한다.
"senderAddress" => "", // 발송자 Email 주소
"senderName" => "", // 발송자 이름
"tempRequestId" => "", // 첨부파일 등록 시 사용한 임시 요청 ID
"templateSid" => "", // 템플릿 ID
"title" => "", // Mail 제목
);
$postParam = json_encode($postParam);
$is_post = true;
$success_code = 201;
} else {
$success_code = 200;
$requestUrl = $requestUrl."/requests/".$requestId."/status";
}
$signautue = makeSignature($secretKey, $method, $requestUrl, $timestamp, $accessKey);
$url = $hostNameUrl.$requestUrl;
echo $url."\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $is_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if($method == "POST") {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParam);
}
$headers = array();
$headers[] = "X-NCP-APIGW-TIMESTAMP: " .$timestamp;
$headers[] = "X-NCP-IAM-ACCESS-KEY: " .$accessKey;
$headers[] = "X-NCP-APIGW-SIGNATURE-V2: " .$signautue;
$headers[] = "content-type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "status_code:".$status_code."\n";
curl_close ($ch);
if($status_code == 200) {
echo $response."\n";
} else {
echo "Error 내용:".$response."\n";
}
} catch(Exception $E) {
echo "Response: ". $E->lastResponse . "\n";
}
function makeSignature($secretKey, $method, $uri, $timestamp, $accessKey) {
$space = " ";
$newLine = "\n";
$hmac = $method.$space.$uri.$newLine.$timestamp.$newLine.$accessKey;
$signautue = base64_encode(hash_hmac('sha256', $hmac, $secretKey,true));
echo "this is signiture : ".$signautue."\n";
return $signautue;
}
?>
+11 -4
View File
@@ -1,13 +1,20 @@
<?php <?php
$new_line = "\r\n"; $new_line = "\r\n";
$html_br_tag = "<br/>";
$from = "support+chocomae@jinaju.com"; $from = "support+chocomae@jinaju.com";
$bcc = "support+chocomae.automail@jinaju.com"; $bcc = "support+chocomae.automail@jinaju.com";
// load service db setting // mail sender info
$bank_name = "카카오뱅크"; $sender_name = "초코마에";
$bank_owner_name = "박지상"; $sender_email = $from;
$bank_account_no = "3333-04-4316912"; $sender_address = $from;
$sender_bcc = $bcc;
// bank account info
$bank_account_no = "3333-04-4316912";
$bank_name = "카카오뱅크";
$bank_owner_name = "박지상";
?> ?>
+257
View File
@@ -1,4 +1,260 @@
<?php <?php
header("Content-Type: application/json");
// variables for ncloud outbound mailer
$host_name_url = "https://mail.apigw.ntruss.com"; // 호스트 URL
$request_url= "/api/v1/mails"; // 요청 URI
$access_key = "qNXWk443wdnDXrkPvoqr"; // 네이버 클라우드 플랫폼 회원에게 발급되는 개인 인증키
$secret_key = "umWxQXulhNSya2CkMNqqVJnSRfwRaN8BjMUX0skV"; // 2차 인증을 위해 서비스마다 할당되는 service secret
$api_key = "eWfapJfbR5W9NXPEsdXGzlJsShEV30DfxIV0d51J"; // API Gateway에서 발급받은 키 (primary key 또는 secondary key)
$method = "POST"; // 요청 method mail 발송 : POST, 발송 확인 GET
$request_id = ""; // 요청 ID
////////////////////////
// nCloud Outbound Mailer
////////////////////////
function makeDefaultPostParamData($maestro_name, $maestro_email) {
global $sender_email, $sender_name, $sender_bcc;
$postParamData["sender_address"] = $sender_email;
$postParamData["sender_name"] = $sender_name;
$postParamData["individual"] = false;
$postParamData["title"] = null;
$postParamData["body"] = null;
$postParamData["recipients"] = array(
array(
"address" => $maestro_email, // 수신자 Email 주소
"name" => "", // 수신자 명
"type" => "R", // 수신자 유형 (R: 수신자, C: 참조인, B: 숨은참조) default: R
// "parameters" => array_map(),
// 치환 파라미터 (수신자별로 적용), ‘치환 ID’ 를 key로, ‘치환 ID에 맵핑되는 값’ 을 value로 가지는 Map 형태의 Object
"parameters" => array(
"maestro_name" => $maestro_name,
), // 치환 파라미터 (수신자별로 적용), ‘치환 ID’ 를 key로, ‘치환 ID에 맵핑되는 값’ 을 value로 가지는 Map 형태의 Object
),
array(
"address" => $sender_bcc, // 수신자 Email 주소
"name" => "", // 수신자 명
"type" => "B", // 수신자 유형 (R: 수신자, C: 참조인, B: 숨은참조) default: R
"parameters" => array_map(),
// 치환 파라미터 (수신자별로 적용), ‘치환 ID’ 를 key로, ‘치환 ID에 맵핑되는 값’ 을 value로 가지는 Map 형태의 Object
),
);
return $postParamData;
}
function sendMailRegisterBankInfo($maestro_name, $maestro_email, $account_type) {
$postParamData = makeDefaultPostParamData($maestro_name, $maestro_email);
$postParamData["templateSid"] = "134";
$postParamData["parameters"] = array(
"account_type" => get_accout_type($account_type),
"bank_account_no" => "3333-04-4316912",
"bank_name" => "카카오뱅크",
"bank_owner_name" => "박지상",
"maestro_name" => $maestro_name,
"price" => number_format(get_price($account_type))
);
sendEmail($postParamData);
}
function sendMailRegisterDone($maestro_name, $maestro_email) {
$postParamData = makeDefaultPostParamData($maestro_name, $maestro_email);
$postParamData["templateSid"] = "138";
$postParamData["parameters"] = array(
"maestro_name" => $maestro_name,
);
sendEmail($postParamData);
}
function sendMailUpgradeBankInfo($maestro_name, $maestro_email, $account_type, $upgrade_account_type) {
$postParamData = makeDefaultPostParamData($maestro_name, $maestro_email);
$postParamData["templateSid"] = "139";
$postParamData["parameters"] = array(
"account_type" => get_accout_type($account_type),
"upgrade_account_type" => get_accout_type($upgrade_account_type),
"bank_account_no" => "3333-04-4316912",
"bank_name" => "카카오뱅크",
"bank_owner_name" => "박지상",
"maestro_name" => $maestro_name,
"price" => number_format(get_price($upgrade_account_type) - get_price($account_type))
);
sendEmail($postParamData);
}
function sendMailUpgradeDone($maestro_name, $maestro_email, $account_type, $upgrade_account_type) {
$postParamData = makeDefaultPostParamData($maestro_name, $maestro_email);
$postParamData["templateSid"] = "140";
$postParamData["parameters"] = array(
"account_type" => get_accout_type($account_type),
"upgrade_account_type" => get_accout_type($upgrade_account_type),
"maestro_name" => $maestro_name,
);
sendEmail($postParamData);
}
function sendEmail($postParamData) {
global $host_name_url, $request_url, $access_key, $secret_key, $api_key, $method, $request_id;
global $sender_name, $sender_address;
$postParam = null;
if($method == "POST") {
$postParam = makePostParam($postParamData);
// echo "\n\r".$postParam."\n\r";
// set_data("postParam", $postParam);
} else {
$request_url = $request_url."/requests/".$request_id."/status";
}
$timestamp = getTimeStamp();
$signature = makeSignature($secret_key, $method, $request_url, $timestamp, $access_key);
cUrl($postParam, $timestamp, $signature);
}
function makeRequestUrl() {
global $method, $request_url, $request_id;
if($method == "POST")
return $request_url;
else
return $request_url."/requests/".$request_id."/status";
}
function getPostParamData($postParamData, $key) {
if(array_key_exists($key, $postParamData))
return $postParamData[$key];
switch($key) {
case "body":
case "sender_address":
case "sender_name":
case "templateSid":
case "title":
return "";
break;
case "parameters":
case "recipients":
return array_map();
break;
case "individual":
return false;
break;
}
}
function makePostParam($postParamData) {
global $sender_name, $sender_address, $sender_bcc;
$postParam = array(
"advertising" => false, // 광고메일여부 default: false
"attachFileIds" => array_map(), // 첨부파일ID
"body" => getPostParamData($postParamData, "body"), // Email 본문
"individual" => getPostParamData($postParamData, "individual"),
// 개인발송여부 (개인발송 시 참조인, 숨은참조 무시됨) default: true
"parameters" => getPostParamData($postParamData, "parameters"),
// 치환 파라미터 (전체 수신자에게 적용), ‘치환 ID’ 를 key로, ‘치환 ID에 맵핑되는 값’ 을 value로 가지는 Map 형태의 Object
"recipients" => getPostParamData($postParamData, "recipients"),
"referencesHeader" => "",
// 특정 메일을 모아서 보기 위해 네이버 메일에서 지원하는 기능, 해당 필드에 동일한 값을 입력한 메일들을 모아서 볼 수 있다. (다음의 형태가 되어야 함 : <unique_id@domain.com> )
"reservationUtc" => "",
// 예약 발송 일시 ( 1970년 1월 1일 00:00:00 협정 세계시(UTC) 부터의 경과 시간을 1/1000초로 환산한 정수 ), reservationDateTime 값보다 이 값이 우선 적용된다.
"reservationDateTime" => "",
// 다음과 같은 형태의 예약 발송 일시 (yyyy-MM-dd HH:mm UTC+09:00), reservationUtc 값이 우선한다.
"sender_address" => getPostParamData($postParamData, "sender_address"), // 발송자 Email 주소
"sender_name" => getPostParamData($postParamData, "sender_name"), // 발송자 이름
"temprequest_id" => "", // 첨부파일 등록 시 사용한 임시 요청 ID
"templateSid" => getPostParamData($postParamData, "templateSid"), // 템플릿 ID
"title" => getPostParamData($postParamData, "title"), // Mail 제목
);
return json_encode($postParam);
}
function getTimeStamp() {
$timestamp = "";
$microtime = "";
list($microtime,$timestamp) = explode(' ',microtime());
$timestamp = $timestamp.substr($microtime, 2, 3);
return $timestamp;
}
function makeSignature($secret_key, $method, $uri, $timestamp, $access_key) {
$space = " ";
$newLine = "\n";
$hmac = $method.$space.$uri.$newLine.$timestamp.$newLine.$access_key;
$signature = base64_encode(hash_hmac('sha256', $hmac, $secret_key,true));
// set_data("signature", $signature);
return $signature;
}
function cUrl($postParam, $timestamp, $signature) {
global $host_name_url, $request_url, $access_key, $secret_key, $api_key, $method, $request_id;
$is_post = false;
if($method == "POST")
$is_post = true;
try {
$url = $host_name_url.$request_url;
// echo $url."\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $is_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if($method == "POST") {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParam);
}
$headers = array();
$headers[] = "X-NCP-APIGW-TIMESTAMP: " .$timestamp;
$headers[] = "X-NCP-IAM-ACCESS-KEY: " .$access_key;
$headers[] = "X-NCP-APIGW-SIGNATURE-V2: " .$signature;
$headers[] = "content-type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// echo "status_code:".$status_code."\n";
curl_close ($ch);
if($status_code == 200) {
set_data("sendmail_respond", $response);
} else if($status_code == 201) {
set_data("sendmail_succeed", $response);
} else {
set_data("sendmail_error", $response);
}
} catch(Exception $E) {
set_data("sendmail_error", $E->lastResponse);
}
}
/*
////////////////////////
// CentOS postfix e-mail sender (canceled)
////////////////////////
function makeMailHeader($from, $bcc) { function makeMailHeader($from, $bcc) {
global $new_line; global $new_line;
@@ -222,5 +478,6 @@ function messageUpgradeDone($maestro_name, $account_type, $upgrade_account_type)
.$new_line .$new_line
."감사합니다.".$new_line; ."감사합니다.".$new_line;
} }
*/
?> ?>
+6 -46
View File
@@ -12,68 +12,28 @@ $type = $_POST["type"];
$maestro_id = $_POST["maestro_id"]; $maestro_id = $_POST["maestro_id"];
$maestro_name = $_POST["maestro_name"]; $maestro_name = $_POST["maestro_name"];
$password = $_POST["password"]; $password = $_POST["password"];
$email = $_POST["email"]; $maestro_email = $_POST["email"];
$account_type = $_POST["account_type"]; $account_type = $_POST["account_type"];
$upgrade_account_type = $_POST["upgrade_account_type"]; $upgrade_account_type = $_POST["upgrade_account_type"];
// set_data("maestro_name", $maestro_name);
// set_data("email", $email);
// set_data("account_type", $account_type);
// set_data("upgrade_account_type", $upgrade_account_type);
if($type == "sendMailRegisterBankInfo") { if($type == "sendMailRegisterBankInfo") {
sendMailRegisterBankInfo($maestro_name, $email, $account_type);
/* sendMailRegisterBankInfo($maestro_name, $maestro_email, $account_type);
// subject registered
$subject = subjectRegisterBankInfo($maestro_name);
set_data("subject", $subject);
// messgae registered
$messgae = messageRegisterBankInfo($maestro_name, $account_type, $upgrade_account_type);
set_data("messgae", $messgae);
*/
} else if($type == "sendMailRegistered") { } else if($type == "sendMailRegistered") {
sendMailRegisterDone($maestro_name, $email, $account_type);
/* sendMailRegisterDone($maestro_name, $maestro_email);
// maestro data
$maestro_data = get_maestro_data($maestro_id);
set_data("maestroData", $maestro_data);
// subject registered
$subject = subjectRegisterDone($maestro_data["name"]);
set_data("subject", $subject);
// messgae registered
$messgae = messageRegisterDone($maestro_data["name"]);
set_data("messgae", $messgae);
*/
} else if($type == "sendMailUpgradeBankInfo") { } else if($type == "sendMailUpgradeBankInfo") {
sendMailUpgradeBankInfo($maestro_name, $email, $account_type, $upgrade_account_type);
/* sendMailUpgradeBankInfo($maestro_name, $maestro_email, $account_type, $upgrade_account_type);
// subject registered
$subject = subjectUpgradeBankInfo($maestro_name);
set_data("subject", $subject);
// messgae registered
$messgae = messageUpgradeBankInfo($maestro_name, $account_type, $upgrade_account_type);
set_data("messgae", $messgae);
*/
} else if($type == "sendMailUpgradeDone") { } else if($type == "sendMailUpgradeDone") {
sendMailUpgradeDone($maestro_name, $email, $account_type, $upgrade_account_type);
/* sendMailUpgradeDone($maestro_name, $maestro_email, $account_type, $upgrade_account_type);
// subject upgrade bank info
$subject = subjectUpgradeDone($maestro_name);
set_data("subject", $subject);
// messgae upgrade bank info
$messgae = messageUpgradeDone($maestro_name, $account_type, $upgrade_account_type);
set_data("messgae", $messgae);
*/
} else if($type == "get_maestro_data") { } else if($type == "get_maestro_data") {
// maestro data // maestro data
$maestro_data = get_maestro_data($maestro_id); $maestro_data = get_maestro_data($maestro_id);