Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Peter Cooperx http://www.petercooper.co.uk/

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Ultra-simplistic Ruby SMTP server

You'll know if you need this, otherwise steer clear ;-)

   1  require 'gserver'
   2  
   3  class SMTPServer < GServer
   4    def serve(io)
   5      @data_mode = false
   6      puts "Connected"
   7      io.print "220 hello\r\n"
   8      loop do
   9        if IO.select([io], nil, nil, 0.1)
  10  	      data = io.readpartial(4096)
  11  	      puts ">>" + data
  12  	      ok, op = process_line(data)
  13  	      break unless ok
  14  	      io.print op
  15        end
  16        break if io.closed?
  17      end
  18      io.print "221 bye\r\n"
  19      io.close
  20    end
  21  
  22    def process_line(line)
  23      if (line =~ /^(HELO|EHLO)/)
  24        return true, "220 and..?\r\n"
  25      end
  26      if (line =~ /^QUIT/)
  27        return false, "bye\r\n"
  28      end
  29      if (line =~ /^MAIL FROM\:/)
  30        return true, "220 OK\r\n"
  31      end
  32      if (line =~ /^RCPT TO\:/)
  33        return true, "220 OK\r\n"
  34      end
  35      if (line =~ /^DATA/)
  36        @data_mode = true
  37        return true, "354 Enter message, ending with \".\" on a line by itself\r\n"
  38      end
  39      if (@data_mode) && (line.chomp =~ /^.$/)
  40        @data_mode = false
  41        return true, "220 OK\r\n"
  42      end
  43      if @data_mode
  44        puts line 
  45        return true, ""
  46      else
  47        return true, "500 ERROR\r\n"
  48      end
  49    end
  50  end
  51  
  52  a = SMTPServer.new(1234)
  53  a.start
  54  a.join

Simple "Send Email from Ruby" Method

By Ian Purton and found at http://jiploo.com/blog/simple-email-send-function-in-ruby/

   1  
   2  def send_email(from, from_alias, to, to_alias, subject, message)
   3  	msg = <<END_OF_MESSAGE
   4  From: #{from_alias} <#{from}>
   5  To: #{to_alias} <#{to}>
   6  Subject: #{subject}
   7  	
   8  #{message}
   9  END_OF_MESSAGE
  10  	
  11  	Net::SMTP.start('localhost') do |smtp|
  12  		smtp.send_message msg, from, to
  13  	end
  14  end

Mail a file as an attachment from the UNIX prompt

   1  uuencode file.txt file.txt | mail email@address.com
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS