Feedback loop for mailing Returnpath, Hotmail

Ok, today I will check if our feedback loop is working correctly.

So that bounces are unsubscribed correctly, abuse emails are unsubscribed etc..

We have had our service for more than 10 years. All our emails double opt in.

It gets more complicated now that Microsoft has decided to turn abandoned Hotmail accounts into spam traps. Microsoft doesn't say how long the account must be abandoned before they turn it into a spam trap, but they are a bit more forgiving when companies send email to abandoned accounts than they are for companies emailing a honey pot address.

After a while if the hotmail stops working or stays full for a long time then Microsoft converts it into a spamtrap. Search Microsoft documentation about this issue, spam traps and Microsoft methods.

The best way to avoid emailing abandoned Hotmail accounts is list hygiene. I will Make sure to remove bounced addresses from our mailing lists, be careful with sending email campaigns to a list that haven't clicked for more than six months.

Anyway that's solved in no time and automated. IP addresses are also green and traffic is not marked as spam or blocked by Microsoft. But we have some trap hits via abandoned Hotmail accounts.

When such a thing happens, we see and govern ourselves with Microsoft. So we have to intervene here, our IP is fixed, we manage our reputation ourselves and this is excellent.

So if we want to reduce trap hits via abandoned Hotmail accounts. We have to use feedback from Microsoft and unsubscribe automatically all emails that we have received from MS services. 

An example of code that could be used.

import imaplib
import re
import requests
from datetime import datetime, date
import sys, traceback, os

from time import sleep

from spontaneousmail import utils
from import BaseCommand
from spontaneousmail.models import SpontaneousProfile
import mailbox

class Command(BaseCommand):
verbose = True
timeout = 5
count = 0
mailbox = "data/Takeout/E-mail/Unsubscribe.mbox"

def handle(self, *args, **options):
Unsubscribe via mailbox
contacts = self.read_contacts()
count = 1
for contact in contacts:
count = count + 1
utils.unsubscribe(contact['email'], "Unsubscribe_mailbox")

def parse_from_email(self, text, pattern):
pattern = re.compile(pattern)
result = re.findall(pattern, text)
for item in result:
#if (item.find("postmaster")):
# import pdb;pdb.set_trace()
# print(item)
if ("" in item) or ("" in item) or ("postmaster" in item) or ("vindazo" in item):
if len(result) > 0:
return result[0]
return None

def read_contacts(self):
#import pdb;pdb.set_trace()
contacts = []
count = 0
for message in mailbox.mbox(self.mailbox):
count = count + 1
subject = message['subject']
from_email = message['From']
if (from_email.find('mailer-daemon') != -1) or (from_email.find('postmaster') != -1) or (from_email.find('MAILER') != -1):
# for <>
#import pdb;pdb.set_trace()
from_email = self.parse_from_email(message.as_string(), "for <([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})>")
if not from_email:
# Final-Recipient: rfc822;
from_email = self.parse_from_email(message.as_string(), "Final-Recipient: rfc822; ([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})")
elif from_email.find('<') != -1:
from_email = self.parse_from_email(from_email, "<([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})>")
if not from_email:
#import pdb;pdb.set_trace()
from_email = self.parse_from_email(message.as_string(), "To:.*([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})")
#import pdb;pdb.set_trace()
if from_email:
#import pdb;pdb.set_trace()
f = open("/tmp/emails/" + str(count), "w")
return contacts