def test_copy(): email_str = """ From: [email protected] To: [email protected] Subject: subject Body """ msg = email.message_from_string(email_str) msg2 = copy(msg) assert to_bytes(msg) == to_bytes(msg2) msg = email.message_from_string("👌") msg2 = copy(msg) assert to_bytes(msg) == to_bytes(msg2)
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 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")] 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, copy(msg), email_log, contact, envelope, smtp, mailbox, user)) return ret