Trying to send an email with attachments

Discussion in 'Ruby' started by grooveska, May 31, 2007.

  1. grooveska

    grooveska Guest

    I am working on a script that will send an email with a .csv file
    attachment. I can get everything set correctly, and the email gets
    sent properly. The problem is with the attachments. This maybe a MIME
    thing instead of something wrong with my script. My script is based
    on an example from Hal Fulton's The Ruby Way book. If anyone has any
    suggestions or see something I did wrong I would greatly appreciate
    it. Below is my script. Below that is what the email I receive looks
    like.

    Thanks!

    ***Begin Script***

    require 'time'
    require 'net/smtp'
    require 'date'

    ##################################################################
    # This portion of the code assembles the filenames of the files #
    # to be attached in the email #
    ##################################################################

    time = Time.now #set time object to the current time
    month = time.month #grab the number of the month from the
    current time
    day = time.day #grab the day of the current time
    year = time.year #grab the year of the current time

    month = month + 1 #to match the format of Ian's file names

    if month < 10 #formatting
    fullmonth = "0" + month.to_s
    else
    fullmonth = month.to_s
    end

    if day < 10
    fullday = "0" + day.to_s
    else
    fullday = day.to_s
    end #end of formatting

    filename1 = "pagecount-" + year.to_s + fullmonth + fullday + ".csv"
    #assemble file name

    #####################################
    # Attach files and send the email #
    ####################################

    def text_plus_attachment (subject, body, filename)
    marker = "MIME_boundary"
    middle = "--#{marker}\n"
    ending = "--#{middle}--\n"
    content = "Content-Type: Multipart/Related; " + "boundary=#{marker};
    " + "typw=text/plain"
    head1 = <<-EOF
    MIME-Version: 1.0
    #{content}
    Subject: #{subject}
    EOF
    binary = File.read(filename)
    encoded = [binary].pack("u") # base64 econding
    head2 = <<-EOF
    Content-Description: "#{filename}"
    Content-Type: text/plain; name="#{filename}"
    Content-Transfer-Encoding: Uencode
    Content-Disposition: attachment; filename="#{filename}"

    EOF

    #Return

    head1 + middle + body + middle + head2 + encoded + ending

    end

    body = <<EOF
    The GFIFax Monthly report is attached.
    EOF

    mailtext = text_plus_attachment("GFIFax Service Monthly Report", body,
    filename1)

    Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
    Net::SMTP.start('smtp.umr.edu', '25', 'umr.edu', '',
    'UMRf@xit1', :plain) do |smtp|
    smtp.sendmail(mailtext, '', [''])
    end

    ***End Script***

    ***Begin Email results*** (This is all in the body of the email
    instead of in the headers)

    MIME-Version: 1.0
    Content-Type: Multipart/Related; boundary=MIME_boundary; typw=text/
    plain
    Subject: GFIFax Service Monthly Report --MIME_boundary The GFIFax
    Monthly report is attached.
    --MIME_boundary
    Content-Description: "pagecount-20070631.csv"
    Content-Type: text/plain; name="pagecount-20070631.csv"
    Content-Transfer-Encoding: Uencode
    Content-Disposition: attachment; filename="pagecount-20070631.csv"

    M4V5N="!&87AE<RPL+`I5<V5R240L(%!A9V5S(%-E;G0L($QI;6ET+"!/=F5R
    M86=E"C4W,S(P,C(Q,30L,BPU,"PP"C4W,S(P,C(Q,34L-"PU,"PP"C4W,S,T
    M,3<X-#8L,2PU,"PP"@H*4F5C96EV960@1F%X97,L+"P*57-E<DE$+%!A9V5S
    M(%)E8V5I=F5D+"!,:6UI="P@3W9E<F%G90HU-S,S-#$V,C,S+#$R+#$P,"PP
    M"C4W,S(P,C(Q,30L,RPQ,#`L,`HU-S,S-#$T.#@Y+#(Q.2PQ,#`L,3$Y"C4W
    M,S,T,38R-S$L.#@L.3DY.2PP"C4W,S(P,C(S.3(L,C`U+#DY.3DL,`HU-S,R
    E,#(R,SDW+#4T+#DY.3DL,`HU-S,R,#(R,SDV+#$Q+#$P,"PP"@``
    ----MIME_boundary
    --

    **End Email**'

    I hope someone can help me out!

    Thanks!
    grooveska, May 31, 2007
    #1
    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. Aleksei Pashin

    Send email message with attachments

    Aleksei Pashin, Oct 12, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    444
    Aleksei Pashin
    Oct 12, 2004
  2. Aleksei Pashin

    Send email message with attachments

    Aleksei Pashin, Oct 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    414
    Schalkwi
    Dec 9, 2005
  3. Music Lover
    Replies:
    5
    Views:
    1,929
    Music Lover
    May 12, 2005
  4. Andrew Thompson
    Replies:
    0
    Views:
    481
    Andrew Thompson
    Dec 7, 2006
  5. grooveska
    Replies:
    0
    Views:
    88
    grooveska
    May 31, 2007
Loading...

Share This Page