Revision 30406d66
Added by Marc Dequènes about 15 years ago
- ID 30406d662870ea3ce7b86adcb1671f0f1be69fa9
lib/cyborghood/mail.rb | ||
---|---|---|
|
||
# add Rails load path for Debian, until rails framework is split properly
|
||
DEB_RAILS_PATH = "/usr/share/rails"
|
||
Dir.new(DEB_RAILS_PATH).each do |file|
|
||
next if file =~ /^\./
|
||
path = File.join(DEB_RAILS_PATH, file, "lib")
|
||
$: << path if File.directory?(path)
|
||
end
|
||
|
||
require 'cyborghood/base'
|
||
require 'delegate'
|
||
require 'tmail'
|
||
require 'tmail_extra'
|
||
require 'gpgme' # >= 1.0.2 needed for :always_trust sign option
|
||
require 'action_mailer/quoting'
|
||
require 'action_mailer/utils'
|
||
require 'net/smtp'
|
||
|
||
# This class handles RFC3156 signed messages, validates them, and extract orders properly.
|
||
# Encrypted content are not implemented yet.
|
||
module CyborgHood
|
||
class Mail < Delegator
|
||
def initialize(msg)
|
||
include ActionMailer::Quoting
|
||
include ActionMailer::Utils
|
||
|
||
def initialize(msg = nil)
|
||
@config = Config.instance
|
||
|
||
# unquote headers and transform into TMail object
|
||
@mail = TMail::Mail.parse(TMail::Unquoter.unquote_and_convert_to(msg, "UTF-8"))
|
||
if msg.nil?
|
||
@mail = TMail::Mail.new
|
||
else
|
||
# unquote headers and transform into TMail object
|
||
@mail = TMail::Mail.parse(TMail::Unquoter.unquote_and_convert_to(msg, "UTF-8"))
|
||
end
|
||
end
|
||
|
||
def __getobj__
|
||
@mail
|
||
end
|
||
|
||
def self.normalize_new_lines(text)
|
||
text.to_s.gsub(/\r\n?/, "\n")
|
||
end
|
||
|
||
def parse
|
||
sig_check = verify_pgp_signature()
|
||
if sig_check.status == 0
|
||
... | ... | |
def create_reply
|
||
mail_reply = @mail.create_reply
|
||
mail_reply.from_addrs = TMail::Address.parse(@config.mail.from_address || self.to.first)
|
||
mail_reply['Organization'] = @config.mail.organization
|
||
self.class.new(mail_reply.to_s)
|
||
end
|
||
|
||
def check_headers
|
||
@mail.header.keys.each do |h|
|
||
@mail[h] = quote_address_if_necessary(@mail[h].to_s, "utf-8")
|
||
end
|
||
end
|
||
|
||
def deliver
|
||
check_headers
|
||
|
||
smtp_server = @config.mail.smtp_server || "localhost"
|
||
smtp_port = @config.mail.smtp_port || 25
|
||
smtp_from = @mail.from_addrs.collect{|a| a.address}.join(", ")
|
||
smtp_to = @mail.to_addrs.collect{|a| a.address}
|
||
Net::SMTP.start(smtp_server, smtp_port) do |smtp|
|
||
#p @mail.to_s
|
||
smtp.send_message(@mail.to_s, smtp_from, smtp_to)
|
||
p @mail.to_s
|
||
#smtp.send_message(@mail.to_s, smtp_from, smtp_to)
|
||
end
|
||
end
|
||
|
||
... | ... | |
p_encrypted.body = encrypted_data
|
||
@mail.parts << p_encrypted
|
||
end
|
||
|
||
def quoted_printable_body=(txt)
|
||
@mail.transfer_encoding = "quoted-printable"
|
||
@mail.body = [normalize_new_lines(txt)].pack("M*")
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
[evol] ensure sent mail is correctly formated (using Rails ActionMailer utility methods)