Refactored gateway retrieval in mail template migration