module Hyla
module Commands
class Sendmail < Command
def self.process(args, options)
location = options[:source] if self.check_mandatory_option?('-s / --source', options[:source])
file_name = options[:file] if check_mandatory_option?('-f / --file', options[:file])
email_attributes = options[:email_attributes] if check_mandatory_option?('-e / --email_attributes', options[:email_attributes])
attachment = options[:attachment]
sender = email_attributes[:from]
recipients = email_attributes[:to]
subject = email_attributes[:subject]
file_path = [location, file_name] * '/'
# Parameters used to connect to the SMTP Server
@smtp_server = email_attributes[:smtp_server] if check_mandatory_option?(':smtp_server not defined in _config.yaml file', email_attributes[:smtp_server])
@port_number = email_attributes[:port] if check_mandatory_option?(':port not defined in _config.yaml file', email_attributes[:port])
@user = email_attributes[:user]
@password = email_attributes[:password]
@authentication = email_attributes[:authentication]
@enable_starttls = email_attributes[:enable_starttls] if check_mandatory_option?(':enable_start_tls not defined in _config.yaml file', email_attributes[:enable_starttls])
body = <<-EOS
Remark : html Link works with Thunderbird, Gmail clients but not using Zimbra Web Client
EOS
mail = populate_email(recipients, sender, subject)
case attachment
when true
attachment = File.read(file_path)
mail.attachments[file_name] = {
:mime_type => 'application/x-html',
:content => attachment,
:Content_Transfer_Encoding => 'quoted-printable'}
inline_html = inline_body_with_attachments(body, mail.attachments)
when false
inline_html = File.read(file_path)
end
html_part = Mail::Part.new do
content_type 'text/html; charset=UTF-8'
body inline_html
end
mail.html_part = html_part
mail.delivery_method :smtp, parameters()
mail.deliver!
Hyla.logger.info "Email send to SMTP server from #{sender} with this subject : #{subject}"
end
#
# Create Mail using
# Recipients, Sender and subject
#
def self.populate_email(recipients, sender, subject)
mail = Mail.new do
to recipients
from sender
subject subject
content_type 'multipart/related'
end
return mail
end
#
# Generate the Hash of the parameters
# used by mail compoent nto send email
#
def self.parameters()
parameters = {}
parameters[:address] = @smtp_server unless @smtp_server.nil?
parameters[:port] = @port_number unless @port_number.nil?
parameters[:enable_starttls] = @enable_starttls unless @enable_starttls.nil?
parameters[:user_name] = @user unless @user.nil?
parameters[:password] = @password unless @password.nil?
parameters[:authentication] = @authentication unless @authentication.nil?
return parameters
end
#
# Substitute filename with cid
#
def self.inline_body_with_attachments(html, attachments)
attachments.each do |attachment|
if (html =~ /#{attachment.filename}/)
html = html.sub(attachment.filename, "cid:#{attachment.cid}")
end
end
return html
end
end # class
end # module Commands
end # module Hyla