async def handle_forward(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> List[Tuple[bool, str]]: """return whether an email has been delivered and the smtp status ("250 Message accepted", "550 Non-existent email address", etc) """ address = rcpt_to.lower().strip() # alias@SL alias = Alias.get_by(email=address) if not alias: LOG.d("alias %s not exist. Try to see if it can be created on the fly", address) alias = try_auto_create(address) if not alias: LOG.d("alias %s cannot be created on-the-fly, return 550", address) return [(False, "550 SL E3 Email not exist")] mail_from = envelope.mail_from.lower().strip() for mb in alias.mailboxes: # email send from a mailbox to alias if mb.email.lower().strip() == mail_from: LOG.exception("cycle email sent from %s to %s", mb, alias) handle_email_sent_to_ourself(alias, mb, msg, alias.user) return [(True, "250 Message accepted for delivery")] contact = get_or_create_contact(msg["From"], envelope.mail_from, alias) email_log = EmailLog.create(contact_id=contact.id, user_id=contact.user_id) db.session.commit() if not alias.enabled: LOG.d("%s is disabled, do not forward", alias) email_log.blocked = True db.session.commit() # do not return 5** to allow user to receive emails later when alias is enabled return [(True, "250 Message accepted for delivery")] user = alias.user ret = [] mailboxes = alias.mailboxes # no need to create a copy of message if len(mailboxes) == 1: mailbox = mailboxes[0] ret.append(await forward_email_to_mailbox(alias, msg, email_log, contact, envelope, smtp, mailbox, user)) # create a copy of message for each forward else: for mailbox in mailboxes: ret.append(await forward_email_to_mailbox(alias, copy(msg), email_log, contact, envelope, smtp, mailbox, user)) return ret
def rate_limited_forward_phase(alias_address: str) -> bool: alias = Alias.get_by(email=alias_address) if alias: return rate_limited_for_alias(alias) or rate_limited_for_mailbox(alias) else: LOG.d( "alias %s not exist. Try to see if it can be created on the fly", alias_address, ) alias = try_auto_create(alias_address) if alias: return rate_limited_for_mailbox(alias) return False
def handle_forward(envelope, smtp: SMTP, msg: Message, rcpt_to: str) -> List[Tuple[bool, str]]: """return whether an email has been delivered and the smtp status ("250 Message accepted", "550 Non-existent email address", etc) """ address = rcpt_to.lower().strip() # alias@SL alias = Alias.get_by(email=address) if not alias: LOG.d("alias %s not exist. Try to see if it can be created on the fly", address) alias = try_auto_create(address) if not alias: LOG.d("alias %s cannot be created on-the-fly, return 550", address) return [(False, "550 SL E3")] contact = get_or_create_contact(msg["From"], envelope.mail_from, alias) email_log = EmailLog.create(contact_id=contact.id, user_id=contact.user_id) if not alias.enabled: LOG.d("%s is disabled, do not forward", alias) email_log.blocked = True db.session.commit() # do not return 5** to allow user to receive emails later when alias is enabled return [(True, "250 Message accepted for delivery")] user = alias.user ret = [] for mailbox in alias.mailboxes: ret.append( forward_email_to_mailbox(alias, msg, email_log, contact, envelope, smtp, mailbox, user)) return ret