Extended CoreBlog2 Comment notification

With the help of CoreBlog’s mailing list (a big thanx to Mr.Nakai, Mr.Welter and Mr.Shibata) I have got the help to make it possible for CoreBlog2 to send notification mail to the ones who have made comments, this is done to make it possible to get a notification when a new comment is added to the same entry.
Full notification is always sent to the blog admin and the blog setting “Send a notification mail on new comment” is reused to setting the commentator notification on/off instead, these mails only contains the URL to the entry.
The mails are sent separately to keep the integrity and security intact.

This is the code needed in cbaddComment:

#Send notify mail to blog admin

try:
    to_addr   = context.getNotify_to()
    from_addr = context.getNotify_from()
    msgbody = context.translate('comment_notify_body')
    elements = {}
    for k in ('title','author','url','body'):
        if REQUEST.form.has_key(k):
            elements[k] = REQUEST.form[k]
        else:
            elements[k] = ''
    elements['post_ip'] = REQUEST.getClientAddr()
    elements['entry_url'] = context.absolute_url()
    msgbody = msgbody % (elements)
    msgsubject = context.translate('comment_notify_title')
    mgsheader = """To: %s
From: %s
Mime-Version: 1.0
Content-Type: text/plain; Charset=utf-8

""" % (to_addr,from_addr)
    cbtool.send_mail(mgsheader+msgbody, to_addr, from_addr, msgsubject)

# check status of notification mail setting
    if context.getSend_comment_notification():

#Get email addresses for commentators
        com_list = context.getComment()
        addr_list = []
        for com in com_list:
            if not addr_list.count(com.getEmail()):
                addr_list.append(com.getEmail())

#Send notify mail if need to commentators

        try:
            for addr in addr_list:
                if addr == REQUEST.form['email']:
                    continue
                else:
                    to_addr = addr
                    from_addr = context.getNotify_from()
                    msgbody = context.absolute_url()
                    msgsubject = context.translate('comment_notify_title')
                    mgsheader = """To: %s
From: %s
Mime-Version: 1.0
Content-Type: text/plain; Charset=utf-8

""" % (to_addr,from_addr)
                    cbtool.send_mail(mgsheader+msgbody, to_addr, from_addr, msgsubject)

        except Exception,e:
             log( 'COREBlog2/cbaddComment: '
                     'Some exception occured, %s' % e )

except Exception,e:
    log( 'COREBlog2/cbaddComment: '
             'Some exception occured, %s' % e )

Replaces:

#Send notify mail if need
if context.getSend_comment_notification():
    try:
        to_addr   = context.getNotify_to()
        from_addr = context.getNotify_to()
        msgbody = context.translate('comment_notify_body')
        elements = {}
        for k in ('title','author','url','body'):
            if REQUEST.form.has_key(k):
                elements[k] = REQUEST.form[k]
            else:
                elements[k] = ''
        elements['post_ip'] = REQUEST.getClientAddr()
        elements['entry_url'] = context.absolute_url()
        msgbody = msgbody % (elements)
        msgsubject = context.translate('comment_notify_title')
        mgsheader = """To: %s
From: %s
Mime-Version: 1.0
Content-Type: text/plain; Charset=utf-8

""" % (to_addr,from_addr)
        cbtool.send_mail(mgsheader+msgbody, to_addr, from_addr, msgsubject)

    except Exception,e:
        log( 'COREBlog2/cbaddComment: '
                 'Some exception occured, %s' % e )

Leave a Reply