1. 程式人生 > >Python-sendgrid郵箱庫的使用

Python-sendgrid郵箱庫的使用

Python中sendgrid庫使用

#幫助文件https://github.com/sendgrid/sendgrid-python

https://sendgrid.com/docs/ui/account-and-settings/

https://sendgrid.api-docs.io/v3.0/mail-send/v3-mail-send

 

1、註冊

https://sendgrid.com/註冊賬號,選擇語言型別,建立api-keys。

 

2、開發環境keys值設定

把建立的keys值放進去。開發環境的shell

Mac

Update the development environment with your 

SENDGRID_API_KEY (more info here), for example:

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env

echo "sendgrid.env" >> .gitignore

source ./sendgrid.env

Sendgrid also supports local environment file .env. Copy or rename .env_sample into .env and update 

SENDGRID_API_KEYwith your key.

Windows

Temporarily set the environment variable(accesible only during the current cli session):

set SENDGRID_API_KEY=YOUR_API_KEY

Permanently set the environment variable(accessible in all subsequent cli sessions):

setx SENDGRID_API_KEY "YOUR_API_KEY"

Install Package

pip install sendgrid

 

3、測試程式碼

# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import sendgrid
import os
from sendgrid.helpers.mail import *


def verification():
    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    from_email = Email("[email protected]")
    to_email = Email("[email protected]")
    subject = "Sending with SendGrid is Fun"
    content = Content("text/plain", "and easy to do anywhere, even with Python")
    mail = Mail(from_email, subject, to_email, content)
    response = sg.client.mail.send.post(request_body=mail.get())
    print(response.status_code)
    print(response.body)
    print(response.headers)


if __name__ == '__main__':
    verification()

 

# import sendgrid
# import os
# sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
# data = {
#   "personalizations": [
#     {
#       "to": [
#         {
#           "email": "[email protected]"
#         }
#       ],
#       "subject": "Sending with SendGrid is Fun"
#     }
#   ],
#   "from": {
#     "email": "[email protected]"
#   },
#   "content": [
#     {
#       "type": "text/plain",
#       "value": "and easy to do anywhere, even with Python"
#     }
#   ]
# }
# response = sg.client.mail.send.post(request_body=data)
# print(response.status_code)
# print(response.body)
# print(response.headers)

 

 

4、Plan

Plan分為三類:

1)、email api

2)、marketing campaigns(營銷活動)

3)、email api 和marketing campaigns(兩者一起)

5、SMTP

簡單郵件傳輸協議(Simple Mail Transfer Protocol,SMTP) 是在Internet傳輸email的事實標準

 

6、SendGridAPIClient引數情況

繼承自object。

 

初始化引數:

apikey: sendgrid需要使用的key。如果未提供的話,會在環境變數中查詢。

api_key: sendgrid需要使用的key,提供後面的版本相容,5.3,後就會被棄用。

impersonate_subuser:模仿subuser賬號,底層客戶端。

 

Host:api呼叫的基本的URL。

 

Opts:棄用引數的排程員。後面相容的版本用path引數,在6以後的版本會移除。

 

7、mail引數

(1)from_email   來自郵箱

(2)subject  主題

(3)to_email   給誰傳送

(4)content   正文

(5)get方法

 

方法裡面有多個條件。

字典形式儲存郵箱,正文和主題等。

 

Keys為:personalizations,個性化(裡面是元組,元組裡面是字典,裡面是主題,給誰傳送訊息),from(來自哪個郵箱),content(正文訊息)。

 

8、email引數

(1)email 郵箱

(2)name 名字

 

 https://sendgrid.api-docs.io/v3.0/mail-send/v3-mail-send

 

9、headers

Authorization ,string ,1 ,validations,required

 

10、request body

11、response

400、401、413、 202