def new_mail_from_input(text)
require 'mail'
mail = Mail.new
raw_headers, raw_body = *text.split(/\n\s*\n/, 2)
headers = {}
raw_headers.split("\n").each do |line|
key, value = *line.split(/:\s*/, 2)
log [key, value].join(':')
if %w(from to cc bcc).include?(key)
value = quote_addresses(value)
end
headers[key] = value
end
log "delivering message with headers: #{headers.to_yaml}"
mail.from = headers['from'] || @username
mail.to = headers['to']
mail.cc = headers['cc']
mail.bcc = headers['bcc']
mail.subject = headers['subject']
mail.from ||= @username
if (attachments = raw_body.split(/\n\s*\n/, 2)[0]) =~ /^attach(ment|ments)*:/
files = YAML::load(attachments).values.flatten
log "attach: #{files}"
files.each do |file|
if File.directory?(file)
Dir.glob("#{file}/*").each {|f| mail.add_file(f) if File.size?(f)}
else
mail.add_file(file) if File.size?(file)
end
end
mail.text_part do
body raw_body.split(/\n\s*\n/, 2)[1]
end
else
mail.text_part do
body raw_body
end
end
mail
end