Created
November 4, 2015 18:21
-
-
Save sakadon/769abb6c1b4f57dd2e70 to your computer and use it in GitHub Desktop.
WordPressを利用してslackからメールを送信する ref: http://qiita.com/sakadon/items/945cfc1f794dd796ad99
This file contains 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 | |
/* | |
Template Name: mail from slack | |
これは、WordPressテーマ上で動作させることを前提としていて、 | |
専用に1こ固定ページをつくり、このテンプレートを読み込ませ、 | |
その固定ページのURIでslack apiと連携させ動作させる | |
あと、tokenは slack api outbound webhooksで設定したのをいれる | |
送信先アドレスで、headersにいれてあるfromのアドレスを許可するように | |
↑Google groupsなど | |
*/ | |
if( isset($_POST['token']) && $_POST['token'] == '####_API_TOKEN_####' ) { | |
// mailfromslackならばこっち | |
$text = $_POST['text']; | |
$text_arr = preg_split('/[\s]+/', $text, 4); | |
$sendUser = htmlspecialchars($_POST['user_name']); | |
$fromCh = htmlspecialchars($_POST['channel_name']); | |
$addTo = '送信先メールアドレス'; | |
$setSubject = '[slack #'.$fromCh.'] '.$text_arr[1]; | |
$setText = $sendUser.'が slack #'.$fromCh." から送信 \n\n".$text_arr[2]."\n\n"."-- \n".'slackより自動送信'; | |
$headers[] = 'From: '.$sendUser.' <tpl_mailfromslack.php@example.com>'; | |
$headers[] = 'Cc: あればなんかメールアドレス'; | |
$return = wp_mail( $addTo, $setSubject, $setText, $headers ); | |
if($return){ | |
echo json_encode(array('text' => '送信実行!ちゃんと送れてるか確認してね')); | |
} else { | |
echo json_encode(array('text' => '送信失敗!'."\n".'この失敗は wp_mail() 関数による判断です。')); | |
} | |
} else { | |
// mailfromslackじゃなければ404 | |
wp_safe_redirect( home_url() ); | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment