Moved code into top level and got email working.