May I suggest you use Pony?
It is an added dependency but it makes creating emails and emails with
attachments much easier.
I have below a program I've made that is very modular in that you pass
in..
- a email server address
- a senders email address
from this you can call one of 2 methods to dispatch emails.
- Emails with no attachments
- Emails with attachments
I know these could be put into 1 but i've not got round to doing that.
For what it's worth the code is below....
require 'pony'
module Sendemail
# call this 1st to setup the email server and senders
# settings
#
def initial(server,address)
@loginfo.info('setup email settings NOW...')
@server = server
@address = address
end
# this to send just an email
#
def send_email(from, to, subject, msg)
@loginfo.info('sending an email - No Attachment sent !')
Pony.mail(:to => "#{to}", :from => "#{from}", :subject =>
"#{subject}", :body => "#{msg}", :via => :smtp, :via_options => {
:address => @server,
:port => @address})
@logbugs.debug("\nserver = #{@server}\nport used = #{@address}\nto
= #{to}\nfrom = #from}\nsubject = #{subject}\nmsg = #{msg}")
end
#
this to send with an attachment
# Know the method and this one could be merged into one
# I just haven't got round to it.
#
def email_attach(from, to, subject, msg,attachment,file_path)
@loginfo.info('sending an email - Attachment sent !')
# these 2 lines are so I can create a PDF via Prawn and add it as
# the email - disregard these 2 lines
#
@table.pdf_setup filename,path,msg
@table.tables
Pony.mail(:to => "#{to}", :from => "#{from}", :subject =>
"#{subject}", :body => "#{msg}", :via => :smtp, :via_options => {
:address => @server,
:port => @address},
:attachments => {"#{attachment}" => File.read("#{file_path}")})
@logbugs.debug("\nserver = #{@server}\nport used = #{@address}\nto
= #{to}\nfrom = #from}\nsubject = #{subject}\n
msg = #{msg}\nfilename = #{file_path}")
end
end
--
Posted via http://www.ruby-forum.com/.