#!/usr/local/bin/ruby
#
# Copyright 2005 Perforce Software. All rights reserved.
#
# Simple Mail Class to pipe messages to sendmail
#
require "P4Form.rb" # For get_email(userid) function
class P4Mail
def initialize()
@from = ""
@to_list = []
@cc_list = []
@subject = ""
@body = ""
end
attr :from, true
attr :to_list, true
attr :cc_list, true
attr :subject, true
attr :body, true
def msgprint(file)
file.printf("From: %s\r\n", from)
if @to_list.length > 0 then
file.printf("To: %s\r\n", @to_list.join(", "))
end
if @cc_list.length > 0 then
file.printf("Cc: %s\r\n", @cc_list.join(", "))
end
file.printf("Date: %s\r\n", Time.now.strftime("%a, %d %b %Y %T %z"))
file.printf("Subject: %s\r\n", subject)
file.printf("\r\n")
file.printf("%s\r\n.\r\n", body)
end
def send()
# Convert any UserIDs in TO and CC to email addresses
@to_list.collect! { |addr| get_email(addr) }
@to_list.compact!
@to_list.uniq!
@cc_list.collect! { |addr| get_email(addr) }
@cc_list.compact!
@cc_list.uniq!
# Remove from CC list anyone who is also in TO
@cc_list -= @to_list
#
# Uncomment to following line to have the system go live
# using sendmail instead of just writing the messages to
# a file (and comment out the other line)
#
#proc = IO.popen("/usr/sbin/sendmail -t", "w+")
proc = IO.popen("/bin/cat >> test.msg", "w+")
msgprint(proc)
proc.close_write
proc.close_read
end
end
#msg = P4Mail.new
#msg.to_list << "alpha"
#msg.to_list << "beta"
#msg.to_list << "gamma"
#msg.to_list << "aaa@bbb"
#msg.to_list << "bbb@ccc"
#msg.cc_list << "zeta"
#msg.cc_list << "beta"
#msg.cc_list << "omega"
#msg.cc_list << "aaa@bbb"
#msg.cc_list << "ddd@eee"
#msg.subject = "Subject"
#msg.body = "Body"
#msg.msgprint($stdout)
#msg.send()