Replying to emails later

What I’m trying to do:
I am trying to reply to an email I’ve received and saved as a dict to database.

What I’ve tried and what’s not working:

I receive an email to an anvil app:

  @anvil.email.handle_message
  def handle_incoming_emails(msg):

I can reply immediately using

msg.reply()

What to do if I want to reply later?


There was a thread about it, however no solution was found:

The message class itself looks pretty complicated (from a simple object serialization point of view) it has multiple other class declarations nested inside of it.

This is untested but what if you:

  1. pickle the msg object using pickle.dumps(msg) creating a bytes string.
  2. put the bytes string into a media object and save that as a cell in a row in a data table for later (maybe with a timestamp or message id so you can retrieve the message easier later)
  3. To reply to the message later write a function that will get the media object, extract the bytes string, and reload the msg object with pickle.loads(pickled_msg_bytes)
  4. take the newly created object and pass that as the argument directly into the function decorated with @anvil.email.handle_message that then calls .reply() on the msg object based on some kind of call logic, and continue as usual.

Again, I have no idea if this will work, but looking at the code for how a Message class object works I don’t see why it would not, so it would be the first thing I would try.

2 Likes

Wow @ianb, this is great.

It should work.

Will test this and report here.

I can report that this approach works!

Thank you @ianb

3 Likes