# Tmail: Saving Excel attachments to disk

**URL:** <https://www.ruby-forum.com/t/tmail-saving-excel-attachments-to-disk/84515>\
**Category:** Ruby\
**Created:** [February 5, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/tmail-saving-excel-attachments-to-disk/84515 "2007-02-05T16:10:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_MSCampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@Andrew\_MSCampbell](https://www.ruby-forum.com/u/Andrew_MSCampbell)\
**Post date:** [February 5, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/tmail-saving-excel-attachments-to-disk/84515/1 "2007-02-05T16:10:38Z")

</div>

I’m using ActionMailer, which uses Tmail, to save  
Excel attachments to disk. I found a tutorial for  
saving attachments. However whether I decode the  
message part or not. Excel says the file is corrupt.  
I’m using Windows XP, latest stable Ruby and latest  
Rails gem. The mails I receive are valid multipart  
mail messages. The spreadsheet attachments are valid  
as well. Any Suggestions on the proper way to save  
Excel attachments using Tmail?

My code is very similar to this tutorial. All I do is  
check the content type of each part. If it is an  
Excel spreadsheet. I push it onto an array. Then  
iterate over the array of spreadsheets, saving them to  
disk. The code I use to write to disk is identical to  
the tutorial.  
[http://mondragon.cc/blog/articles/2006/12/30/decoding-email-attachments-with-actionmailer-base-receive](http://mondragon.cc/blog/articles/2006/12/30/decoding-email-attachments-with-actionmailer-base-receive)

Thanks,

---

<div class="post-metadata">

**Author:** ![Andrew\_MSCampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@Andrew\_MSCampbell](https://www.ruby-forum.com/u/Andrew_MSCampbell)\
**Post date:** [February 5, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/tmail-saving-excel-attachments-to-disk/84515/2 "2007-02-05T16:29:39Z")

</div>

On 2/5/07, Andrew M. Campbell [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My code is very similar to this tutorial. All I do is  
> check the content type of each part. If it is an  
> Excel spreadsheet. I push it onto an array. Then  
> iterate over the array of spreadsheets, saving them to  
> disk. The code I use to write to disk is identical to  
> the tutorial.  
> [http://mondragon.cc/blog/articles/2006/12/30/decoding-email-attachments-with-actionmailer-base-receive](http://mondragon.cc/blog/articles/2006/12/30/decoding-email-attachments-with-actionmailer-base-receive)
> 
> Thanks,

try adding “binary” to File.open() if you’re on windows

you can check if this is your problem by sending a known file to  
yourself and compare the sent and received files - if there’s a  
problem, they’d differ in length, just make sure the sent file  
contains \n (13)

---

<div class="post-metadata">

**Author:** ![Andrew\_MSCampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@Andrew\_MSCampbell](https://www.ruby-forum.com/u/Andrew_MSCampbell)\
**Post date:** [February 5, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/tmail-saving-excel-attachments-to-disk/84515/3 "2007-02-05T16:44:42Z")

</div>

— Jan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> multipart
> 
> > iterate over the array of spreadsheets, saving  
> > them to  
> > disk. The code I use to write to disk is  
> > identical to  
> > the tutorial.

[http://mondragon.cc/blog/articles/2006/12/30/decoding-email-attachments-with-actionmailer-base-receive](http://mondragon.cc/blog/articles/2006/12/30/decoding-email-attachments-with-actionmailer-base-receive)

> problem, they’d differ in length, just make sure the  
> sent file  
> contains \n (13)

Adding binary to File.open worked like a champ. Thank  
you.
