views:

57

answers:

2

I need to send an email with an attachment via Ruby.

Been searching around but haven't found any simple script example to do this.

Closest I've found is ActionMailer but that seems to require a bunch of other scripts to to run. (NOTE: I am not using Ruby on Rails)

A: 

See this ActionMailer tutorial.

1st result for Google: actionmailer tutorial.

mcandre
+1  A: 

Have you checked out Mail? That is what the new ActionMailer API in Rails 3 is built upon.

"Mail is an internet library for Ruby that is designed to handle emails generation, parsing and sending in a simple, rubyesque manner."

Here comes a quick example from the docs:

require 'mail'

@mail = Mail.new
file_data = File.read('path/to/myfile.pdf')
@mail.attachments['myfile.pdf'] = { :mime_type => 'application/x-pdf',
                                  :content => File.read('path/to/myfile.pdf') }

Update: Or even more simply:

@mail = Mail.new
@mail.add_file("/path/to/file.jpg")
Daniel Abrahamsson