LPgenerator / django-db-mailer

Django module to easily send emails/sms/tts/push using django templates stored on database and managed through the Django Admin
https://github.com/LPgenerator/django-db-mailer
GNU General Public License v2.0
256 stars 80 forks source link

Can't enable tracking option #56

Closed DimaKudosh closed 8 years ago

DimaKudosh commented 8 years ago

I installed redis, django-redis, django-celery, httpagentparser, django-ipware and set DB_MAILER_TRACK_ENABLE and DB_MAILER_ENABLE_LOGGING to true, but when I sent email it doesn't create any record in track table. What did I do wrong? Maybe I missed some settings? I used AWS SES as email backend.

gotlium commented 8 years ago

Add urls route:

# urls.py
urlpatterns += patterns(
    '', url(r'^dbmail/', include('dbmail.urls')),
)

and show me raw message

DimaKudosh commented 8 years ago

Added urls , but it doesn't help me. What you mean? Where I can find raw message?

gotlium commented 8 years ago

You can send email to gmail, and click to Show original message. Template must be HTML, not TXT.

gotlium commented 8 years ago

And run celery worker. As you can see https://github.com/LPgenerator/django-db-mailer/blob/master/dbmail/views.py#L61 if celery is enabled, it required running workers to process requests.

gotlium commented 8 years ago

Send message from console:

$ ./manage.py shell
Python 2.7.11 (default, Dec 27 2015, 01:48:39) 
Type "copyright", "credits" or "license" for more information.

In [1]: from dbmail import send_db_mail

In [2]: send_db_mail('welcome', 'test@example.com', use_celery=False)
Content-Type: multipart/alternative;
 boundary="===============6734253122455930070=="
MIME-Version: 1.0
Subject: Welcome
From: Django <no_reply@local.host>
To: test@example.com
Date: Thu, 02 Jun 2016 17:48:17 -0000
Message-ID: <20160602174817.91403.32234@gotlium.local>

--===============6734253122455930070==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

Welcome to our site. We are glad to see you.

--===============6734253122455930070==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

<html><body><p>Welcome to our site. We are glad to see you.</p></body></html>
<table bgcolor="white"><tr><td><font size="-1" color="black"><img src="http://127.0.0.1:8000/dbmail/mail_read_tracker/IjE0NjQ4ODk2OTcuNzUxNTY0LTE0Yjk1ZDU0LTk1NjAtNDBjOC1iN2U4LWNlN2RhZDI1YzgzMiI:1b8WjB:Y-YJv8O_aZXpzxB4G478BQ5qjO0/" width="16" height="16" alt="" title="" border="0"></font></td></tr></table></center>
--===============6734253122455930070==--

-------------------------------------------------------------------------------

Open tracking URL

In [3]: !curl -v http://127.0.0.1:8000/dbmail/mail_read_tracker/IjE0NjQ4ODk2OTcuNzUxNTY0LTE0Yjk1ZDU0LTk1NjAtNDBjOC1iN2U4LWNlN2RhZDI1YzgzMiI:1b8WjB:Y-YJv8O_aZXpzxB4G478BQ5qjO0/
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> GET /dbmail/mail_read_tracker/IjE0NjQ4ODk2OTcuNzUxNTY0LTE0Yjk1ZDU0LTk1NjAtNDBjOC1iN2U4LWNlN2RhZDI1YzgzMiI:1b8WjB:Y-YJv8O_aZXpzxB4G478BQ5qjO0/ HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.43.0
> Accept: */*
> 
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Date: Thu, 02 Jun 2016 17:52:49 GMT
< Server: WSGIServer/0.1 Python/2.7.11
< Content-Type: image/gif
< 
* Closing connection 0
GIF89a????!?,D;

Check rows on table

In [4]: from dbmail.models import MailLogTrack

In [5]: MailLogTrack.objects.all().count()
1
DimaKudosh commented 8 years ago

I sent email from console, but block with tracker url didn't appear in the end of html, also It didn't appear when I sent message to my email. Maybe I missed something in settings. There is my settings:

# Celery

BROKER_URL = 'redis://127.0.0.1:6379/1'

CELERY_ACKS_LATE = True
CELERYD_PREFETCH_MULTIPLIER = 1

# use priority steps only for mail queue
if 'mail_messages' in sys.argv:
    BROKER_TRANSPORT_OPTIONS = {
        'priority_steps': list(range(10)),
    }

CELERY_TASK_SERIALIZER = 'pickle'
CELERY_DEFAULT_QUEUE = 'default'  # use mail_messages, if workers is divided

djcelery.setup_loader()

# DB Mailer settings
DB_MAILER_ENABLE_CELERY = True
DB_MAILER_SEND_RETRY = 2
DB_MAILER_LOGS_EXPIRE_DAYS = 30
DB_MAILER_ENABLE_LOGGING = True
DB_MAILER_TRACK_ENABLE = True
DB_MAILER_ALLOWED_MODELS_ON_ADMIN = [
    'MailCategory',
    'MailTemplate',
    'MailLog',
    'MailLogTrack',
    'MailFromEmailCredential',
    'MailFromEmail',
]

CACHES = {
    # Redis
    "default": {
        'BACKEND': 'redis_cache.cache.RedisCache',
        'LOCATION': '127.0.0.1:6379',
    },
}

I installed this libs: celery, redis, django-celery, django-redis, django-redis-cache, django-ipware, httpagentparser.

gotlium commented 8 years ago

Template must be HTML, not TXT.

Show me your template with all fields.

DimaKudosh commented 8 years ago
{
'from_email_id': 2, 
'interval': None, 
'context_note': u'Context note', 
'name': u'Default welcome email', 
'is_html': True, 
'is_active': True, 
'created': datetime.datetime(2016, 6, 2, 9, 42, 38, 448465, tzinfo=<UTC>),
 '_state': <django.db.models.base.ModelState object at 0x7ff61a025950>, 
'updated': datetime.datetime(2016, 6, 3, 8, 30, 11, 166733, tzinfo=<UTC>), 
'slug': u'default_welcome_email', 
'priority': 6, 
'num_of_retries': 3, 
'is_admin': False, 
'base_id': None, 
'message': u'<html>....</html>', 
'category_id': 1, 
'enable_log': True, 
'id': 1, 
'subject': u'Subject'
}
gotlium commented 8 years ago
  1. Try to clean cache on templates list, and re-send message
  2. You can print exception message here https://github.com/LPgenerator/django-db-mailer/blob/master/dbmail/backends/mail.py#L153
DimaKudosh commented 8 years ago

It doesn't help me. It's how I send emails:

def send_welcome_email(sender, instance, **kwargs):
        # do something
        send_db_mail(welcome_email.slug, email_to, context)

post_save.connect(send_welcome_email, sender=Invitation, dispatch_uid="send_welcome_email")

I don't use db-mailer signals. Maybe there is a problem with this signals?

gotlium commented 8 years ago

Django sites framework is configured? Domain and SITE_ID is set? Try to debug this method https://github.com/LPgenerator/django-db-mailer/blob/master/dbmail/backends/mail.py#L141

DimaKudosh commented 8 years ago

No, I configure it, but got this error in celery:

Task dbmail.mail_track[82c34044-5078-4632-8769-d331be13c993] retry: Retry in 300s: ImportError('cannot import name GeoIP',)
gotlium commented 8 years ago

Install libgeoip and MaxMind database (https://docs.djangoproject.com/en/1.8/ref/contrib/gis/geoip/)

DimaKudosh commented 8 years ago

Thanks, it's worked