Send email with attachments in Ruby

Discussion in 'Ruby' started by loominator1970, Sep 3, 2008.

  1. I'm trying to find some code to send an email with attachment in
    ruby. I'm using the following code to send an email, but I would like
    to send an PDF. Can someone please tell me what code i need to add to
    include a file called "the_attachment.pdf"? Thanks, Dave

    ## send email
    require 'net/smtp'

    # set up the email addresses
    user_from = ""
    user_to = ""

    the_email = "From: \n" +
    "Subject: Email with attachment\n\n" +
    "See attached PDF.\n\n"

    # handling exceptions
    begin
    Net::SMTP.start('localhost') do |smtp|
    smtp.send_message(the_email, user_from, user_to)
    end

    rescue Exception => e
    print "Exception occured: " + e
    end
     
    loominator1970, Sep 3, 2008
    #1
    1. Advertising

  2. Lloyd Linklater, Sep 3, 2008
    #2
    1. Advertising

  3. Hi,


    Am Donnerstag, 04. Sep 2008, 02:09:27 +0900 schrieb loominator1970:
    > I'm trying to find some code to send an email with attachment in
    > ruby. I'm using the following code to send an email, but I would like
    > to send an PDF. Can someone please tell me what code i need to add to
    > include a file called "the_attachment.pdf"? Thanks, Dave


    Two years ago I started to write my own mail library. Initially I
    only thought of filtering mails. Soon I started to generate mails,
    too. In the meantime I filtered about 100,000 mails and I generated
    about 25,000.

    If you like to have a look at it, please do something like:

    gem fetch --source http://bertram-scharpf.homelinux.com/bs_gems/ cropmail
    fetch http://bertram-scharpf.homelinux.com/bs_gems/cropmail-1.6.gem

    It isn't documented very well because nobody uses it but me, but
    it is thoroughly tested and it is very easy to use.

    Below I will cite some sample code how I generate a mail.

    I would be pleased if you like it. In case you don't I wish you
    good luck anyway.


    Bertram


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

    require "bs-net/mail"
    require "stringio"

    m = BsNet::Mail.new
    m.headers.add :from, ""
    m.headers.add :to, ""
    m.headers.add :bcc, %w( )
    m.headers.add :date, Time.now.rfc822
    m.headers.add :subject, "Some useless information"

    plain = BsNet::Message.new
    ct = BsNet::ContentType.new "text/plain", :charset => "utf-8"
    plain.headers.add :content_type, ct
    plain.headers.add :content_transfer_encoding, "8bit"
    StringIo_Open plain.body do |b|
    b.puts "This mail is coming with an attachment."
    end
    m.push plain

    attachment = BsNet::Message.new
    ct = BsNet::ContentType.new "text/comma-separated-values",
    :charset => "utf-8", :name => "somefile.csv"
    attachment.headers.add :content_type, ct
    attachment.headers.add :content_transfer_encoding, "8bit"
    cf = BsNet::ContentField.new "inline", :filename => "somefile.csv"
    attachment.headers.add :content_disposition, cf
    StringIO.new attachment.body do |b|
    b.puts somedata
    end
    m.push attachment

    m.sendmail


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Sep 3, 2008
    #3
  4. loominator1970

    Dave Baldwin Guest

    On 3 Sep 2008, at 18:09, loominator1970 wrote:

    > I'm trying to find some code to send an email with attachment in
    > ruby. I'm using the following code to send an email, but I would like
    > to send an PDF. Can someone please tell me what code i need to add to
    > include a file called "the_attachment.pdf"? Thanks, Dave



    I use this:


    require 'rubygems'
    require 'action_mailer'
    require 'mime/types'

    ActionMailer::Base.smtp_settings = { :address =>
    '10.209.3.26', :domain => '3dlabs.com'}

    class Mailer < ActionMailer::Base
    def message (title, body)
    from 'Dave Baldwin <>'
    recipients ''
    subject title
    body body

    # Include all the pdf files in the PDF subdirectory as attachments.
    FileList['PDF/*.pdf'].each do |path|
    file = File.basename(path)
    mime_type = MIME::Types.of(file).first
    content_type = mime_type ? mime_type.content_type : 'application/
    binary'
    attachment (content_type) do |a|
    a.body = File.read(path)
    a.filename = file
    a.transfer_encoding = 'quoted-printable' if content_type =~ /^text
    \//
    end
    end
    end
    end

    Mailer.deliver_message('some title', 'the body message')


    Dave.
     
    Dave Baldwin, Sep 4, 2008
    #4
  5. I don't think there is an easy way to do so with net/smtp, put
    actionmailer can handle attacments, I believe.
     
    Slartibartfast, Sep 4, 2008
    #5
  6. In article <48bfdeb2$0$23194$>,
    Bob Schäfer <> wrote:
    >if there's a *ruby way* to do this, i'd love to know what it is. i
    >always feel a little dirty doing it like this.


    There is no easy way to play with this :(

    You could try SimpleMail (http://simplemail.rubyforge.org/doc/) from
    RubyForge.

    rmail has it on its TODO for years :(

    Yet another are where Ruby is lacking compared to other languages...
    --
    Ollivier ROBERT -=- EEC/RIF/SEU -=-
    Systems Engineering Unit
     
    Ollivier Robert, Sep 5, 2008
    #6
  7. Here's updated code using sendmail and Dir.glob instead of FileList:

    require 'rubygems'
    require 'action_mailer'
    require 'mime/types'

    ActionMailer::Base.delivery_method = :sendmail
    ActionMailer::Base.default_content_type = "text/plain"

    class Mailer < ActionMailer::Base
    def message (from, to, title, body, attachments)
    from from
    recipients to
    subject title
    body body

    Dir.glob(attachments).each do |path|
    file = File.basename(path)
    mime_type = MIME::Types.of(file).first
    content_type = mime_type ? mime_type.content_type :
    'application/binary'
    attachment (content_type) do |a|
    a.body = File.read(path)
    a.filename = file
    a.transfer_encoding = 'quoted-printable' if content_type =~
    /^text\//
    end
    end
    end
    end

    Mailer.deliver_message 'Christopher Pickslay <>',
    '',
    'Some reports',
    "See attached tab-delimited reports\n\n",
    '/tmp/reports/*.txt'


    Dave Baldwin wrote:
    > On 3 Sep 2008, at 18:09, loominator1970 wrote:
    >
    >> I'm trying to find some code to send an email with attachment in
    >> ruby. I'm using the following code to send an email, but I would like
    >> to send an PDF. Can someone please tell me what code i need to add to
    >> include a file called "the_attachment.pdf"? Thanks, Dave

    >
    >
    > I use this:
    >
    >
    > require 'rubygems'
    > require 'action_mailer'
    > require 'mime/types'
    >
    > ActionMailer::Base.smtp_settings = { :address =>
    > '10.209.3.26', :domain => '3dlabs.com'}
    >
    > class Mailer < ActionMailer::Base
    > def message (title, body)
    > from 'Dave Baldwin <>'
    > recipients ''
    > subject title
    > body body
    >
    > # Include all the pdf files in the PDF subdirectory as attachments.
    > FileList['PDF/*.pdf'].each do |path|
    > file = File.basename(path)
    > mime_type = MIME::Types.of(file).first
    > content_type = mime_type ? mime_type.content_type : 'application/
    > binary'
    > attachment (content_type) do |a|
    > a.body = File.read(path)
    > a.filename = file
    > a.transfer_encoding = 'quoted-printable' if content_type =~
    > /^text
    > \//
    > end
    > end
    > end
    > end
    >
    > Mailer.deliver_message('some title', 'the body message')
    >
    >
    > Dave.


    --
    Posted via http://www.ruby-forum.com/.
     
    Christopher Pickslay, Sep 21, 2010
    #7
    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:
    453
    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:
    422
    Schalkwi
    Dec 9, 2005
  3. Music Lover
    Replies:
    5
    Views:
    1,952
    Music Lover
    May 12, 2005
  4. Andrew Thompson
    Replies:
    0
    Views:
    496
    Andrew Thompson
    Dec 7, 2006
  5. rtilley

    ruby to send email attachments

    rtilley, Apr 2, 2006, in forum: Ruby
    Replies:
    2
    Views:
    359
    Dimitri Aivaliotis
    Apr 3, 2006
Loading...

Share This Page