Can you also pull this tempfile stuff out into a method, and just pass in the attachment? I think that will clean things up somewhat.