From: Reid Thompson Date: 2009-02-23T01:49:56+09:00 Subject: Re: how do you add attachments in tmail Adam Akhtar wrote: > anyone? Ive been googling like mad but cant find anything. It must be > able to support this. > > > > may be of use http://maanpaa.blogspot.com/2006/08/ruby-email-with-authentication-and.html Ville Maanpaa on weekdays thoughts on coding, Ruby etc. Tuesday, August 22, 2006 Ruby email with authentication and attachments #!/usr/bin/ruby # # smtp_auth.rb # Ville Maanpaa - email with authentication including attachments # This is the Linux version - on windows add gems and use tmail in Rails directory # require 'base64' require 'tmail' require 'net/smtp' require 'getopts' def usage( status, msg = nil ) output = (status == 0 ? $stdout : $stderr) output.puts msg if msg output.print(< Usage: #{File.basename $0} [-s ] [-f ] --subject="release notes" --tolist="toListProd.txt" --data2="errors" --attach1="ReleaseNotes_Servers.doc" --attach2="ReleaseNotes_Clients.doc" --attach3="ReleaseNotes_Web. -h,--host=addr SMTP server address. (default=imap.nexatech.com) -s,--subject=sbj subject of the message. (default=(none)) -f,--from=from from address. (default=usernmae) EOS exit status end def main attachments=[] to=[] ARGV.each do |a| if a.index("attach") attachments << a.split("=")[1] elsif a.index("@") to << a end end ARGV.delete_if{|x|x.index("attach")} getopts(nil, 'from:', 'f:', 'tolist:', 'subject:', 'data2:') smtphost = $OPT_host || $OPT_h || 'smtp.server.com' subject = $OPT_subject || $OPT_s data2="" if $OPT_data2 if FileTest.exists?($OPT_data2) File.open($OPT_data2, "r") do |f| f.each_line { |line| data2 << line } end end end tolist = $OPT_tolist || 'tolist.txt' from = $OPT_from || $OPT_f || 'your@email.com' usage(1, 'Sender address not given') unless from if FileTest.exists?(tolist) File.open(tolist, "r") do |f| f.each_line { |line| to << line } end end usage(1, 'Receipt address(es) not given') if to.empty? send_mail smtphost, setup_mail(from, to, subject, data2, attachments) end def setup_mail( from, to, subject, body, attachments) mail = TMail::Mail.new mail.date = Time.now mail.from = from if to.class.to_s.index("Array") #this is where we read toList.txt file toarr=[] bcc=[] cc=[] to.each do |addr| if addr.index("#") #this is a comment elsif addr.index("bcc:") bcc << addr[4..-1] mail.bcc=bcc elsif addr.index("cc:") cc << addr[3..-1] mail.cc=cc else toarr< mail.to=toarr end end else mail.to = to end mail.subject = subject if subject mail.mime_version = '1.0' mail.set_content_type 'multipart', 'mixed' mailpart1=TMail::Mail.new mailpart1.body = body mailpart1.set_content_type 'text', 'plain' mail.parts << mailpart1 attachments.each do |att| if FileTest.exists?(att) content=IO::readlines(att, nil) mailpart=TMail::Mail.new mailpart.body = Base64.encode64(content.to_s) mailpart.transfer_encoding="Base64" mailpart['Content-Disposition'] = "attachment; filename=#{att}" mail.parts << mailpart end end mail end def send_mail( host, mail ) msg = mail.encoded $stderr.print msg if $DEBUG smtp = Net::SMTP.new(host, 25) smtp.set_debug_output $stderr if $DEBUG smtp.start('your.serve.com', 'username', 'password', :login) { #CHANGE THESE TO FIT YOUR ENV smtp.send_mail msg, mail.from_address, mail.destinations } end main posted by Ville at 11:21 AM 0 Comments: Post a Comment << Home About Me Name: Ville Location: United States Minor Outlying Islands Blogging on Programming and Dana Point for now View my complete profile Previous Posts * Locate Technology on the Map * Ruby script to send email with authentication and ... * Ajax * programming - GUI toolkits Powered by Blogger