Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Programmatically adding an Excel attachment

Reply
Thread Tools

Programmatically adding an Excel attachment

 
 
gregarican
Guest
Posts: n/a
 
      01-15-2009
I've checked out this page --> http://www.tutorialspoint.com/ruby/r...ding_email.htm
that shows how to e-mail a text file attachment.I'm trying to attach
an Excel file, but the receiver gets a corrupted file attachment. I've
changed the Content-Type: in the MIME header to application/vnd.ms-
excel but it doesn't seem to work. Anyone care to offer a quick code
snippet that will fit the bill?
 
Reply With Quote
 
 
 
 
gregarican
Guest
Posts: n/a
 
      01-16-2009
On Jan 15, 4:23*pm, gregarican <greg.kuj...@gmail.com> wrote:
> I've checked out this page -->http://www.tutorialspoint.com/ruby/r...ding_email.htm
> that shows how to e-mail a text file attachment.I'm trying to attach
> an Excel file, but the receiver gets a corrupted file attachment. I've
> changed the Content-Type: in the MIME header to application/vnd.ms-
> excel but it doesn't seem to work. Anyone care to offer a quick code
> snippet that will fit the bill?


I solved my own problem. It was just a question of me not parsing the
Excel file correctly. Since I was basing things off sample code that
opened plain text files to send as attachments, I wasn't building the
attachment properly. Here's a snipped of my little routine. Works like
a champ, and I didn't have to install ActionMailer or any other module
since I cobbled the code together manually. It will fit the bill!

Code is below if anyone runs into a similar quandary.

-----------------------------------

# Read a file and encode it into base64 format
savedDoc = path+filename
file = File.open(savedDoc, 'rb')
filecontent = file.read()
encodedcontent = [filecontent].pack("m*") # base64

marker = "AUNIQUEMARKER"

body =<<EOF
Attached is your Excel spreadsheet!
EOF

# Define the main headers.
part1 =<<EOF
From: Me <>
To: You <>
Subject: Message with Excel Attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{marker}
--#{marker}
EOF

# Define the message action
part2 =<<EOF
Content-Type: text/plain
Content-Transfer-Encoding: 8bit

#{body}
--#{marker}
EOF

# Define the attachment section
part3 =<<EOF
Content-Type: application/vnd.ms-excel; name=\"#{filename}\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="#{filename}"
Content-Description: "#{filename}"

#{encodedcontent}
--#{marker}--
EOF

mailtext = part1 + part2 + part3

# Let's put our code in safe area
begin
Net::SMTP.start(server=localhost, port=25) do |smtp|
smtp.sendmail(mailtext, '', '')
end
rescue Exception => e
print "Exception occured: " + e
end
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Excel reports ::::Excel 2003 Migration To Excel 2007 =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?= ASP .Net 15 10-24-2007 01:34 PM
adding an excel worksheet to a generetad excel file via perl FlorianKraft@gmx.de Perl Misc 1 04-17-2007 12:53 PM
Trying to send Excel attachment; the operation failed due to network or other communication problems Edward Computer Support 5 03-24-2007 10:52 PM
excel file attachment =?Utf-8?B?RGFuaWVs?= ASP .Net 3 08-18-2005 01:33 PM
sending an excel attachment with MIME::Lite p cooper Perl Misc 1 01-31-2004 12:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57