Saving incoming msg object to reply later (Serialization Error)

I am able to successfully receive incoming emails and save the information (from, to, headers, html, etc) to my data tables but I was wondering if there is a way to save the incoming email msg object in its entirety so that I can reply to the message directly at a later time using msg.reply(…)? Ideally, instead of sending a fresh new message to the customer I would like it to be sent as a reply to the original thread.

I tried saving it in a simple object with:

a = app_tables.incoming_emails.add_row(MSG=msg,CLAIMLESS=False, CLAIMS=[])

and the error I received was:

anvil.server.SerializationError: Cannot serialize arguments to function. Cannot serialize <class 'anvil.email.Message'> object at msg['kwargs']['MSG']

The only reason I was planning on saving the message object itself is for direct reply to thread. Is there a more suitable way to approach this?

In order to reply, you probably don’t need to save the entire message, just the “from” and “subject” parts. (But you might want to save the whole thing for reference later.)

Note: despite the name, a “Simple Object” column can store any number, string, boolean, list, or dictionary, but not a more general Python object. (See https://anvil.works/docs/data-tables#column-types).

If you can convert msg into a Python dict, where each value in the dict is one of the permitted types, then you can store the resulting dict in that column.

In any case, this new documentation may help: https://anvil.works/docs/email

It’s brand new, so there are still a few loose ends. For example, the docs at the link
https://anvil.works/docs/api/anvil.email#Message
do not (yet) describe the anvil.email.Message object.

If you visit https://anvil.works/docs, you can also Search on “email”. This will actually search beyond the documentation, into relevant tutorials, that may prove helpful.

3 Likes

Yep, that’s a bug, we’ll get that fixed! In the meantime, the autocompleter really does know about Message objects, so you can use that as your reference for all the fields you might want to store in a dict

1 Like

I guess I am still confused about how I should approach saving an incoming email message so I can reply at a later time to the original thread. Is the Message object only accessible within the
functions decorated with @anvil.email.handle_message() upon incoming?

Or do I not even need the message object to reply to the thread? It seems that anvil.email.sending an email w/ same from and subject would send them an email in a new thread.

I tried this but it would still reply from a new thread. It would work if there was a header argument such as anvil.email.send(.... , headers=[("In-Reply-To", message_id)]).

I wanted to use only anvil.email, but in the meantime using sendgrid works by means of message.header = Header('In-Reply-To', 'message_id', p=1)

Am I missing something simple with the anvil email service?