Programmatically adding an Excel attachment

Discussion in 'Ruby' started by gregarican, Jan 15, 2009.

  1. gregarican

    gregarican Guest

    I've checked out this page --> http://www.tutorialspoint.com/ruby/ruby_sending_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?
     
    gregarican, Jan 15, 2009
    #1
    1. Advertising

  2. gregarican

    gregarican Guest

    On Jan 15, 4:23 pm, gregarican <> wrote:
    > I've checked out this page -->http://www.tutorialspoint.com/ruby/ruby_sending_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
     
    gregarican, Jan 16, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raoul Snyman
    Replies:
    14
    Views:
    11,102
    Curt_C [MVP]
    Apr 29, 2004
  2. =?Utf-8?B?RGFuaWVs?=

    excel file attachment

    =?Utf-8?B?RGFuaWVs?=, Aug 18, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    408
    =?Utf-8?B?RGFuaWVs?=
    Aug 18, 2005
  3. =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=

    Problem with Excel reports ::::Excel 2003 Migration To Excel 2007

    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=, Oct 5, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    1,525
    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=
    Oct 24, 2007
  4. p cooper
    Replies:
    1
    Views:
    838
    Ben Morrow
    Jan 31, 2004
  5. Replies:
    1
    Views:
    297
    Mirco Wahab
    Apr 17, 2007
Loading...

Share This Page