def emailRoster(self):
        postitems = self.makeDictFromPost()
        tokey = 'event_roster_email_to'
        fromkey = 'event_roster_email_from'
        textkey = 'event_roster_email_text'

        if tokey not in postitems or postitems[tokey] == None \
                or postitems[tokey] == '':
            return "no to address"
        if fromkey not in postitems or postitems[fromkey] == None \
                or postitems[fromkey] == '':
            return "no to address"
        elif textkey not in postitems or postitems[textkey] == None \
                or postitems[textkey] == '':
            postitems[textkey] = ''

        registry = getUtility(IRegistry)
        subject = registry.records['collective.eventmanager.emailtemplates.IEMailTemplateSettings.roster_subject'].value
        htmlmessage = registry.records['collective.eventmanager.emailtemplates.IEMailTemplateSettings.roster_htmlbody'].value
        plainmessage = registry.records['collective.eventmanager.emailtemplates.IEMailTemplateSettings.roster_textbody'].value
        if subject == None:
            subject = 'Roster for ' + self.context.title
        if htmlmessage == None:
            htmlmessage = ''
        if plainmessage == None:
            plainmessage = ''

        htmlmessage += '<div>' + postitems[textkey] + '</div>'
        plainmessage += '\n\n' + postitems[textkey]

        regs = [self.context.registrations[a]
                    for a in self.context.registrations]

        subjecttemplate = Template(subject)
        htmltemplate = Template(htmlmessage)
        plaintemplate = Template(plainmessage)
        renderedsubject = subjecttemplate.render(emevent=self.context,
                                                 registrations=regs)
        renderedhtml = htmltemplate.render(emevent=self.context,
                                           registrations=regs)
        renderedplain = plaintemplate.render(emevent=self.context,
                                           registrations=regs)

        mh = getToolByName(self.context, 'MailHost')
        msg = MIMEMultipart('alternative')
        msgpart1 = MIMEText(renderedplain, 'plain', 'utf-8')
        msgpart2 = MIMEText(renderedhtml, 'html', 'utf-8')
        msg.attach(msgpart1)
        msg.attach(msgpart2)
        msg['Subject'] = renderedsubject.decode('utf-8')
        msg['From'] = postitems[fromkey]
        msg['To'] = postitems[tokey]
        try:
            mh.send(msg)
        except Exception:
            return 'failure'

        return 'success'
def sendEMail(emevent, emailtype, mto=[], reg=None, defattachments=[],
              deffrom=None, defsubject=None, defmsg=None):

    mtemplate = ''
    mfrom = deffrom
    msubject = defsubject
    mbody = defmsg
    additional_html_body = None
    additional_plain_body = None
    mattachments = defattachments

    mh = getToolByName(emevent, 'MailHost')
    registry = getUtility(IRegistry)

    # get data configured for this specific event
    if emailtype == 'thank you':
        mtemplate = 'thankyou'
        mfrom = emevent.thankYouEMailFrom
        msubject = emevent.thankYouEMailSubject
        mbody = emevent.thankYouEMailBody

    elif emailtype == 'on waiting list':
        mtemplate = 'onwaitinglist'
        mfrom = emevent.waitingListEMailFrom
        msubject = emevent.waitingListEMailSubject
        mbody = emevent.waitingListEMailBody

    elif emailtype == 'registration full':
        mtemplate = 'registrationfull'
        mfrom = emevent.registrationFullEMailFrom
        msubject = emevent.registrationFullEMailSubject
        mbody = emevent.registrationFullEMailBody

    elif emailtype == 'confirmation':
        mtemplate = 'confirmation'
        if emevent.thankYouIncludeConfirmation and reg is not None:
            # only include a confirmation link where a registration has
            #   been approved.
            confirmtext = ''
            confirmhtml = ''
            portal = getSite()
            wf = getToolByName(portal, 'portal_workflow')
            regstatus = wf.getStatusOf(
                'collective.eventmanager.Registration_workflow',
                reg)
            if regstatus is not None \
                    and regstatus['review_state'] == 'approved':
                confirmreghash = generateRegistrationHash('confirmation', reg)
                confirmtext = """
Please visit the following URL in order to confirm your registration:

%s/confirm-registration?h=%s


""" % (emevent.absolute_url(), confirmreghash)
                confirmhtml = '<br />Please ' \
                '<a href="%s/confirm-registration?h=%s">confirm your ' \
                'registration</a>.</div><br /><br />' \
                % (emevent.absolute_url(), confirmreghash)

            # put together the final messages
            cancelreghash = generateRegistrationHash('cancel', reg)
            additional_plain_body = """
=======================%s
If you'd like to cancel your registration, please visit the following URL:

%s/cancel-registration?h=%s""" % (confirmtext,
                                  emevent.absolute_url(), cancelreghash)
            additional_html_body = \
                '<div>=======================%s<div>' \
                'You may <a href="%s/cancel-registration?h=%s">cancel your ' \
                'registration</a> at any time.</div>' \
                % (confirmhtml,
                   emevent.absolute_url(), cancelreghash)

    elif emailtype == 'announcement':
        mtemplate = 'announcement'

    elif emailtype == 'other':
        mtemplate = 'other'

    if mfrom == None or mfrom == '':
        return False

    # get the keys for each of the registry entries that define the
    # email templates
    registrykey = "collective.eventmanager.emailtemplates" \
                  ".IEMailTemplateSettings.%s_%s"
    tsubkey = registrykey % (mtemplate, 'subject')
    thtmlkey = registrykey % (mtemplate, 'htmlbody')
    tplainkey = registrykey % (mtemplate, 'textbody')

    # get the site wide templates
    tsub = registry.records[tsubkey].value
    thtml = registry.records[thtmlkey].value
    tplain = registry.records[tplainkey].value

    subtemplate = Template(tsub)
    htmltemplate = Template(thtml)
    plaintemplate = Template(tplain)

    messagetemplate = Template(mbody)

    # apply results of event template render to site wide templates
    messageresult = messagetemplate.render(emevent=emevent).decode('utf-8')
    messagehtml = htmltemplate.render(
        emevent=emevent,
        event_content=messageresult)
    messageplain = plaintemplate.render(
        emevent=emevent,
        event_content=messageresult)

    if additional_html_body is not None:
        messagehtml += additional_html_body
    if additional_plain_body is not None:
        messageplain += additional_plain_body

    subject = subtemplate.render(emevent=emevent, event_content=msubject)

    # create a multipart message to hold both a text and html version
    msg = MIMEMultipart('alternative')
    msg['Subject'] = subject.decode('utf-8')
    msg['From'] = mfrom.encode('ascii')
    msgpart1 = MIMEText(messageplain, 'plain', 'utf-8')
    msgpart2 = MIMEText(messagehtml, 'html', 'utf-8')
    msg.attach(msgpart1)
    msg.attach(msgpart2)

    # if there were any attachments, then add them to the message
    if mattachments != None and len(mattachments) > 0:
        for attachment in mattachments:
            apart = MIMEBase('application', 'octet-stream')
            apart.set_payload(attachment['data'])
            encoders.encode_base64(apart)
            apart.add_header('Content-Disposition', 'attachment',
                            filename=attachment['name'])
            msg.attach(apart)

    # send a separate message to each address
    for address in mto:
        msg['To'] = address.encode('ascii')
        mh.send(msg.as_string(), address, mfrom, subject)
        #mh.send(msg)

    #mh.send('message', '*****@*****.**', '*****@*****.**', 'subject')

    return True