Guest User

Untitled

a guest
Mar 8th, 2018
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. begin
  2. require 'mailfactory'
  3. require 'net/smtp'
  4. rescue LoadError
  5. puts "You need to install the mailfactory gem to use Merb::Mailer"
  6. MERB_LOGGER.warn "You need to install the mailfactory gem to use Merb::Mailer"
  7. end
  8.  
  9. class MailFactory
  10. attr_reader :html, :text
  11. end
  12.  
  13. module Merb
  14.  
  15. # You'll need a simple config like this in merb_init.rb if you want
  16. # to actually send mail:
  17. #
  18. # Merb::Mailer.config = {
  19. # :host=>'smtp.yourserver.com',
  20. # :port=>'25',
  21. # :user=>'user',
  22. # :pass=>'pass',
  23. # :auth=>:plain # :plain, :login, or :cram_md5, default :plain
  24. # }
  25. # Merb::Mailer.delivery_method = :sendmail
  26. #
  27. # You could send mail manually like this (but it's better to use
  28. # a MailController instead).
  29. #
  30. # m = Merb::Mailer.new :to => 'foo@bar.com',
  31. # :from => 'bar@foo.com',
  32. # :subject => 'Welcome to whatever!',
  33. # :body => partial(:sometemplate)
  34. # m.deliver!
  35.  
  36. class Mailer
  37.  
  38. class_inheritable_accessor :config, :delivery_method, :deliveries
  39. attr_accessor :mail
  40. self.deliveries = []
  41.  
  42. def sendmail
  43. sendmail = IO.popen("sendmail #{@mail.to}", 'w+')
  44. sendmail.puts @mail.to_s
  45. sendmail.close
  46. end
  47.  
  48. # :plain, :login, or :cram_md5
  49. def net_smtp
  50. Net::SMTP.start(config[:host], config[:port].to_i, config[:domain],
  51. config[:user], config[:pass], (config[:auth].to_sym||:plain)) { |smtp|
  52. smtp.send_message(@mail.to_s, @mail.from.first, @mail.to)
  53. }
  54. end
  55.  
  56. def test_send
  57. deliveries << @mail
  58. end
  59.  
  60. def deliver!
  61. send(delivery_method || :net_smtp)
  62. end
  63.  
  64. def attach(file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
  65. type = nil, headers = nil)
  66. if file_or_files.is_a?(Array)
  67. file_or_files.each {|k,v| @mail.add_attachment_as k, *v}
  68. else
  69. raise ArgumentError, "You did not pass in a file. Instead, you sent a #{file_or_files.class}" if !file_or_files.is_a?(File)
  70. @mail.add_attachment_as(file_or_files, filename, type, headers)
  71. end
  72. end
  73.  
  74. def initialize(o={})
  75. self.config = :sendmail if config.nil?
  76. o[:rawhtml] = o.delete(:html)
  77. m = MailFactory.new()
  78. o.each { |k,v| m.send "#{k}=", v }
  79. @mail = m
  80. end
  81.  
  82. end
  83. end
Add Comment
Please, Sign In to add comment