LoginSignup
0
0

More than 1 year has passed since last update.

SendGridの始め方

Posted at

SendGridの始め方

AzureでSMTPが送信できなかったので代替手段をとして調査をしたのでメモメモ

前提

Windows環境
Python3

入口

いくつかある。

  1. 構造計画研究所
  2. AzureのMarketPlace
  3. Twilio SendGrid

無料分が一番多い構造計画研究所さんを基本に記述する

登録

下から登録
いろいろと記述箇所があるがすべて埋める
登録が完了すると代替翌日には完了メールが届く
https://sendgrid.kke.co.jp/

APIの登録

最初にすべきこと

  1. Setteings→API Keys
    image.png
  2. Create API Key
    image.png
  3. API Key Nameに適当に入力してFull Access
    image.png
  4. Kyeが表示される。めちゃくちゃ重要。要メモ

環境用意

環境変数を設定

setx SENDGRID_API_KEY "Sgから始まるKey"
要再起動

サンプルソース

import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
from_email='from_email@example.com',
to_emails='to@example.com',
subject='Sending with Twilio SendGrid is Fun',
html_content='and easy to do anywhere, even with Python')
try:
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(str(e))

迷惑メールになる場合

DKIMの設定をする
設定を進めてDNSを登録
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0