Exemplo n.º 1
0
 def set_sender_full_name(self):
     if not self.sender_full_name and self.sender:
         if self.sender == "Administrator":
             self.sender_full_name = dataent.db.get_value(
                 "User", "Administrator", "full_name")
             self.sender = dataent.db.get_value("User", "Administrator",
                                                "email")
         elif self.sender == "Guest":
             self.sender_full_name = self.sender
             self.sender = None
         else:
             if self.sent_or_received == 'Sent':
                 validate_email_add(self.sender, throw=True)
             sender_name, sender_email = parse_addr(self.sender)
             if sender_name == sender_email:
                 sender_name = None
             self.sender = sender_email
             self.sender_full_name = sender_name or dataent.db.exists(
                 "Contact", {"email_id": sender_email}) or sender_email
Exemplo n.º 2
0
def filter_email_list(doc, email_list, exclude, is_cc=False, is_bcc=False):
    # temp variables
    filtered = []
    email_address_list = []

    for email in list(set(email_list)):
        email_address = (parse_addr(email)[1] or "").lower()
        if not email_address:
            continue

        # this will be used to eventually find email addresses that aren't sent to
        doc.all_email_addresses.append(email_address)

        if (email in exclude) or (email_address in exclude):
            continue

        if is_cc:
            is_user_enabled = dataent.db.get_value("User", email_address,
                                                   "enabled")
            if is_user_enabled == 0:
                # don't send to disabled users
                continue

        if is_bcc:
            is_user_enabled = dataent.db.get_value("User", email_address,
                                                   "enabled")
            if is_user_enabled == 0:
                continue

        # make sure of case-insensitive uniqueness of email address
        if email_address not in email_address_list:
            # append the full email i.e. "Human <*****@*****.**>"
            filtered.append(email)
            email_address_list.append(email_address)

    doc.sent_email_addresses.extend(email_address_list)

    return filtered
Exemplo n.º 3
0
	def replace_sender_name(self):
		if cint(self.email_account.always_use_account_name_as_sender_name):
			self.set_header('X-Original-From', self.sender)
			sender_name, sender_email = parse_addr(self.sender)
			self.sender = email.utils.formataddr((str(Header(self.email_account.name, 'utf-8')), sender_email))
Exemplo n.º 4
0
def get_outgoing_email_account(raise_exception_not_set=True,
                               append_to=None,
                               sender=None):
    """Returns outgoing email account based on `append_to` or the default
		outgoing account. If default outgoing account is not found, it will
		try getting settings from `site_config.json`."""

    sender_email_id = None
    if sender:
        sender_email_id = parse_addr(sender)[1]

    if not getattr(dataent.local, "outgoing_email_account", None):
        dataent.local.outgoing_email_account = {}

    if not dataent.local.outgoing_email_account.get(append_to) \
     or dataent.local.outgoing_email_account.get(sender_email_id) \
     or dataent.local.outgoing_email_account.get("default"):
        email_account = None

        if append_to:
            # append_to is only valid when enable_incoming is checked

            # in case of multiple Email Accounts with same append_to
            # narrow it down based on email_id
            email_account = _get_email_account({
                "enable_outgoing": 1,
                "enable_incoming": 1,
                "append_to": append_to,
                "email_id": sender_email_id
            })

            # else find the first Email Account with append_to
            if not email_account:
                email_account = _get_email_account({
                    "enable_outgoing": 1,
                    "enable_incoming": 1,
                    "append_to": append_to
                })

        if not email_account and sender_email_id:
            # check if the sender has email account with enable_outgoing
            email_account = _get_email_account({
                "enable_outgoing": 1,
                "email_id": sender_email_id
            })

        if not email_account:
            # sender don't have the outging email account
            sender_email_id = None
            email_account = get_default_outgoing_email_account(
                raise_exception_not_set=raise_exception_not_set)

        if not email_account and raise_exception_not_set and cint(
                dataent.db.get_single_value('System Settings',
                                            'setup_complete')):
            dataent.throw(
                _("Please setup default Email Account from Setup > Email > Email Account"
                  ), dataent.OutgoingEmailError)

        if email_account:
            if email_account.enable_outgoing and not getattr(
                    email_account, 'from_site_config', False):
                raise_exception = True
                if email_account.smtp_server in [
                        'localhost', '127.0.0.1'
                ] or email_account.no_smtp_authentication:
                    raise_exception = False
                email_account.password = email_account.get_password(
                    raise_exception=raise_exception)
            email_account.default_sender = email.utils.formataddr(
                (email_account.name, email_account.get("email_id")))

        dataent.local.outgoing_email_account[append_to or sender_email_id
                                             or "default"] = email_account

    return dataent.local.outgoing_email_account.get(append_to) \
     or dataent.local.outgoing_email_account.get(sender_email_id) \
     or dataent.local.outgoing_email_account.get("default")