※こちらは旧サイトです(新サイトはこちら

Sendgridのv3 Mail Send APIでメール送信(Python)

2017-12-05 14:02:11

APIキー認証でセキュリティを強化しよう

SendgridでAPIが提供されてたので使ってみたメモ

API Key発行手順

実装方法

公式からは、sendgridパッケージが提供されててこちらの利用を推奨との事ですが、Githubにあったサンプルがそのままでは動かなかったので、今回は時間の都合上、requestsで単純なPOSTを投げる方で実装しました。以下、その手順

$ python --version
Python 3.6.2
$ pip install requests

$ pip freeze | grep requests
requests==2.18.4
$ vim send.py

import requests

APIKEY="[API Key]"

headers = {
        "Authorization": "Bearer "+APIKEY
        }
payload = {
        "to": "to@example.com",
        "from": "from@example.com",
        "subject": "タイトル",
        "html": "本文"
        }

r = requests.post("https://api.sendgrid.com/api/mail.send.json", headers=headers, params=payload)
$ python send.py