Project

General

Profile

« Previous | Next » 

Revision 275e20ec

Added by Marc Dequènes about 15 years ago

  • ID 275e20ecd94d3a83477dbec7e39e75ee6f619fee

[evol] create basic mail reply and send it using SMTP

View differences:

lib/cyborghood/lang_additions.rb
self.class.human_name
end
end
# WARNING: the TMail send_to() methos is _unsuable_, even with the following fixes
require 'tmail'
module TMail
class Mail
# 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
end
end
lib/cyborghood/mail.rb
require 'tmail'
require 'tmail_extra'
require 'gpgme'
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)
@config = Config.instance
# unquote headers and transform into TMail object
@mail = TMail::Mail.parse(TMail::Unquoter.unquote_and_convert_to(msg, "UTF-8"))
end
......
nil
end
def create_reply
mail_reply = @mail.create_reply
mail_reply.from_addrs = TMail::Address.parse(@config.mail.from_address || self.to.first)
self.class.new(mail_reply.to_s)
end
def deliver
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|
smtp.send_message(@mail.to_s, smtp_from, smtp_to)
end
end
def to_s
@mail.to_s
end
end
end
postman
end
mail = Mail.new(msg)
logger.info "Mail #{mail.message_id}: #{mail.from_addrs} -> #{mail.to_addrs} (#{mail.subject})"
logger.info "Received mail with ID '#{mail.message_id}': #{mail.from_addrs} -> #{mail.to_addrs} (#{mail.subject})"
# ignore mails not signed
unless mail.is_pgp_signed?
......
end
CommandParser.run(order)
mail_reply = mail.create_reply
mail_reply.body = "COUCOU"
mail_reply.deliver
end
end

Also available in: Unified diff