Project

General

Profile

« Previous | Next » 

Revision 30406d66

Added by Marc Dequènes about 15 years ago

  • ID 30406d662870ea3ce7b86adcb1671f0f1be69fa9

[evol] ensure sent mail is correctly formated (using Rails ActionMailer utility methods)

View differences:

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
postman
if reply_attachments.empty?
mail_reply.set_content_type("text", "plain")
mail_reply.set_disposition("inline")
mail_reply.transfer_encoding = "quoted-printable"
mail_reply.body = reply_txt
mail_reply.quoted_printable_body = reply_txt
else
mail_reply.set_content_type("multipart", "mixed", {'boundary' => TMail.new_boundary})
parts = []
p = TMail::Mail.new
p = CyborgHood::Mail.new
p.set_content_type("text", "plain", {'charset' => "utf-8"})
p.set_disposition("inline")
p.transfer_encoding = "quoted-printable"
p.body = [Mail.normalize_new_lines(reply_txt)].pack("M*")
p.quoted_printable_body = reply_txt
mail_reply.parts << p
reply_attachments.each do |attachment|
p = TMail::Mail.new
p = CyborgHood::Mail.new
p.set_content_type("text", "plain", {'charset' => "utf-8"})
p.set_disposition("attachment", {'filename' => "test.rb"})
p.transfer_encoding = "quoted-printable"
p.body = [Mail.normalize_new_lines(attachment)].pack("M*")
p.quoted_printable_body = attachment
mail_reply.parts << p
end
end

Also available in: Unified diff