lib/ronin/network/smtp/email.rb in ronin-support-0.1.0 vs lib/ronin/network/smtp/email.rb in ronin-support-0.2.0.rc1
- old
+ new
@@ -23,10 +23,12 @@
#
# Represents an Email to be sent over {SMTP}.
#
class Email
+ CRLF = "\n\r"
+
# Sender of the email
attr_accessor :from
# Recipient of the email
attr_accessor :to
@@ -38,10 +40,13 @@
attr_accessor :date
# Unique message-id string
attr_accessor :message_id
+ # Additional headers
+ attr_reader :headers
+
# Body of the email
attr_accessor :body
#
# Creates a new Email object.
@@ -50,11 +55,11 @@
# Additional options.
#
# @option options [String] :from
# The address the email is from.
#
- # @option options [String] :to
+ # @option options [Array, String] :to
# The address that the email should be sent to.
#
# @option options [String] :subject
# The subject of the email.
#
@@ -62,32 +67,46 @@
# Message-ID of the email.
#
# @option options [String, Time] :date (Time.now)
# The date the email was sent on.
#
+ # @option options [Hash<String => String}] :headers
+ # Additional headers.
+ #
# @option options [String, Array<String>] :body
# The body of the email.
#
# @yield [email]
# If a block is given, it will be passed the newly created email
# object.
#
# @yieldparam [Email] email
# The newly created email object.
#
+ # @api public
+ #
def initialize(options={})
@from = options[:from]
@to = options[:to]
@subject = options[:subject]
- @date = options[:date] || Time.now
+ @date = options.fetch(:date,Time.now)
@message_id = options[:message_id]
+ @headers = {}
+
+ if options[:headers]
+ @headers.merge!(options[:headers])
+ end
+
@body = []
- if options[:body].kind_of?(Array)
- @body += options[:body]
- else
- @body << options[:body]
+ if options[:body]
+ case options[:body]
+ when Array
+ @body += options[:body]
+ else
+ @body << options[:body]
+ end
end
yield self if block_given?
end
@@ -97,29 +116,35 @@
# @return [String]
# Properly formatted SMTP message.
#
# @see http://www.ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/classes/Net/SMTP.html
#
+ # @api public
+ #
def to_s
address = lambda { |info|
- if info.kind_of?(Array)
- return "#{info[0]} <#{info[1]}>"
- elsif info.kind_of?(Hash)
- return "#{info[:name]} <#{info[:email]}>"
+ case info
+ when Array
+ "#{info[0]} <#{info[1]}>"
else
- return info
+ info
end
}
message = []
if @from
- message << "From: #{address.call(@from)}"
+ message << "From: #{@from}"
end
if @to
- message << "To: #{address.call(@to)}"
+ message << case @to
+ when Array
+ "To: #{@to.join(', ')}"
+ else
+ "To: #{@to}"
+ end
end
if @subject
message << "Subject: #{@subject}"
end
@@ -130,13 +155,17 @@
if @message_id
message << "Message-Id: <#{@message_id}>"
end
+ @headers.each do |name,value|
+ message << "#{name}: #{value}"
+ end
+
message << ''
message += @body
- return message.join("\n")
+ return message.join(CRLF)
end
end
end
end