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

Tuesday, August 15, 2006

Locate Technology on the Map

Did some playing around with google maps API, searches for job listings and puts cities where technology is found on the map(works only for single words for now, like Ruby/C etc.)
Compare Ruby and C# for example... who would have tought, lots of C# in Seatle...
Also it doesn't work quite right where there are hundreds of matches like Java(I got tired of the HTML parsing....), might get back to it at some point.
http://maps.maanpaa.com/
Ville Maanpaa

Thursday, August 03, 2006

Ruby script to send email with authentication and attachments

Here's a little script I wrote to send email in Ruby with smtp authentication and attachments, it needs tmail(included in Rails). It could obviously use more polish, I wrote it based on some samples in tmail, unfortunately the tmail docs are little lacking. Since I wrote it on linux/mac, moving it to windows was little tricky because Tmail ruby library includes c-code that needs to be compiled. However the rails package active_mailer includes tmail (I am not quite sure if it is exact same version as on the Tmail page), so I just had to figure out to require rubygems and active_mailer on windows in addition to Tmail.
Also as usual I got stuck for a while with the attachments on windows, since as usual I forgot the idiotic behavior on windows where attachment files such as docs or images need to be opened with "rb" flags to indicate binary file(not necessary on Unix like Linux and Mac), otherwise it won't error, but it will just truncate the file.

Now this can handle arbitrary number of attachments and you can put in toList things like in
#--tolist="toListProd.txt"
cc:
bcc
#and commments

Anyway here is the version that works on windows, for linux just remove the items mentioned above:
http://maanpaa.blogspot.com/2006/08/ruby-email-with-authentication-and.html

Ajax

Since I wrote about GUI toolkits it really seems world is further and further moving towards the web. I believe thick GUIs still have their place, where super fast performance like what wxwidgets can offer is required, however you can do lot with Ajax now, here's a little demo I wrote using Ruby Rails and prototype libraries, it has movable windows, drag-drop items, "push" updates, in-place edits, on the fly stylesheet changing etc.
Here's a screenshot:

This link may or may not work because it's simply running at my house on anold laptop...
http://nexavibes.maanpaa.com