smtp.send without blocking the application?

I’ve got my application sending emails via smtp, but it blocks: input, stops animations, and prevents or queues other UI calls on the application until the smtp call is complete.

Is there any way to do the send operation asynchronously? I would like the user to be able to do other operations in the application while waiting for smtp to complete.

Here is some example code, where I create a spinner widget and it becomes frozen during the smtp call.

I’ve tested this on the simulator (build 2016.2828) and my android device (sgs3).

local smtp = require("socket.smtp") local widget = require("widget") local spinner = widget.newSpinner({x=display.contentCenterX, y=display.contentCenterY}) spinner:start() local r, e = smtp.send({ from = "\<my@email.com\>", rcpt = {"\<recipient@email.com\>", source = smtp.message(messageTable), user = me, password = passWord, server = serverHostName, port = 25 }) print(r, e)

Thanks,

Daniel

I don’t know if coroutines will help you or not.  The socket library is a blocking library.

https://coronalabs.com/blog/2015/02/10/tutorial-using-coroutines-in-corona/

Rob

Coroutines are useful, but unfortunately aren’t applicable in this case, as far as my understanding goes.

I’d have to re-write the socket library from scratch to prevent it from blocking the thread. I guess I either make do and expect the user to be patient while the smtp function finishes it’s work, or find an asynchronous lua socket library. I certainly don’t have the knowledge or time to write my own.

The other thing you could do is to have a web service that you could transmit your email databits too (from, to, subject, body) and have a script on the server do the actual send. That way you can use network.request() to contact the server script. It’s non-blocking.

Rob

Thanks Rob, I like that method the best and will look into that.

I’ve got quite a few options now so you can consider this thread solved.

Daniel

I don’t know if coroutines will help you or not.  The socket library is a blocking library.

https://coronalabs.com/blog/2015/02/10/tutorial-using-coroutines-in-corona/

Rob

Coroutines are useful, but unfortunately aren’t applicable in this case, as far as my understanding goes.

I’d have to re-write the socket library from scratch to prevent it from blocking the thread. I guess I either make do and expect the user to be patient while the smtp function finishes it’s work, or find an asynchronous lua socket library. I certainly don’t have the knowledge or time to write my own.

The other thing you could do is to have a web service that you could transmit your email databits too (from, to, subject, body) and have a script on the server do the actual send. That way you can use network.request() to contact the server script. It’s non-blocking.

Rob

Thanks Rob, I like that method the best and will look into that.

I’ve got quite a few options now so you can consider this thread solved.

Daniel