在 Python 中处理 Sendgrid smtp API 中的反弹
Handling Bounce in Sendgrid smtp API in Python
我正在通过 python EmailMultiAlternatives 通过 sendgrid smtp api 发送营销电子邮件。我想知道如何直接从那里处理退回邮件以将特定电子邮件标记为无法送达。
代码片段是:
def send1():
text_content = 'Hi this is the text version'
connection = get_connection(host=EMAIL_HOST,
port=EMAIL_PORT,
username=EMAIL_HOST_USER,
password=EMAIL_HOST_PASSWORD,
use_tls=EMAIL_USE_TLS)
connection.open()
subject = 'Inviting {0} to join the Business Network of SMEs'.format('surya')
html_content = template.format('Surya')
from_email = 'sp@abc.com'
to = 'abc@gmail.com'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to], connection=connection)
msg.attach_alternative(html_content, "text/html")
msg.send()
connection.close()
有没有可能只有在msg.send()
之后才能在这里得到响应,还是有其他方式。
响应屏蔽和弹跳等事件的最佳方式是实施 event webhook。
您还可以通过 bounces endpoint.
轮询数据
因此,对于那些可能正在寻找解决方案的人:
我每天都在使用 imaplib python 包来抓取收件箱(我发送电子邮件所用的邮件 ID),以获取退回的电子邮件和投诉以获取那些不需要的电子邮件。
def bounce():
M = imaplib.IMAP4_SSL('imap.zoho.com')
M.login('email@emailcom', password)
M.select()
line = '(HEADER Subject "Bounce")'
typ, data = M.uid('search', line)
if typ != 'OK':
return
print(len(data[0].split()))
for i in data[0].split():
result, data = M.uid('fetch', i, '(RFC822)')
raw_email = data[0][1].decode('utf-8', 'ignore')
emg = email.message_from_string(raw_email)
w = get_first_text_block(emg)
emails = re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", str(w), re.I)
因此,每天或每小时执行一次代码会对您有所帮助。
您也可以使用sendgrid。下面是用6.4.1版本写的。
import sendgrid
sg = sendgrid.SendGridAPIClient(api_key="<your_key>")
payload = {
"limit": 100,
"offset": 0,
}
response = sg.client.suppression.bounces.get(query_params=payload)
query_params 可以省略以接受默认值,还有 start_time 和 end_time 可用(需要是整数 Unix 时间)。
我正在通过 python EmailMultiAlternatives 通过 sendgrid smtp api 发送营销电子邮件。我想知道如何直接从那里处理退回邮件以将特定电子邮件标记为无法送达。
代码片段是:
def send1():
text_content = 'Hi this is the text version'
connection = get_connection(host=EMAIL_HOST,
port=EMAIL_PORT,
username=EMAIL_HOST_USER,
password=EMAIL_HOST_PASSWORD,
use_tls=EMAIL_USE_TLS)
connection.open()
subject = 'Inviting {0} to join the Business Network of SMEs'.format('surya')
html_content = template.format('Surya')
from_email = 'sp@abc.com'
to = 'abc@gmail.com'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to], connection=connection)
msg.attach_alternative(html_content, "text/html")
msg.send()
connection.close()
有没有可能只有在msg.send()
之后才能在这里得到响应,还是有其他方式。
响应屏蔽和弹跳等事件的最佳方式是实施 event webhook。
您还可以通过 bounces endpoint.
轮询数据因此,对于那些可能正在寻找解决方案的人:
我每天都在使用 imaplib python 包来抓取收件箱(我发送电子邮件所用的邮件 ID),以获取退回的电子邮件和投诉以获取那些不需要的电子邮件。
def bounce():
M = imaplib.IMAP4_SSL('imap.zoho.com')
M.login('email@emailcom', password)
M.select()
line = '(HEADER Subject "Bounce")'
typ, data = M.uid('search', line)
if typ != 'OK':
return
print(len(data[0].split()))
for i in data[0].split():
result, data = M.uid('fetch', i, '(RFC822)')
raw_email = data[0][1].decode('utf-8', 'ignore')
emg = email.message_from_string(raw_email)
w = get_first_text_block(emg)
emails = re.findall(r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", str(w), re.I)
因此,每天或每小时执行一次代码会对您有所帮助。
您也可以使用sendgrid。下面是用6.4.1版本写的。
import sendgrid
sg = sendgrid.SendGridAPIClient(api_key="<your_key>")
payload = {
"limit": 100,
"offset": 0,
}
response = sg.client.suppression.bounces.get(query_params=payload)
query_params 可以省略以接受默认值,还有 start_time 和 end_time 可用(需要是整数 Unix 时间)。