~vcs-imports/silva/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Copyright (c) 2008-2010 Infrae. All rights reserved.
# See also LICENSE.txt
# $Id: mail.py 46475 2010-11-11 15:05:48Z sylvain $

import logging
from smtplib import SMTPException

# Zope
from AccessControl import ModuleSecurityInfo
from Products.MailHost.MailHost import _mungeHeaders
from Products.Silva import MAILHOST_ID

logger = logging.getLogger('silva.email')

module_security =  ModuleSecurityInfo('Products.Silva.helpers')

module_security.declareProtected('Use mailhost services', 'sendmail')
def sendmail(context, message, mto=None, mfrom=None, subject=None):
    """Send a fraking mail, should work with regular Zope Mailhost,
    and MaildropHost.
    """

    mh = getattr(context.get_root(), MAILHOST_ID)
    messageText, mto, mfrom = _mungeHeaders(message, mto, mfrom, subject, charset='utf-8')
    try:
        mh._send(mfrom, mto, messageText)
    except SMTPException as error:
        logger.error('Error sending email from %s to %s: %s' % (
                mfrom, mto, str(error)))