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-5 of 5 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

Simplifying ActionMailer development in Ruby on Rails

Sick of writing almost the same thing over and over in your ActionMailer classes? Skip all that, and use something like this.

   1  
   2  class Mailer < ActionMailer::Base
   3  
   4    helper ActionView::Helpers::UrlHelper
   5  
   6    def generic_mailer(options)
   7      @recipients = options[:recipients] || "me@privacy.net"
   8      @from = options[:from] || "me@privacy.net"
   9      @cc = options[:cc] || ""
  10      @bcc = options[:bcc] || ""
  11      @subject = options[:subject] || ""
  12      @body = options[:body] || {}
  13      @headers = options[:headers] || {}
  14      @charset = options[:charset] || "utf-8"
  15    end
  16    
  17    # Create placeholders for whichever e-mails you need to deal with.
  18    # Override mail elements where necessary
  19    
  20    def contact_us(options)
  21      self.generic_mailer(options)
  22    end
  23  
  24    ...
  25  
  26  end


(If you have a configuration loaded into a constant, you could just replace the defaults above and use your app's defaults to make it all cleaner, of course)

And then from your controller you can do stuff like this:

   1  Mailer.deliver_contact_us(
   2     :recipients => "x@x.com",
   3     :body => { 
   4                 :name => params[:name],
   5                 :phone => params[:phone],
   6                 :email => params[:email],
   7                 :message => params[:message]
   8               },
   9     :from => "y@y.com"
  10  )

Mail a file as an attachment from the UNIX prompt

   1  uuencode file.txt file.txt | mail email@address.com

Atom / RSS feed of your GMail account

   1  https://USERNAME:PASSWORD@gmail.google.com/gmail/feed/atom
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS