Guest User

Untitled

a guest
Feb 28th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. require 'net/smtp'
  2. require 'mailfactory'
  3. module Merb
  4. class Mailer
  5.  
  6. shared_accessor :config
  7.  
  8. def sendmail
  9. sendmail = IO.popen("sendmail #{@mail.to}", 'w+')
  10. sendmail.puts @mail.to_s
  11. sendmail.close
  12. end
  13.  
  14. # :plain, :login, or :cram_md5
  15. def net_smtp
  16. Net::SMTP.start(config[:host], config[:port].to_i, config[:domain],
  17. config[:user], config[:pass], (config[:auth].intern||:plain)) { |smtp|
  18. smtp.send_message(@mail.to_s(), @mail.from, @mail.to)
  19. }
  20. end
  21.  
  22. def deliver!
  23. config == :sendmail ? sendmail : net_smtp
  24. end
  25.  
  26. def initialize(o={})
  27. self.config = :sendmail if config.nil?
  28. @mail = returning MailFactory.new() do |m|
  29. m.to = o[:to]
  30. m.from = o[:from]
  31. m.subject = o[:subject]
  32. m.text = o[:body]
  33. m.html = o[:html] if o[:html]
  34. end
  35. end
  36.  
  37. end
  38. end
  39.  
  40. m = Merb::Mailer.new :to => 'foo@bar.com',
  41. :from => 'bar@foo.com',
  42. :subject => 'Welcome to whatever!',
  43. :body => partial(:sometemplate)
  44. m.deliver!
  45.  
  46. Merb::Mailer.config = {
  47. :host=>'smtp.yourserver.com',
  48. :port=>'25',
  49. :user=>'user',
  50. :pass=>'pass',
  51. :auth=>:plain # :plain, :login, or :cram_md5, default :plain
  52. }
Add Comment
Please, Sign In to add comment