Sha256: 47737f763f8e03be389727a41494d075ae34afa97ac1aa64aa41fa8dcfcda52a

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

class Knj::Mailobj
	def initialize(args = {})
		@args = {
			"smtp_host" => "localhost",
			"smtp_port" => 25,
			"smtp_user" => nil,
			"smtp_passwd" => nil,
			"smtp_domain" => ENV["HOSTNAME"]
		}
		
		if args.is_a?(Hash)
			args.each do |key, value|
				@args[key] = value
			end
		end
		
		self.send if @args["send"]
	end
	
	def html=(value)
		@args["html"] = value
	end
	
	def text=(value)
		@args["text"] = value
	end
	
	def from=(value)
		@args["from"] = value
	end
	
	def subject=(value)
		@args["subject"] = value
	end
	
	def to=(value)
		@args["to"] = value.untaint
	end
	
	def send
		raise "No email has been defined to send to." if !@args["to"]
		raise "No subject has been defined." if !@args["subject"]
		raise "No content has been defined." if !@args["text"] and !@args["html"]
		
		require "mail"
		
		mail = Mail.new
		mail.to = @args["to"]
		mail.subject = @args["subject"]
		mail.date = Time.new
		mail.from = @args["from"] if @args["from"]
		
		if @args["html"]
			tha_html = @args["html"]
			mail.html_part do
				content_type "text/html; charset=UTF-8"
				body tha_html
			end
		elsif @args["text"]
			tha_text = @args["text"]
			mail.text_part do
				body tha_text
			end
		end
		
		smtp_start = Net::SMTP.new(@args["smtp_host"], @args["smtp_port"])
		smtp_start.enable_ssl if @args["ssl"]
		smtp_start.enable_starttls if @args["tls"]
		
		if !@args["smtp_domain"]
			if @args["smtp_host"]
				@args["smtp_domain"] = @args["smtp_host"]
			else
				raise "SMTP domain not given."
			end
		end
		
		smtp_start.start(@args["smtp_domain"], @args["smtp_user"], @args["smtp_passwd"]) do |smtp|
			smtp.send_message(mail.to_s, @args["from"], @args["to"])
		end
	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
knjrbfw-0.0.8 lib/knj/mailobj.rb
knjrbfw-0.0.7 lib/knj/mailobj.rb
knjrbfw-0.0.4 lib/knj/mailobj.rb
knjrbfw-0.0.3 lib/knj/mailobj.rb