Created
July 27, 2019 00:18
-
-
Save kenichimiki/95670957bb1b9a07b8ea4c5a09529298 to your computer and use it in GitHub Desktop.
PHP script for Inbound Email Parse Webhook of SendGrid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/************************************************************************* | |
* sendgrid-api (MAIN) | |
* Home Tools for private. Using IFTTT and Google Home etc | |
* | |
* PHP 5 or later | |
* | |
* @category Home IoT | |
* @author Miki | |
* @url https://www.miki-ie.com/ | |
* @copyright 2019 (c) MIKI-IE All rights Reserved. | |
* @license https://opensource.org/licenses/mit-license.html MIT License | |
* @version 1.0 | |
*************************************************************************/ | |
//ログのファイル名 | |
define("SENDGRID_API_LOG_NAME","sendgrid"); | |
//DB | |
define("DB_HOST","@IP_Adress@:@Port@"); | |
define("DB_USER","@User@"); | |
define("DB_PASS","@Password@"); | |
define("DB_DBNAME","@DB_Name@"); | |
define("DB_TABLENAME1","@TableName1@"); | |
define("DB_TABLENAME2","@TableName2@"); | |
function addDBRecord($table, $datetime, $value1) { | |
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DBNAME); | |
if (mysqli_connect_errno()) { | |
logger("Connect failed: ".mysqli_connect_error(),"ERROR"); | |
exit(); | |
} | |
if (!$mysqli->set_charset("utf8")) { | |
logger("Error loading character set utf8: ".$mysqli->error,"ERROR"); | |
exit(); | |
} | |
switch ($table) { | |
case DB_TABLENAME1: | |
// SQL(INSERT)を作成 | |
$sql = "INSERT INTO $table ( | |
DATETIME, COUNT | |
) VALUES ( | |
'$datetime', $value1 | |
)"; | |
break; | |
//addDBRecord(DB_TABLENAME2, $datetime_text, $state_text); | |
case DB_TABLENAME2: | |
// SQL(INSERT)を作成 | |
$str = mb_convert_encoding($value1, "UTF-8"); | |
$sql = "INSERT INTO $table ( | |
DATETIME2, STATE | |
) VALUES ( | |
'$datetime', N'$str' | |
)"; | |
break; | |
default: | |
logger("Internal DB Tabel Name Error. table:{$table}","ERROR"); | |
} | |
if (!$mysqli->query($sql)) { | |
logger("SQL query error Errormessage: ".$mysqli->error,"ERROR"); | |
} | |
$mysqli->close(); | |
} | |
function logger($text, $level) { | |
$datetime = date('Y-m-d H:i:s'); | |
$date = date('Ym'); | |
$file_name = __DIR__ . "/log/".SENDGRID_API_LOG_NAME."-{$date}.log"; | |
$text = "{$datetime} [{$level}] {$text}" . PHP_EOL; | |
echo $text; | |
if(!(file_exists($file_name))){ | |
touch($file_name); | |
chmod($file_name, 0777); | |
} | |
return error_log(print_r($text, TRUE), 3, $file_name); | |
} | |
$post_from = $_POST['from']; | |
$post_charsets = $_POST['charsets']; | |
$post_charsets_array = json_decode($post_charsets, true); | |
$post_charsets_all = print_r($post_charsets_array, true); | |
$post_encode_subject = mb_convert_encoding($_POST['subject'], "UTF-8", $post_charsets_array["subject"]); | |
$post_encode_subject = preg_replace('/[\x00-\x1F\x7F]/', '', $post_encode_subject); | |
$post_encode_text = mb_convert_encoding($_POST['text'], "UTF-8", $post_charsets_array["text"]); | |
$post_encode_text = preg_replace('/[\x00-\x1F\x7F]/', '', $post_encode_text); | |
logger("mb_convert_encoding, post_subject : {$post_encode_subject}","DEBUG"); | |
logger("mb_convert_encoding, post_text : {$post_encode_text}","DEBUG"); | |
$text_substr = mb_substr($post_text, 0, 15); | |
logger("Start sendgrid: key={$key} , from={$post_from} , subject={$post_subject} , text={$text_substr}","INFO"); | |
if(isset($_POST['from'])) { | |
switch ($post_from) { | |
case '[email protected]': //メール受信内容に合わせて個別処理。以下はメール本文から文字列切り出しを実施 | |
$start = mb_strpos($post_text, "時刻:"); | |
$datetime_text = mb_substr($post_text, $start + 4, 19,"utf-8"); | |
logger("Start CAM-A7DE datetime : {$datetime_text}","INFO"); | |
addDBRecord(DB_TABLENAME1, $datetime_text, 1); | |
break; | |
case '[email protected]': //メール受信内容に合わせて個別処理。以下はメール本文から文字列切り出しを実施 | |
$datetime_text = mb_substr($post_text, 0, 16,"utf-8"); | |
$temp_text = mb_substr($post_text, 18); | |
$end_point = mb_strpos($temp_text, "。"); | |
$state_text = mb_substr($temp_text, 0, $end_point,"utf-8"); | |
logger("end_point : {$end_point} ,state : {$state_text}","DEBUG"); | |
logger("Start CSP-Security datetime : {$datetime_text} state : {$state_text}","INFO"); | |
addDBRecord(DB_TABLENAME2, $datetime_text, $state_text); | |
break; | |
default: | |
logger("This is private API. (in Default)","ERROR"); | |
} | |
}else{ | |
logger("This is private API. (in else)","ERROR"); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment