mail_queue: use db exceptions and parameters. fix $limit not escaped properly