Extract email sending logic