def send_mail(obj, notification):    
    """
    Send mail to all recipients
    """
    member = getToolByName(obj, 'portal_membership').getAuthenticatedMember()
    from_email = member.getProperty('email')

    site_props = getToolByName(obj, 'portal_properties').site_properties
    encoding = site_props.getProperty('default_charset', 'UTF-8')

    registration = getToolByName(obj, 'portal_registration')
    if from_email and registration.isValidEmail(from_email):    
        pass
    else:
        return False, _(u"Your email address is invalid: %s" % str(from_email))

    host = obj.MailHost

    errors = {}

    kw = {}
    kw['REQUEST'] = self.request

    subject = recursiveTranslate(_(u'Your roles have changed in the GroupSpace ${content_title}', mapping= {'content_title': obj.Title()}), **kw) 

    for info in notification.values():
        
        old_roles = list(info['old_roles'])
        old_roles.sort()
        new_roles = list(info['new_roles'])
        old_roles.sort()

        msg = recursiveTranslate(_(u'You now have the following roles in the group \"${content_title}\":\n\t${new_roles}\nlocated at\n${content_url}\nBefore the change, you had the following roles in the group:\n\t${old_roles}', 
                                    mapping= {'content_title': obj.Title(), 
                                              'new_roles' : str(', '.join(new_roles)), 
                                              'content_url' : obj.absolute_url(), 
                                              'old_roles' : str(', '.join(old_roles))}),
                                  **kw)

        try:
            host.send(
                safe_unicode(msg),
                mto=info['email'],
                mfrom=from_email,
                subject=safe_unicode(subject),
                charset=encoding)
        except (SMTPServerDisconnected,
                SMTPSenderRefused,
                SMTPRecipientsRefused,
                SMTPDataError,
                SMTPConnectError,
                SMTPHeloError,
                SMTPAuthenticationError,
                socket.error,
                MailHostError), e:
            logger.error("collective.groupspace.mail error: %s", e, exc_info=1)
            errors[str(e)] = True
        except Exception, e:
            logger.error("collective.groupspace.mail error: %s", e, exc_info=1)
            raise
Beispiel #2
0
def send_mail(obj, notification):
    """
    Send mail to all recipients
    """
    member = getToolByName(obj, 'portal_membership').getAuthenticatedMember()
    from_email = member.getProperty('email')

    site_props = getToolByName(obj, 'portal_properties').site_properties
    encoding = site_props.getProperty('default_charset', 'UTF-8')

    registration = getToolByName(obj, 'portal_registration')
    if from_email and registration.isValidEmail(from_email):
        pass
    else:
        return False, _(u"Your email address is invalid: %s" % str(from_email))

    host = obj.MailHost

    errors = {}

    kw = {}
    kw['REQUEST'] = self.request

    subject = recursiveTranslate(
        _(u'Your roles have changed in the GroupSpace ${content_title}',
          mapping={'content_title': obj.Title()}), **kw)

    for info in notification.values():

        old_roles = list(info['old_roles'])
        old_roles.sort()
        new_roles = list(info['new_roles'])
        old_roles.sort()

        msg = recursiveTranslate(
            _(u'You now have the following roles in the group \"${content_title}\":\n\t${new_roles}\nlocated at\n${content_url}\nBefore the change, you had the following roles in the group:\n\t${old_roles}',
              mapping={
                  'content_title': obj.Title(),
                  'new_roles': str(', '.join(new_roles)),
                  'content_url': obj.absolute_url(),
                  'old_roles': str(', '.join(old_roles))
              }), **kw)

        try:
            host.send(safe_unicode(msg),
                      mto=info['email'],
                      mfrom=from_email,
                      subject=safe_unicode(subject),
                      charset=encoding)
        except (SMTPServerDisconnected, SMTPSenderRefused,
                SMTPRecipientsRefused, SMTPDataError, SMTPConnectError,
                SMTPHeloError, SMTPAuthenticationError, socket.error,
                MailHostError), e:
            logger.error("collective.groupspace.mail error: %s", e, exc_info=1)
            errors[str(e)] = True
        except Exception, e:
            logger.error("collective.groupspace.mail error: %s", e, exc_info=1)
            raise
Beispiel #3
0
    def send(self, mails, send_from_address, send_to_members, message):
        """
        Send the mail
        """

        context = aq_inner(self.context)

        encoding = self.site_properties.getProperty('default_charset', 'UTF-8')

        host = context.MailHost

        to_emails = []
        for user_name, user_id, user_email in mails:
            if user_id in send_to_members:
                to_emails.append(user_email)

        member = self.portal_membership.getAuthenticatedMember()
        from_name = member.getProperty(
            'fullname',
            '') != '' and member.getProperty('fullname') or member.getId()

        kw = {}
        kw['REQUEST'] = self.request

        untranslated = _(
            u"""The following message has been written by ${from_name} to the group ${content_title} located at ${content_url}:\n${message}""",
            mapping={
                'from_name': from_name,
                'content_title': context.Title(),
                'content_url': context.absolute_url(),
                'message': message
            })

        msg = recursiveTranslate(untranslated, **kw)

        subject = recursiveTranslate(
            _(u"""Message to the group \"${content_title}\" """,
              mapping={'content_title': context.Title()}), **kw)

        try:
            host.send(safe_unicode(msg),
                      mto=to_emails,
                      mfrom=send_from_address,
                      subject=safe_unicode(subject),
                      charset=encoding)

        except (SMTPServerDisconnected, SMTPSenderRefused,
                SMTPRecipientsRefused, SMTPDataError, SMTPConnectError,
                SMTPHeloError, SMTPAuthenticationError, socket.error,
                MailHostError), e:
            return False, _(u"An error occurred while sending the email. %s" %
                            str(e))
 def __call__(self):
     """Send the email or render the page
     """
     form = self.request.form
     submitted = form.get('form.submitted', False)
     send_button = form.get('form.button.Send', None) is not None    
     if submitted and send_button:
         if not self.request.get('REQUEST_METHOD','GET') == 'POST':
             raise Forbidden
         authenticator = self.context.restrictedTraverse('@@authenticator', 
                                                         None) 
         if not authenticator.verify(): 
             raise Forbidden
         send_to_members = form.get('send_to_members', [])
         send_from_address = form.get('send_from_address', [])
         message = form.get('message', "")
         
         proceed_to_send_mail = True
                     
         if not message:
             msg = _(u"Please fill in a message.")
             self.errors['message'] = msg
             proceed_to_send_mail = False
         if not send_to_members:
             msg = _(u"Select at least one recipient.")
             self.errors['send_to_members'] = msg
             proceed_to_send_mail = False
         if not self.portal_registration.isValidEmail(send_from_address):
             msg = _(u"Please enter a valid email address.")
             self.errors['send_from_address'] = msg              
             proceed_to_send_mail = False
         
         status_message = IStatusMessage(self.request)
         if proceed_to_send_mail:
             # Refetch all the user mails so no fake user ids can be injected
             mails = self.get_mails(role=form.get('role', None),
                                    user=form.get('user', None),
                                    group=form.get('group', None))
             success, msg = self.send(mails, 
                                      send_from_address, 
                                      send_to_members, 
                                      message)
             if success:
                 status_message.addStatusMessage(msg, type='info')
             else:
                 status_message.addStatusMessage(msg, type='error')
         else:
             msg = _(u"Please correct the indicated errors.")
             status_message.addStatusMessage(msg, type='error')
             
     return self.template()
Beispiel #5
0
    def __call__(self):
        """Send the email or render the page
        """
        form = self.request.form
        submitted = form.get('form.submitted', False)
        send_button = form.get('form.button.Send', None) is not None
        if submitted and send_button:
            if not self.request.get('REQUEST_METHOD', 'GET') == 'POST':
                raise Forbidden
            authenticator = self.context.restrictedTraverse(
                '@@authenticator', None)
            if not authenticator.verify():
                raise Forbidden
            send_to_members = form.get('send_to_members', [])
            send_from_address = form.get('send_from_address', [])
            message = form.get('message', "")

            proceed_to_send_mail = True

            if not message:
                msg = _(u"Please fill in a message.")
                self.errors['message'] = msg
                proceed_to_send_mail = False
            if not send_to_members:
                msg = _(u"Select at least one recipient.")
                self.errors['send_to_members'] = msg
                proceed_to_send_mail = False
            if not self.portal_registration.isValidEmail(send_from_address):
                msg = _(u"Please enter a valid email address.")
                self.errors['send_from_address'] = msg
                proceed_to_send_mail = False

            status_message = IStatusMessage(self.request)
            if proceed_to_send_mail:
                # Refetch all the user mails so no fake user ids can be injected
                mails = self.get_mails(role=form.get('role', None),
                                       user=form.get('user', None),
                                       group=form.get('group', None))
                success, msg = self.send(mails, send_from_address,
                                         send_to_members, message)
                if success:
                    status_message.addStatusMessage(msg, type='info')
                else:
                    status_message.addStatusMessage(msg, type='error')
            else:
                msg = _(u"Please correct the indicated errors.")
                status_message.addStatusMessage(msg, type='error')

        return self.template()
    def send(self, mails, send_from_address, send_to_members, message):
        """
        Send the mail
        """

        context = aq_inner(self.context)

        encoding = self.site_properties.getProperty('default_charset', 'UTF-8')
                            
        host = context.MailHost
    
        to_emails = []
        for user_name, user_id, user_email in mails:
            if user_id in send_to_members:
                to_emails.append(user_email)
                          
        member  = self.portal_membership.getAuthenticatedMember()
        from_name = member.getProperty('fullname', '') != '' and member.getProperty('fullname') or member.getId()

        kw = {}
        kw['REQUEST'] = self.request

        untranslated = _(u"""The following message has been written by ${from_name} to the group ${content_title} located at ${content_url}:\n${message}""", 
                          mapping = {'from_name' : from_name, 'content_title' : context.Title(), 'content_url' : context.absolute_url(), 'message' : message}
                        )

        msg = recursiveTranslate(untranslated, **kw)

        subject = recursiveTranslate(_(u"""Message to the group \"${content_title}\" """, mapping = {'content_title' : context.Title()}), **kw)

        try:
            host.send(
                safe_unicode(msg),
                mto=to_emails,
                mfrom=send_from_address,
                subject=safe_unicode(subject),
                charset=encoding)

        except (SMTPServerDisconnected,
                SMTPSenderRefused,
                SMTPRecipientsRefused,
                SMTPDataError,
                SMTPConnectError,
                SMTPHeloError,
                SMTPAuthenticationError,
                socket.error,
                MailHostError), e:
            return False, _(u"An error occurred while sending the email. %s" % str(e)) 
Beispiel #7
0
            u"""The following message has been written by ${from_name} to the group ${content_title} located at ${content_url}:\n${message}""",
            mapping={
                'from_name': from_name,
                'content_title': context.Title(),
                'content_url': context.absolute_url(),
                'message': message
            })

        msg = recursiveTranslate(untranslated, **kw)

        subject = recursiveTranslate(
            _(u"""Message to the group \"${content_title}\" """,
              mapping={'content_title': context.Title()}), **kw)

        try:
            host.send(safe_unicode(msg),
                      mto=to_emails,
                      mfrom=send_from_address,
                      subject=safe_unicode(subject),
                      charset=encoding)

        except (SMTPServerDisconnected, SMTPSenderRefused,
                SMTPRecipientsRefused, SMTPDataError, SMTPConnectError,
                SMTPHeloError, SMTPAuthenticationError, socket.error,
                MailHostError), e:
            return False, _(u"An error occurred while sending the email. %s" %
                            str(e))
        except:
            raise
        return True, _(u"Emails sent.")
Beispiel #8
0
        msg = recursiveTranslate(
            _(u'You now have the following roles in the group \"${content_title}\":\n\t${new_roles}\nlocated at\n${content_url}\nBefore the change, you had the following roles in the group:\n\t${old_roles}',
              mapping={
                  'content_title': obj.Title(),
                  'new_roles': str(', '.join(new_roles)),
                  'content_url': obj.absolute_url(),
                  'old_roles': str(', '.join(old_roles))
              }), **kw)

        try:
            host.send(safe_unicode(msg),
                      mto=info['email'],
                      mfrom=from_email,
                      subject=safe_unicode(subject),
                      charset=encoding)
        except (SMTPServerDisconnected, SMTPSenderRefused,
                SMTPRecipientsRefused, SMTPDataError, SMTPConnectError,
                SMTPHeloError, SMTPAuthenticationError, socket.error,
                MailHostError), e:
            logger.error("collective.groupspace.mail error: %s", e, exc_info=1)
            errors[str(e)] = True
        except Exception, e:
            logger.error("collective.groupspace.mail error: %s", e, exc_info=1)
            raise

    if errors:
        return False, _(u"An error occurred while sending the email. %s" %
                        ','.join(errors.keys()))

    return True, _(u"Notification emails sent.")
        untranslated = _(u"""The following message has been written by ${from_name} to the group ${content_title} located at ${content_url}:\n${message}""", 
                          mapping = {'from_name' : from_name, 'content_title' : context.Title(), 'content_url' : context.absolute_url(), 'message' : message}
                        )

        msg = recursiveTranslate(untranslated, **kw)

        subject = recursiveTranslate(_(u"""Message to the group \"${content_title}\" """, mapping = {'content_title' : context.Title()}), **kw)

        try:
            host.send(
                safe_unicode(msg),
                mto=to_emails,
                mfrom=send_from_address,
                subject=safe_unicode(subject),
                charset=encoding)

        except (SMTPServerDisconnected,
                SMTPSenderRefused,
                SMTPRecipientsRefused,
                SMTPDataError,
                SMTPConnectError,
                SMTPHeloError,
                SMTPAuthenticationError,
                socket.error,
                MailHostError), e:
            return False, _(u"An error occurred while sending the email. %s" % str(e)) 
        except:
            raise
        return True, _(u"Emails sent.")
                    
        try:
            host.send(
                safe_unicode(msg),
                mto=info['email'],
                mfrom=from_email,
                subject=safe_unicode(subject),
                charset=encoding)
        except (SMTPServerDisconnected,
                SMTPSenderRefused,
                SMTPRecipientsRefused,
                SMTPDataError,
                SMTPConnectError,
                SMTPHeloError,
                SMTPAuthenticationError,
                socket.error,
                MailHostError), e:
            logger.error("collective.groupspace.mail error: %s", e, exc_info=1)
            errors[str(e)] = True
        except Exception, e:
            logger.error("collective.groupspace.mail error: %s", e, exc_info=1)
            raise

    if errors:
        return False, _(u"An error occurred while sending the email. %s" % ','.join(errors.keys()))
    
    return True, _(u"Notification emails sent.")