ไลบรารีนี้ช่วยให้คุณใช้ SendGrid Web API v3 ผ่าน Python ได้อย่างรวดเร็วและง่ายดาย
เวอร์ชัน 3.X.X+ ของไลบรารีนี้ให้การสนับสนุนอย่างเต็มที่สำหรับจุดสิ้นสุด SendGrid Web API v3 ทั้งหมด รวมถึง v3 ใหม่ /mail/send
ไลบรารีนี้แสดงถึงจุดเริ่มต้นของเส้นทางใหม่สำหรับ SendGrid เราต้องการให้ห้องสมุดนี้ขับเคลื่อนโดยชุมชนและ SendGrid เป็นผู้นำ เราต้องการความช่วยเหลือจากคุณเพื่อให้บรรลุเป้าหมายนี้ เพื่อช่วยให้แน่ใจว่าเรากำลังสร้างสิ่งที่ถูกต้องในลำดับที่ถูกต้อง เราขอให้คุณสร้างปัญหาและดึงคำขอ หรือเพียงโหวตเห็นด้วยหรือแสดงความคิดเห็นเกี่ยวกับปัญหาที่มีอยู่หรือดึงคำขอ
หากคุณต้องการความช่วยเหลือในการใช้ SendGrid โปรดตรวจสอบศูนย์ช่วยเหลือ Twilio SendGrid Support
โปรดเรียกดูส่วนที่เหลือของ README นี้เพื่อดูรายละเอียดเพิ่มเติม
อัปเดตสภาพแวดล้อมการพัฒนาด้วย SENDGRID_API_KEY ของคุณ (ข้อมูลเพิ่มเติมที่นี่) ตัวอย่างเช่น:
echo " export SENDGRID_API_KEY='YOUR_API_KEY' " > sendgrid.env
echo " sendgrid.env " >> .gitignore
source ./sendgrid.env
SendGrid ยังรองรับไฟล์สภาพแวดล้อมในเครื่อง .env
คัดลอกหรือเปลี่ยนชื่อ .env_sample
เป็น .env
และอัปเดต SENDGRID_API_KEY ด้วยคีย์ของคุณ
ตั้งค่าตัวแปรสภาพแวดล้อมชั่วคราว (เข้าถึงได้เฉพาะในช่วงเซสชัน cli ปัจจุบันเท่านั้น):
set SENDGRID_API_KEY=YOUR_API_KEY
ตั้งค่าตัวแปรสภาพแวดล้อมอย่างถาวร (สามารถเข้าถึงได้ในเซสชัน cli ที่ตามมาทั้งหมด):
setx SENDGRID_API_KEY " YOUR_API_KEY "
pip install sendgrid
ต่อไปนี้เป็นรหัสขั้นต่ำที่จำเป็นในการส่งอีเมลด้วย /mail/send Helper (นี่คือตัวอย่างแบบเต็ม):
import sendgrid
import os
from sendgrid . helpers . mail import *
sg = sendgrid . SendGridAPIClient ( api_key = os . environ . get ( 'SENDGRID_API_KEY' ))
from_email = Email ( "[email protected]" )
to_email = To ( "[email protected]" )
subject = "Sending with SendGrid is Fun"
content = Content ( "text/plain" , "and easy to do anywhere, even with Python" )
mail = Mail ( from_email , to_email , subject , content )
response = sg . client . mail . send . post ( request_body = mail . get ())
print ( response . status_code )
print ( response . body )
print ( response . headers )
ตัวสร้าง Mail
สร้างวัตถุการตั้งค่าส่วนบุคคลสำหรับคุณ นี่คือตัวอย่างวิธีการเพิ่ม
ต่อไปนี้เป็นรหัสขั้นต่ำที่จำเป็นในการส่งอีเมลโดยไม่มี /mail/send Helper (นี่คือตัวอย่างแบบเต็ม):
import sendgrid
import os
sg = sendgrid . SendGridAPIClient ( api_key = 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 )
import sendgrid
import os
sg = sendgrid . SendGridAPIClient ( api_key = os . environ . get ( 'SENDGRID_API_KEY' ))
response = sg . client . suppression . bounces . get ()
print ( response . status_code )
print ( response . body )
print ( response . headers )
import sendgrid
import os
sg = sendgrid . SendGridAPIClient ( api_key = os . environ . get ( 'SENDGRID_API_KEY' ))
response = sg . client . _ ( "suppression/bounces" ). get ()
print ( response . status_code )
print ( response . body )
print ( response . headers )
โปรดดูผู้ช่วยของเราในการใช้ Inbound Parse webhook ของเรา
ตัวอย่างกรณีการใช้งาน API ทั่วไป เช่น วิธีส่งอีเมลด้วยเทมเพลตธุรกรรม
การอัปเดตทั้งหมดของไลบรารีนี้ได้รับการบันทึกไว้ใน CHANGELOG และการเผยแพร่ของเรา
เราสนับสนุนให้สนับสนุนห้องสมุดของเรา (คุณอาจได้คะแนนดีๆ ด้วย) โปรดดูรายละเอียดในคู่มือการมีส่วนร่วมของเรา
ลิงค์ด่วน:
โปรดดูคู่มือการแก้ไขปัญหาของเราสำหรับปัญหาห้องสมุดทั่วไป
sendgrid-python ได้รับการดูแลและให้ทุนโดย Twilio SendGrid, Inc. ชื่อและโลโก้สำหรับ sendgrid-python เป็นเครื่องหมายการค้าของ Twilio SendGrid, Inc.
หากคุณต้องการความช่วยเหลือ โปรดตรวจสอบศูนย์ช่วยเหลือ Twilio SendGrid Support
ใบอนุญาต MIT (MIT)