from django.core.mail import EmailMultiAlternatives
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _

from pybb import settings as pybb_settings
from pybb.util import absolute_url

# i18n features disabled. See ticket #47

TOPIC_SUBSCRIPTION_TEXT_TEMPLATE = (u"""New reply from %(username)s to topic that you have subscribed on.
---
%(message)s
---
See topic: %(post_url)s
Unsubscribe %(unsubscribe_url)s""")

PM_RECIPIENT_TEXT_TEMPLATE = (u"""User %(username)s have sent your the new private message.
---
%(message)s
---
See message online: %(pm_url)s""")


def send_mail(rec_list, subject, text, html=None):
    """
    Shortcut for sending email.
    """

    from_email = settings.DEFAULT_FROM_EMAIL

    msg = EmailMultiAlternatives(subject, text, from_email, rec_list)
    if html:
        msg.attach_alternative(html, "text/html")
    if pybb_settings.EMAIL_DEBUG:
        print '---begin---'
        print 'To:', rec_list
        print 'Subject:', subject
        print 'Body:', text
        print '---end---'
    else:
        msg.send(fail_silently=True)


def notify_topic_subscribers(post):
    from pybb.models import Post

    topic = post.topic
    if post != topic.head:
        for user in topic.subscribers.all():
            if user != post.user:
                subject = u'RE: %s' % topic.name
                to_email = user.email
                text_content = TOPIC_SUBSCRIPTION_TEXT_TEMPLATE % {
                    'username': post.user.username,
                    'message': post.body_text,
                    'post_url': absolute_url(post.get_absolute_url()),
                    'unsubscribe_url': absolute_url(reverse('pybb_delete_subscription', args=[post.topic.id])),
                }
                #html_content = html_version(post)
                send_mail([to_email], subject, text_content)


def notify_pm_recipients(pm):
    from pybb.models import PrivateMessage

    subject = (u'New private message for you')
    to_email = pm.dst_user.email
    text_content = PM_RECIPIENT_TEXT_TEMPLATE % {
        'username': pm.src_user.username,
        'message': pm.body_text,
        'pm_url': absolute_url(pm.get_absolute_url()),
    }
    send_mail([to_email], subject, text_content)

@@ -1938,16 +1938,40 @@
 ts(pm):%0A
+    if not pm.read:%0A    
     from
@@ -2006,16 +2006,20 @@
 essage%0A%0A
+    
     subj
@@ -2057,16 +2057,20 @@
 r you')%0A
+    
     to_e
@@ -2094,24 +2094,28 @@
 r.email%0A    
+   
+ 
 text_content
@@ -2148,32 +2148,36 @@
 ATE %25 %7B%0A        
+    
 'username': pm.s
@@ -2194,32 +2194,36 @@
 ername,%0A        
+  
+  
 'message': pm.bo
@@ -2223,32 +2223,36 @@
 : pm.body_text,%0A
+    
         'pm_url'
@@ -2290,26 +2290,34 @@
 url()),%0A    
-%7D%0A
+    %7D%0A    
     send_mai

fix #49: really