メールの高速大量送信サービス「SendGrid」のREST API経由でメールの送信を行うAppleScriptです。
試用にあたっては、SendGridにサインアップして試用アカウントを取得してください。この試用アカウントで実運用を行わないことがアカウト取得の前提条件となっています(かなり念押しされました、、、)。
アクセストークンを取得したら、本リスト内のretAccessTokenハンドラ内にペーストしてください。本リストを掲載状態のままAccessTokenが伏字の状態で実行しても、メール送信は行えません。
気になるメールの転送速度ですが、本AppleScriptではだいたい1通あたり0.1秒程度です。Mail.app経由で一般的なプロバイダのメールサーバーを経由して送るよりは10倍程度高速ですが、APIの呼び方をチューニングすることで、さらにこの10倍ぐらいの高速送信は行えるようになります。
AppleScript名:POST method REST API__Sendgrid_Send v1 |
— Created 2017-05-23 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set a1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() repeat with i from 1 to 10 set aSubject to "ぴよまるさんからの新しいおしらせ" & i as string set aBody to "ぴよまるさんこんばんわ" & i as string set toAddress to "maro@piyocast.com" set fromAddress to "piyomarusoft@mac.com" set aRes to sendMailViaSendGrid(aSubject, aBody, toAddress, fromAddress) of me end repeat set b1Dat to current application’s NSDate’s timeIntervalSinceReferenceDate() set c1Dat to (b1Dat – a1Dat) as real return c1Dat on sendMailViaSendGrid(aSubject, aBody, toAddress, fromAddress) set accessToken to "Bearer " & retAccessToken() —Access Token set reqURLStr to "https://api.sendgrid.com/v3/mail/send" set aRec to {personalizations:{{|to|:{{email:toAddress}}, subject:aSubject}}, |from|:{email:fromAddress, |name|:"ぴよ まるお"}, content:{{type:"text/plain", value:aBody}}} set aRes to callRestPOSTAPIAndParseResults(reqURLStr, accessToken, aRec) of me set aRESTres to json of aRes set aRESCode to (responseCode of aRes) as integer return (aRESCode = 202) –リクエスト成立ならtrueが返る( ) end sendMailViaSendGrid –POST methodのREST APIを呼ぶ on callRestPOSTAPIAndParseResults(aURL, anAPIkey, aRec) set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL) aRequest’s setHTTPMethod:"POST" aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData) aRequest’s setHTTPShouldHandleCookies:false aRequest’s setTimeoutInterval:60 aRequest’s setValue:anAPIkey forHTTPHeaderField:"Authorization" aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type" aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding" set dataJson to current application’s NSJSONSerialization’s dataWithJSONObject:aRec options:0 |error|:(missing value) aRequest’s setHTTPBody:dataJson –CALL REST API set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value) –Parse Results set resList to aRes as list set bRes to contents of (first item of resList) set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding) set jsonString to current application’s NSString’s stringWithString:resStr set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) –Get Response Code set dRes to contents of second item of resList set resCode to (dRes’s statusCode()) as integer –Get Response Header set resHeaders to (dRes’s allHeaderFields()) as record return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders} end callRestPOSTAPIAndParseResults on retAccessToken() return "XX.XxX_XXxxXXxxxxxxXxXXXx.xXXXxXXXxXXXxXXxXxxxXXXxXXxxxXXXXxxxXxXxXXx" end retAccessToken |
More from my site
(Visited 81 times, 1 visits today)