2011-09-26 9 views
10

मेरा Django एप्लिकेशन बहुत सारे ईमेल भेजता है और मैंने इसे पूरी तरह से परीक्षण करने का प्रयास किया है। हालांकि, पहले कुछ महीनों के लिए, मैं यह सुनिश्चित करने के लिए सभी आउटगोइंग ईमेल लॉग करना चाहता हूं कि सबकुछ सुचारू रूप से काम कर रहा है। क्या एक Django मॉड्यूल है जो मुझे ऐसा करने की अनुमति देता है और आउटगोइंग ईमेल को प्रशासन पैनलमैं Django में सभी आउटगोइंग ईमेल कैसे लॉग कर सकता हूं?

धन्यवाद के माध्यम से दिखाई देता है।

उत्तर

8

मैंने एक कस्टम ईमेल बैकएंड लिखा जो एक मॉडल को सामान लॉग करता है।

यहाँ मेरी बैकएंड है:

from django.core.mail.backends.smtp import * 
from django.db import transaction 

from modules.common.models import * 

class LoggingEmailBackend(EmailBackend): 
    """ 
    A wrapper around the SMTP backend that logs all emails to the DB. 
    """ 
    def send_messages(self, email_messages): 
    """ 
    A helper method that does the actual logging 
    """ 
    with transaction.commit_on_success(): 

     for email_message in email_messages: 

      email_record = Email.objects.create(
       to='; '.join(email_message.recipients()), 
       subject=email_message.subject, body=email_message.body, 
      ) 

      try: 
       return super(LoggingEmailBackend, self)._send(
        email_message 
       ) 
      except: 
       email_record.ok = False 
       return False 
      finally: 
       email_record.ok = True 
       return True 

यहाँ मॉडल है:

class Email(models.Model): 
    """ 
    Model to store all the outgoing emails. 
    """ 
    when = models.DateTimeField(
     null=False, auto_now_add=True 
    ) 
    to = models.EmailField(
     null=False, blank=False, 
    ) 
    subject = models.CharField(
     null=False, max_length=128, 
    ) 
    body = models.TextField(
     null=False, max_length=1024, 
    ) 
    ok = models.BooleanField(
     null=False, default=True, 
    ) 

यहाँ मेरी मॉडल है:

from django.contrib import admin 

from modules.common.models import * 

class EmailAdmin(admin.ModelAdmin): 
    """ 
    Admin part for managing the the Email model 
    """ 
    list_display = ['to', 'subject', 'ok',] 
    list_filter = ['ok'] 
    readonly_fields = ['when', 'to', 'subject', 'body', 'ok'] 
    search_fields = ['subject', 'body', 'to'] 

    def has_delete_permission(self, request, obj=None): 
     return False 

    def has_add_permission(self, request): 
     return False 


admin.site.register(Email, EmailAdmin) 
+1

यह रिकॉर्ड नहीं करेगा विफलता पर ठीक = झूठी। मॉडल सहेजा नहीं गया है। –

1

मुझे नहीं पता कि एक मॉड्यूल मौजूद है जो इस तरह से काम करता है, लेकिन एक कस्टम लिखना केक का एक टुकड़ा है। बस एक अलग मॉडल बनाएं और हर बार जब आप एक ईमेल भेजें, एक नया उदाहरण बनाएं (ईमेल भेजने के लिए एक कस्टम विधि का उपयोग करें)। फिर, इस मॉडल को व्यवस्थापक और बिंगो से लिंक करें ..

2

के बाद से ओपी प्रवेश के बारे में और नहीं के बारे में डीबी के लिए बचत से पूछा, यहां एक मिडलवेयर है जो करता है:

import django.core.mail.backends.smtp 
import logging 

logger = logging.getLogger(__name__) # or you could enter a specific logger name 

class LoggingBackend(django.core.mail.backends.smtp.EmailBackend): 

    def send_messages(self, email_messages): 
    try: 
     for msg in email_messages: 
      logger.info(u"Sending message '%s' to recipients: %s", msg.subject, msg.to) 
    except: 
     logger.exception("Problem logging recipients, ignoring") 

    return super(LoggingBackend, self).send_messages(email_messages) 

और फिर अपने settings.py में:

EMAIL_BACKEND = 'whereiputit.LoggingBackend' 
संबंधित मुद्दे