示例#1
0
def send_email_to_mailbox(account,
                          to,
                          subject,
                          body,
                          bcc=None,
                          cc=None,
                          reply_to=None,
                          html_body=None,
                          attachments=[],
                          raw_message=None,
                          from_address=None):
    message_body = HTMLBody(html_body) if html_body else body
    m = Message(
        account=account,
        mime_content=raw_message.encode('UTF-8') if raw_message else None,
        folder=account.sent,
        cc_recipients=cc,
        bcc_recipients=bcc,
        subject=subject,
        body=message_body,
        to_recipients=to,
        reply_to=reply_to,
        author=from_address)
    if account.protocol.version.build <= EXCHANGE_2010_SP2:
        m.save()
        for attachment in attachments:
            m.attach(attachment)
        m.send()
    else:
        for attachment in attachments:
            m.attach(attachment)
        m.send_and_save()
    return m
示例#2
0
def send_email_to_mailbox(account,
                          to,
                          subject,
                          body,
                          bcc=None,
                          cc=None,
                          reply_to=None,
                          html_body=None,
                          attachments=[]):
    message_body = HTMLBody(html_body) if html_body else body
    m = Message(account=account,
                folder=account.sent,
                cc_recipients=cc,
                bcc_recipients=bcc,
                subject=subject,
                body=message_body,
                to_recipients=to,
                reply_to=reply_to)
    if account.protocol.version.build <= EXCHANGE_2010_SP2:
        m.save()
        for attachment in attachments:
            m.attach(attachment)
        m.send()
    else:
        for attachment in attachments:
            m.attach(attachment)
        m.send_and_save()
    return m
def generate_email(account, body, recipient, hb):
    '''
    The function gets called for each recipient.
    Emails are saved in the Drafts folder for human review
    '''
    
    m = Message(
        account=account,
        folder=account.drafts,
        subject='Discovery alert',
        body=HTMLBody(body),
        to_recipients=[Mailbox(email_address=recipient)]
    )

    root_path = join(os.getcwd(), 'pyalerts')
    hb_temp_path = join(root_path, "temp", f"temp_{hb}")

    for image_basename in os.listdir(hb_temp_path):

        image_path = join(hb_temp_path, image_basename)

        with open(image_path, 'rb') as f:
            embed_image = FileAttachment(
                name=image_path,
                content_id=os.path.basename(image_path),
                content=f.read(),
                is_inline=True)

        m.attach(embed_image)

    m.save()
示例#4
0
    def test_counts(self):
        # Test count values on a folder
        f = Folder(parent=self.account.inbox,
                   name=get_random_string(16)).save()
        f.refresh()

        self.assertEqual(f.total_count, 0)
        self.assertEqual(f.unread_count, 0)
        self.assertEqual(f.child_folder_count, 0)
        # Create some items
        items = []
        for i in range(3):
            subject = 'Test Subject %s' % i
            item = Message(account=self.account,
                           folder=f,
                           is_read=False,
                           subject=subject,
                           categories=self.categories)
            item.save()
            items.append(item)
        # Refresh values and see that total_count and unread_count changes
        f.refresh()
        self.assertEqual(f.total_count, 3)
        self.assertEqual(f.unread_count, 3)
        self.assertEqual(f.child_folder_count, 0)
        for i in items:
            i.is_read = True
            i.save()
        # Refresh values and see that unread_count changes
        f.refresh()
        self.assertEqual(f.total_count, 3)
        self.assertEqual(f.unread_count, 0)
        self.assertEqual(f.child_folder_count, 0)
        self.bulk_delete(items)
        # Refresh values and see that total_count changes
        f.refresh()
        self.assertEqual(f.total_count, 0)
        self.assertEqual(f.unread_count, 0)
        self.assertEqual(f.child_folder_count, 0)
        # Create some subfolders
        subfolders = []
        for i in range(3):
            subfolders.append(
                Folder(parent=f, name=get_random_string(16)).save())
        # Refresh values and see that child_folder_count changes
        f.refresh()
        self.assertEqual(f.total_count, 0)
        self.assertEqual(f.unread_count, 0)
        self.assertEqual(f.child_folder_count, 3)
        for sub_f in subfolders:
            sub_f.delete()
        # Refresh values and see that child_folder_count changes
        f.refresh()
        self.assertEqual(f.total_count, 0)
        self.assertEqual(f.unread_count, 0)
        self.assertEqual(f.child_folder_count, 0)
        f.delete()
示例#5
0
def send_email_to_mailbox(account: Account,
                          to: List[str],
                          subject: str,
                          body: str,
                          bcc: List[str],
                          cc: List[str],
                          reply_to: List[str],
                          html_body: Optional[str] = None,
                          attachments: Optional[List[str]] = None,
                          raw_message: Optional[str] = None,
                          from_address: Optional[str] = None):
    """
    Send an email to a mailbox.

    Args:
        account (Account): account from which to send an email.
        to (list[str]): a list of emails to send an email.
        subject (str): subject of the mail.
        body (str): body of the email.
        reply_to (list[str]): list of emails of which to reply to from the sent email.
        bcc (list[str]): list of email addresses for the 'bcc' field.
        cc (list[str]): list of email addresses for the 'cc' field.
        html_body (str): HTML formatted content (body) of the email to be sent. This argument
            overrides the "body" argument.
        attachments (list[str]): list of names of attachments to send.
        raw_message (str): Raw email message from MimeContent type.
        from_address (str): the email address from which to reply.
    """
    if not attachments:
        attachments = []
    message_body = HTMLBody(html_body) if html_body else body
    m = Message(
        account=account,
        mime_content=raw_message.encode('UTF-8') if raw_message else None,
        folder=account.sent,
        cc_recipients=cc,
        bcc_recipients=bcc,
        subject=subject,
        body=message_body,
        to_recipients=to,
        reply_to=reply_to,
        author=from_address)
    if account.protocol.version.build <= EXCHANGE_2010_SP2:
        m.save()
        for attachment in attachments:
            m.attach(attachment)
        m.send()
    else:
        for attachment in attachments:
            m.attach(attachment)
        m.send_and_save()
    return m
示例#6
0
    def red_upload_emails(self):
        """This method uploads emails from self.emails, to the folder specific to the victim.
        """

        sub_inbox, to_send = self._build_folder(self.full_username)

        # Copy all of the saved messages into the specified folder
        for email in self.emails:
            print(email.sender)

            m = Message(account=Session._red_account,
                        folder=sub_inbox,
                        subject=email.subject,
                        body=email.body,
                        to_recipients=[email.sender])
            m.save()
示例#7
0
def send_feedback(message):
    """Store feedback in exchange public folder"""
    if settings.FEEDBACK_FOLDER is None:
        return

    account = get_exchange_account()
    try:
        public_folder = reduce(operator.truediv, settings.FEEDBACK_FOLDER,
                               account.public_folders_root)
        m = Message(
            account=account,
            folder=public_folder,
            subject=FEEDBACK_SUBJECT,
            body=message,
        )
        m.save()
    except Exception as e:
        logging.error(e)