2016-08-17 15 views
1

私は、python EmailMultiAlternativesを介してsendgrid smtp apiからマーケティング電子メールを送信しています。特定の電子メールを配信不能としてマークするには、そこから直接バウンスを処理する方法を知りたいのですが。Pythonで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 = '[email protected]' 
    to = '[email protected]' 
    msg = EmailMultiAlternatives(subject, text_content, from_email, [to], connection=connection) 
    msg.attach_alternative(html_content, "text/html") 
    msg.send() 
    connection.close() 

msg.send()後にここに応答を取得することが可能ですまたはいくつかの他の方法があります。

答えて

0

ブロックやバウンスのようなイベントに応答する最善の方法は、event webhookを実装することです。

bounces endpointでデータをポーリングすることもできます。

0

ので、解決策を探しているかもしれない人のために:

私は(私はメールを送信しています、それを通してメールIDの)受信トレイを掻き取るimaplibのPythonパッケージを使用していますへのバウンスメールや苦情のための日常それらの不要な電子メールを入手してください。

def bounce(): 

    M = imaplib.IMAP4_SSL('imap.zoho.com') 
    M.login('[email protected]', 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\.\-+_][email protected][a-z0-9\.\-+_]+\.[a-z]+", str(w), re.I) 

だから毎日または毎時間コードを実行すると役立ちます。

関連する問題