Exemplo n.º 1
0
 def notify(self, msgobj=None, attachments=[]):
     if self.emails:
         emails = self.emails.split(",")
         if emails:
             sendemail(to=emails,
                       subject=msgobj.title,
                       body=msgobj.content,
                       attachments=attachments)
Exemplo n.º 2
0
 def notify(self, msgobj=None, attachments=[]):
     emails = []
     if self.emails:
         emails.extend(self.emails.split(","))
         if self.owner and self.owner.emails:
             emails.extend(self.owner.emails.split(","))
         sendemail(to=emails,
                   subject=msgobj.title,
                   body=msgobj.content,
                   attachments=attachments)
Exemplo n.º 3
0
    def notify(self, msgobj, attachments=[]):
        emails = []

        for obj in [self.contact, self.company]:
            if obj and obj.emails:
                emails.extend(obj.emails.split(","))
        sendemail(to=emails,
                  subject=msgobj.title,
                  body=msgobj.content,
                  attachments=attachments)
Exemplo n.º 4
0
 def notify(self, msgobj, attachments=[]):
     emails = []
     if self.emails:
         emails.extend(self.emails.split(","))
     for obj in [self.users]:
         if obj is not None:
             emails.extend(obj.emails.split(","))
     if emails:
         sendemail(to=emails, subject=msgobj.title,
                   body=msgobj.content, attachments=attachments)
Exemplo n.º 5
0
 def notify(self, msgobj, attachments=[]):
     emails = []
     for c in self.contacts:
         if c.emails:
             emails.extend(c.emails.split(","))
     if self.owner and self.owner.emails:
         emails.extend(self.owner.emails.split(","))
     sendemail(to=emails,
               subject=msgobj.title,
               body=msgobj.content,
               attachments=attachments)
Exemplo n.º 6
0
 def notify(self, msgobj, attachments=[]):
     emails = []
     for c in self.contacts:
         emails.extend(c.emails.split(","))
     if self.promoter and self.promoter.emails:
         emails.extend(self.promoter.emails.split(","))
     if self.guardian and self.guardian.emails:
         emails.extend(self.guardian.emails.split(","))
     if emails:
         sendemail(to=emails,
                   subject=msgobj.title,
                   body=msgobj.content,
                   attachments=attachments)
Exemplo n.º 7
0
def try_send(host, notification_emails, msg_id, subject, body, reply_to=None):
    time.sleep(1)
    now = datetime.now()
    state = MessageState.FAILED
    if notification_emails:
        try:
            sendemail(to=notification_emails,
                      from_='%s_message@%s' % (msg_id, host),
                      subject=subject,
                      body=body,
                      attachments=[],
                      reply_to=reply_to)
            state = MessageState.SENT
        except Exception as e:
            print('Error sending email : %s' % e)
    else:
        print('email not sent: message.notification_emails is empty list')

    from crm.db import db
    Message.query.filter_by(id=msg_id).update({
        'state': state,
        'time_sent': now
    })
    db.session.commit()
Exemplo n.º 8
0
def handle_mail(to, sender, subject, body):
    """
    Fired on every new email received 

    @param to [str]: receivers list. [should be in format $uid_roottypeobj@$domain].
    @param sender str: sender email. [should be in CRM database users/contacts emails] 
    @param subject str: subject
    @param body email.Message: email message object.

    If sender is not in recognized senders (contacts/users emails) an email will be sent back to him to contact support. 
    If sender is in recognized senders: we get the correct object receiving the message and attach the email text body to its messages.
    If receiever is SUPPORT_EMAIL: an email will be sent to it using sendgrid.
    """
    _contacts_emails = ",".join(
        [c.emails for c in db.session.query(Contact).all()])

    _users_emails = ",".join([u.emails for u in db.session.query(User).all()])

    RECOGNIZED_SENDERS = _contacts_emails + _users_emails

    rootclasses = RootModel.__subclasses__()

    if sender not in RECOGNIZED_SENDERS:
        print("CANT RECOGNIZE SENDER ", sender)
        sendemail(to=[sender], from_=SUPPORT_EMAIL)
    else:
        for x in to:
            msupport = match(PATTERN_SUPPORT_EMAIL, x)
            mrootobj = match(PATTERN_TO_ROOTOBJ, x)
            if msupport is not None:
                d = msupport.groupdict()
                domain = d['domain']
                sendemail(from_=SUPPORT_EMAIL, to=[sender], body=body)
            if mrootobj is not None:
                d = mrootobj.groupdict()
                objid = d['objid']
                rootobjtype = d['rootobjtype']
                cls = None
                q = [
                    x for x in rootclasses if x.__name__.lower() == rootobjtype
                ]
                if q:
                    cls = q[0]
                else:
                    continue

                obj = cls.query.filter(cls.id == objid).first()

                if obj:
                    body, attachments = parse_email_body(body)
                    # body, attachments [hashedfilename, hashedfilpath, hashedfileurl, originalfilename, binarycontent, type]
                    msgobj = Message(title=subject, content=body)

                    for attachment in attachments:
                        if not os.path.exists(attachment.hashedfilepath):
                            with open(attachment.hashedfilepath, "wb") as hf:
                                hf.write(attachment.binarycontent)
                        msgobj.links.append(
                            Link(url=attachment.hashedfileurl,
                                 labels=attachment.hashedfilename + "," +
                                 attachment.originalfilename))
                        msgobj.state = MessageState.TOSEND
                    obj.messages.append(msgobj)
                    db.session.add(obj)
                    try:
                        obj.notify(msgobj, attachments)
                        msgobj.state = MessageState.SENT
                    except:
                        msgobj.state = MessageState.FAILED
                    db.session.add(obj)

                domain = d['domain']
                db.session.commit()
Exemplo n.º 9
0
def handle_mail(to, sender, subject, body):
    """
    Fired on every new email received 

    @param to [str]: receivers list. [should be in format $uid_roottypeobj@$domain].
    @param sender str: sender email. [should be in CRM database users/contacts emails] 
    @param subject str: subject
    @param body email.Message: email message object.

    If sender is not in recognized senders (contacts/users emails) an email will be sent back to him to contact support. 
    If sender is in recognized senders: we get the correct object receiving the message and attach the email text body to its messages.
    If receiever is SUPPORT_EMAIL: an email will be sent to it using sendgrid.
    """

    # _contacts_emails = ",".join(
    #     [c.emails for c in db.session.query(Contact).all()]
    # )

    sender_obj = get_sender(sender)

    supported_models_to_send_to = RootModel.__subclasses__() + [Message]

    if sender_obj is None and SUPPORT_EMAIL not in to:
        print("CANT RECOGNIZE SENDER ", sender)
        sendemail(to=[sender], from_=SUPPORT_EMAIL)
    else:
        for x in to:
            msupport = match(PATTERN_SUPPORT_EMAIL, x)
            mrootobj = match(PATTERN_TO_ROOTOBJ, x)
            if msupport is not None:
                d = msupport.groupdict()
                sendemail(from_=[sender], to=[SUPPORT_EMAIL], body=body)
                continue
            if mrootobj is not None:
                d = mrootobj.groupdict()
                objid = d['objid']

                rootobjtype = d['rootobjtype']
                cls = None
                q = [
                    x for x in supported_models_to_send_to
                    if x.__name__.lower() == rootobjtype
                ]
                if q:
                    cls = q[0]
                else:
                    continue

                obj = cls.query.filter(cls.id == objid).first()
                if obj:

                    body, attachments = parse_email_body(body)
                    # body, attachments [hashedfilename, hashedfilpath, hashedfileurl, originalfilename, binarycontent, type]
                    msgobj = Message(title=subject,
                                     content=body,
                                     author_original=sender_obj)

                    for attachment in attachments:
                        if not os.path.exists(attachment.hashedfilepath):
                            with open(attachment.hashedfilepath, "wb") as hf:
                                hf.write(attachment.binarycontent)
                        msgobj.links.append(
                            Link(url=attachment.hashedfileurl,
                                 labels=attachment.hashedfilename + "," +
                                 attachment.originalfilename,
                                 filename=attachment.originalfilename))
                    if cls.__name__ == Message.__name__:
                        msgobj.deal_id = obj.deal_id
                        msgobj.company_id = obj.company_id
                        msgobj.contact_id = obj.contact_id
                        msgobj.user_id = obj.user_id
                        msgobj.task_id = obj.task_id
                        msgobj.organization_id = obj.organization_id
                        msgobj.project_id = obj.project_id
                        msgobj.sprint_id = obj.sprint_id
                        msgobj.event_id = obj.event_id
                        msgobj.author_last = sender_obj
                        msgobj.author_last_id = sender_obj.id
                        obj.replies.append(msgobj)
                    else:
                        obj.messages.append(msgobj)
                    db.session.add(obj)
                db.session.commit()