in reply to Email a File from Code

Instead of talking to the mail server yourself, I recommend using MIME::Lite for sending mails and attaching files. The documentation also has lots of examples.