Add: sending email to unregistered maestro in PHP

This commit is contained in:
2019-04-25 21:15:26 +09:00
parent d8e9a2a12b
commit f540db4fba
6 changed files with 279 additions and 16 deletions
+7
View File
@@ -0,0 +1,7 @@
#!/usr/bin/php
<?php
include("test_ncloud.php");
# php test_ncloud.php 삼화초 jisangs@daum.net 2019-04-22
# php test_ncloud.php name email date
?>
+121
View File
@@ -0,0 +1,121 @@
<?php
if($argc == 1) {
echo "no maestro name";
exit(0);
} else if($argc == 2) {
echo "no maestro email";
exit(0);
} else if($argc == 3) {
echo "no register date";
exit(0);
}
$maestro_name = $argv[1];
$maestro_email = $argv[2];
$register_date = $argv[3];
$hostNameUrl = "https://mail.apigw.ntruss.com"; // 호스트 URL
$requestUrl= "/api/v1/mails"; // 요청 URI
$accessKey = "qNXWk443wdnDXrkPvoqr"; // 네이버 클라우드 플랫폼 회원에게 발급되는 개인 인증키
$secretKey = "umWxQXulhNSya2CkMNqqVJnSRfwRaN8BjMUX0skV"; // 2차 인증을 위해 서비스마다 할당되는 service secret
$apiKey = "eWfapJfbR5W9NXPEsdXGzlJsShEV30DfxIV0d51J"; // 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(), // 첨부파일ID
"body" => "", // Email 본문
"individual" => false, // 개인발송여부 (개인발송 시 참조인, 숨은참조 무시됨) default: true
"parameters" => array(
"maestro_name" => $maestro_name,
"register_date" => $register_date,
), // 치환 파라미터 (수신자별로 적용), ‘치환 ID’ 를 key로, ‘치환 ID에 맵핑되는 값’ 을 value로 가지는 Map 형태의 Object
"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
),
),
"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" => "support+chocomae@jinaju.com", // 발송자 Email 주소
"senderName" => "초코마에", // 발송자 이름
"tempRequestId" => "", // 첨부파일 등록 시 사용한 임시 요청 ID
"templateSid" => "337", // 템플릿 ID
"title" => "", // Mail 제목
);
$postParam = json_encode($postParam);
echo "json:\n";
echo $postParam;
echo "\n";
$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;
}
?>