in reply to how to speed the mail process?

You should profile your code to see which parts are slow. For a nice introduction read this article

We could suggest some possible performance tweeks that you likely don't use yet, but without knowing the code that's rather inefficient.

A small improvement could be to prepare the query outside the loop, you only have to do that once.