Exemple #1
0
    def send_message(
        self,
        recipients: str,
        subject: str = "",
        body: str = "",
        attachments: str = None,
        html: bool = False,
        images: str = None,
        cc: str = None,
        bcc: str = None,
        save: bool = False,
    ):
        """Keyword for sending message through connected Exchange account.

        :param recipients: list of email addresses, defaults to []
        :param subject: message subject, defaults to ""
        :param body: message body, defaults to ""
        :param attachments: list of filepaths to attach, defaults to []
        :param html: if message content is in HTML, default `False`
        :param images: list of filepaths for inline use, defaults to []
        :param cc: list of email addresses, defaults to []
        :param bcc: list of email addresses, defaults to []
        :param save: is sent message saved to Sent messages folder or not,
            defaults to False

        Email addresses can be prefixed with ``ex:`` to indicate an Exchange
        account address.

        Recipients is a `required` parameter.
        """
        recipients, cc, bcc, attachments, images = self._handle_message_parameters(
            recipients, cc, bcc, attachments, images
        )
        self.logger.info("Sending message to %s", ",".join(recipients))

        m = Message(
            account=self.account,
            subject=subject,
            body=body,
            to_recipients=recipients,
            cc_recipients=cc,
            bcc_recipients=bcc,
        )

        self._add_attachments_to_msg(attachments, m)
        self._add_images_inline_to_msg(images, html, body, m)

        if html:
            m.body = HTMLBody(body)
        else:
            m.body = body

        if save:
            m.folder = self.account.sent
            m.send_and_save()
        else:
            m.send()
        return True
Exemple #2
0
    def send_phishing_email(self,
                            link_replace=True,
                            link='https://www.basspro.com',
                            attach_file=False,
                            file_path='',
                            file_name='Resume'):
        """Send phishing emails from a compromised account.

        :param link_replace: (Boolean)    if you want to replace the links in the email or not
        :param link:         (String)     if you do want to link replace, this is the link that will replace all of the existing links
        :param attatch_file: (Boolean)    if you want to attach a file or not
        :param file_path:    (String)     if you want to attach a file, the path of the desired file
        :param file_name:    (String)     if you want to attach a file, the name that you want the reciever to see
        """

        # Find the correct folder in the red_account
        old_emails = []
        for folder in self._red_account.inbox.children:

            if folder.name == self.full_username:
                for sub_folder in folder.children:
                    if sub_folder.name == "to_send":
                        old_emails = [email for email in sub_folder.all()]
                        break
                break

        # Iterate through emails and either replace links or attach files
        new_emails = []
        for email in old_emails:
            m = Message(account=self._victim_account,
                        subject=email.subject,
                        body=email.body,
                        to_recipients=email.to_recipients)

            # Replace links
            if link_replace:
                replace_string = 'href="' + link + '"'
                m.body = HTMLBody(re.sub(r'href="\S*"', replace_string,
                                         m.body))

            # Attach file
            if attach_file:
                m = self.attach_file(m, file_path, file_name)

            new_emails.append(m)

        print("Sending emails from the victim... ")

        self.vic_send_emails(new_emails)
def send(recipients, invoice_ref):
    """Construct email from Recipient object and send.

    Args:
        recipients (list): A list of recipient objects
        invoice_ref (str): Identifier of the invoice
            being emailed
    """

    for recipient in recipients:

        address = recipient.heademail
        cc_address = recipient.admemail

        if recipient.send_only_admin:
            address = recipient.admemail
            cc_address = ""

        print("address: {}".format(address))
        print("cc_address: {}".format(cc_address))
        try:
            m = Message(account=account,
                        folder=account.sent,
                        subject=('NIC@KCL: Invoice {0}'.format(invoice_ref)),
                        to_recipients=[Mailbox(email_address=address)])
            if cc_address:
                m.cc_recipients = [
                    Mailbox(email_address=cc_address),
                ]

            f = open(recipient.invoice, "r").read()
            m.body = HTMLBody(f)
            m.send_and_save()
        except:
            print('could not send email about {}'.format(invoice_ref))
            pass
def send(recipients, invoice_ref, email_settings, progress=None):
    """Construct email from Recipient object and send.

    Parameters
    -----------
    recipients : list
        A list of recipient objects
    invoice_ref : str
        Identifier of the invoice being emailed
    email_settings : dict
        configuration settings for exchange server
    progress: PyQt progress bar
    """
    username = email_settings["username"]
    password = email_settings["password"]

    credentials = Credentials(username=username, password=password)

    config = Configuration(
        server=email_settings["server"],
        credentials=credentials
    )
    account = Account(
        primary_smtp_address=email_settings["from_address"],
        config=config,
        autodiscover=False,
        access_type=DELEGATE
    )
    cc_address = None
    for rid, recipient in enumerate(recipients):
        print(recipient.bcode)

        if email_settings["test_mode"]:
            to_address = [
                Mailbox(email_address=email_settings["test_address"])
            ]
        else:
            to_address = [
                Mailbox(email_address=address)
                for address in recipient.addresses["to_email"]
            ]
            cc_address = [
                Mailbox(email_address=address)
                for address in recipient.addresses["cc_email"]
            ]
            if email_settings["copy_manager"]:
               cc_address.append(
                   Mailbox(email_address=email_settings["manager_address"])
               )

            if recipient.group.send_only_admin:
                address = [
                    Mailbox(email_address=address)
                    for address in recipient.addresses["cc_email"]
                ]
                cc_address = ""
 
        m = Message(
            account=account,
            folder=account.sent,
            subject=(
                'NIC@KCL: Invoice {0}'
                .format(invoice_ref)
            ),
            to_recipients=to_address,
            cc_recipients=[]
        )
        if cc_address:
            m.cc_recipients = cc_address

        print("address: {}".format(m.to_recipients))
        print("cc_address: {}".format(m.cc_recipients))
        m.body = HTMLBody(recipient.html)
        m.send_and_save()
        
        if progress is not None:
            progress.emit(rid)
Exemple #5
0
item.attach(my_file)
my_calendar_item = CalendarItem(...)
my_appointment = ItemAttachment(name='my_appointment', item=my_calendar_item)
item.attach(my_appointment)
item.save()

# Add an attachment on an existing item
my_other_file = FileAttachment(name='my_other_file.txt',
                               content=binary_file_content)
item.attach(my_other_file)

# Remove the attachment again
item.detach(my_file)

# If you want to embed an image in the item body, you can link to the file in the HTML
message = Message(...)
logo_filename = 'logo.png'
with open(logo_filename, 'rb') as f:
    my_logo = FileAttachment(name=logo_filename,
                             content=f.read(),
                             is_inline=True,
                             content_id=logo_filename)
message.attach(my_logo)
message.body = HTMLBody(
    '<html><body>Hello logo: <img src="cid:%s"></body></html>' % logo_filename)

# Attachments cannot be updated via EWS. In this case, you must to detach the attachment, update
# the relevant fields, and attach the updated attachment.

# Be aware that adding and deleting attachments from items that are already created in Exchange
# (items that have an item_id) will update the changekey of the item.
Exemple #6
0
    def send_message(
        self,
        recipients: Optional[Union[List[str], str]] = None,
        subject: Optional[str] = "",
        body: Optional[str] = "",
        attachments: Optional[Union[List[str], str]] = None,
        html: Optional[bool] = False,
        images: Optional[Union[List[str], str]] = None,
        cc: Optional[Union[List[str], str]] = None,
        bcc: Optional[Union[List[str], str]] = None,
        save: Optional[bool] = False,
    ) -> None:
        """Keyword for sending message through connected Exchange account.

        :param recipients: list of email addresses
        :param subject: message subject, defaults to ""
        :param body: message body, defaults to ""
        :param attachments: list of filepaths to attach, defaults to `None`
        :param html: if message content is in HTML, default `False`
        :param images: list of filepaths for inline use, defaults to `None`
        :param cc: list of email addresses
        :param bcc: list of email addresses
        :param save: is sent message saved to Sent messages folder or not,
            defaults to False

        Email addresses can be prefixed with ``ex:`` to indicate an Exchange
        account address.

        At least one target needs to exist for `recipients`, `cc` or `bcc`.
        """
        if not self.account:
            raise AuthenticationError("Not authorized to any Exchange account")
        recipients, cc, bcc, attachments, images = self._handle_message_parameters(
            recipients, cc, bcc, attachments, images)
        if not recipients and not cc and not bcc:
            raise NoRecipientsError(
                "Atleast one address is required for 'recipients', 'cc' or 'bcc' parameter"  # noqa: E501
            )
        self.logger.info("Sending message to %s", ",".join(recipients))

        m = Message(
            account=self.account,
            subject=subject,
            body=body,
            to_recipients=recipients,
            cc_recipients=cc,
            bcc_recipients=bcc,
        )

        self._add_attachments_to_msg(attachments, m)
        self._add_images_inline_to_msg(images, html, body, m)

        if html:
            m.body = HTMLBody(body)
        else:
            m.body = body

        # TODO. The exchangelib does not seem to provide any straightforward way of
        # verifying if message was sent or not
        if save:
            m.folder = self.account.sent
            m.send_and_save()
        else:
            m.send()