root/lib/tmail_extra.rb @ 3cdff641
bd9c8c28 | Marc Dequènes (Duck) | #--
|
|
# CyborgHood, a distributed system management software.
|
|||
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|||
#
|
|||
# This program is free software: you can redistribute it and/or modify
|
|||
# it under the terms of the GNU General Public License as published by
|
|||
# the Free Software Foundation, either version 3 of the License, or
|
|||
# (at your option) any later version.
|
|||
#
|
|||
# This program is distributed in the hope that it will be useful,
|
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||
# GNU General Public License for more details.
|
|||
#
|
|||
# You should have received a copy of the GNU General Public License
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#++
|
|||
require 'tmail'
|
|||
require 'action_mailer/quoting'
|
|||
require 'action_mailer/utils'
|
|||
module TMail
|
|||
class Mail
|
|||
extend ActionMailer::Quoting
|
|||
include ActionMailer::Utils
|
|||
# WARNING: the TMail send_to() method is _unsuable_, even with the following fixes
|
|||
# fix method using obsoleted from_address() method
|
|||
def do_send_to( smtp )
|
|||
from = from_addrs or raise ArgumentError, 'no from address'
|
|||
(dests = destinations).empty? and raise ArgumentError, 'no receipient'
|
|||
yield
|
|||
send_to_0 smtp, from, dests
|
|||
end
|
|||
# fix method using encoded() with a wrong number of args
|
|||
def send_to_0( smtp, from, to )
|
|||
smtp.ready(from, to) do |f|
|
|||
encoded "\r\n", 'j', f
|
|||
end
|
|||
end
|
|||
# extra methods
|
|||
def quoted_printable_body=(txt)
|
|||
self.transfer_encoding = "quoted-printable"
|
|||
self.body = [normalize_new_lines(txt)].pack("M")
|
|||
end
|
|||
18970c0e | Marc Dequènes (Duck) | def body_normalized
|
|
normalize_new_lines(self.body)
|
|||
end
|
|||
bd9c8c28 | Marc Dequènes (Duck) | def self.header_value_pretty(v)
|
|
Unquoter.unquote_and_convert_to(v.to_s, "UTF-8")
|
|||
end
|
|||
def self.header_value_smtp(v)
|
|||
quote_address_if_necessary(v, "UTF-8")
|
|||
end
|
|||
["from", "to", "cc", "bcc", "reply_to"].each do |h|
|
|||
class_eval <<-EOF
|
|||
def #{h}_addrs_pretty
|
|||
return nil if self.#{h}_addrs.nil?
|
|||
self.#{h}_addrs.collect do |a|
|
|||
self.class.header_value_pretty(a)
|
|||
end
|
|||
end
|
|||
def #{h}_pretty
|
|||
(self.#{h}_addrs_pretty || []).join(", ")
|
|||
end
|
|||
EOF
|
|||
end
|
|||
def subject_pretty
|
|||
self.class.header_value_pretty(self.subject)
|
|||
end
|
|||
private
|
|||
# reuse current boundary if already exist, not to break signed parts
|
|||
def with_multipart_encoding( strategy )
|
|||
if parts().empty? # DO NOT USE @parts
|
|||
yield
|
|||
else
|
|||
bound = ::TMail.new_boundary
|
|||
if @header.key? 'content-type'
|
|||
if @header['content-type'].params.key? 'boundary'
|
|||
bound = @header['content-type'].params['boundary']
|
|||
else
|
|||
@header['content-type'].params['boundary'] = bound
|
|||
end
|
|||
else
|
|||
store 'Content-Type', %<multipart/mixed; boundary="#{bound}">
|
|||
end
|
|||
yield
|
|||
parts().each do |tm|
|
|||
strategy.puts
|
|||
strategy.puts '--' + bound
|
|||
tm.accept strategy
|
|||
end
|
|||
strategy.puts
|
|||
strategy.puts '--' + bound + '--'
|
|||
strategy.write epilogue()
|
|||
end
|
|||
end
|
|||
end
|
|||
9615a42b | Marc Dequènes (Duck) | ||
###
|
|||
# default Content-Type is: text/plain; charset=us-ascii
|
|||
# (see http://tools.ietf.org/html/rfc2045#section-5.2)
|
|||
class ContentTypeHeader
|
|||
def set( args )
|
|||
@main, @sub, @params = *args
|
|||
if @main.blank?
|
|||
@main = 'text'
|
|||
@sub = 'plain'
|
|||
@params = {'charset' => 'us-ascii'}
|
|||
end
|
|||
end
|
|||
end
|
|||
# don't break compatibility, but ensure RFC is respected when
|
|||
# no specific 'default' argument is specified
|
|||
class Mail
|
|||
def content_type( default = nil )
|
|||
rfc_default = 'text/plain'
|
|||
if h = @header['content-type']
|
|||
h.content_type || default || rfc_default
|
|||
else
|
|||
default || rfc_default
|
|||
end
|
|||
end
|
|||
def main_type( default = nil )
|
|||
rfc_default = 'text'
|
|||
if h = @header['content-type']
|
|||
h.main_type || default || rfc_default
|
|||
else
|
|||
default || rfc_default
|
|||
end
|
|||
end
|
|||
def sub_type( default = nil )
|
|||
rfc_default = 'plain'
|
|||
if h = @header['content-type']
|
|||
h.sub_type || default || rfc_default
|
|||
else
|
|||
default || rfc_default
|
|||
end
|
|||
end
|
|||
def type_param( name, default = nil )
|
|||
v = if h = @header['content-type']
|
|||
h[name] || default
|
|||
else
|
|||
default
|
|||
end
|
|||
name == 'charset' ? v || 'us-ascii' : v
|
|||
end
|
|||
def charset( default = nil )
|
|||
rfc_default = 'us-ascii'
|
|||
if h = @header['content-type']
|
|||
h['charset'] or default or rfc_default
|
|||
else
|
|||
default or rfc_default
|
|||
end
|
|||
end
|
|||
end
|
|||
#
|
|||
###
|
|||
bd9c8c28 | Marc Dequènes (Duck) | end
|