I am creating an saas email marketing system where users can create a company and run campaigns. Each user receives emails, which I send through smtplib. I then fetch the emails back using IMAP. I need to track the status of the emails, such as whether they are delivered, opened, or received an automated reply, etc.
For all connected accounts that are linked with a company’s campaign, the emails will be fetched and shown in the inbox. How can I determine that an email is a reply to a sent email and establish this relationship?
How can I track the email status?
Here is my model structure
class SentEmail(models.Model):
campaign = models.ForeignKey(to=Campaign, null=False, blank=False, on_delete=models.CASCADE)
company = models.ForeignKey(to=Company, null=True, blank=False, on_delete=models.CASCADE)
template = models.ForeignKey(to=EmailVariant, null=True, blank=True, on_delete=models.CASCADE)
followup_template = models.ForeignKey(to=FollowUpEmail, null=True, blank=True, on_delete=models.CASCADE)
contact = models.ForeignKey(to=Contact, null=False, blank=False, on_delete=models.CASCADE)
connected_account = models.ForeignKey(to=ConnectedAccount, null=False, blank=False, on_delete=models.CASCADE)
resend_id = models.CharField(max_length=250, null=True, blank=True)
email_content = models.TextField(null=True, blank=True)
email_sent = models.BooleanField(default=False)
email_delivered = models.BooleanField(default=False)
email_complained = models.BooleanField(default=False)
email_bounced = models.BooleanField(default=False)
email_opened = models.BooleanField(default=False)
email_clicked = models.BooleanField(default=False)
is_followup = models.BooleanField(default=False)
is_lead_replied = models.BooleanField(default=False)
is_automated_reply = models.BooleanField(default=False)
is_positive_reply = models.BooleanField(default=False)
created_at = models.DateTimeField(default=timezone.now)
objects = SentEmailManager()
class Meta:
verbose_name = 'Sent Email'
verbose_name_plural = 'Sent Emails'
def __str__(self):
return self.contact.name
I have researched using ChatGPT and other resources, and some suggest using webhooks to track events and configure them, or enabling tracking in the SMTP settings panel.
However, in my scenario, each user who buys the system can create a company, and each company can have multiple campaigns. Each campaign could have multiple connected SMTP accounts. Therefore, it isn’t feasible for each user to configure the webhooks themselves. I need a solution to solve this issue within my system.
I need something unique may be email id and then i can see this is reply of that email that i am storing in sentemail table