You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@whimsical.apache.org by cu...@apache.org on 2019/06/03 14:19:09 UTC
[whimsy] 01/02: Move mailer to server-side; sample erb template
This is an automated email from the ASF dual-hosted git repository.
curcuru pushed a commit to branch mail_ldap_notification
in repository https://gitbox.apache.org/repos/asf/whimsy.git
commit d53a2676826785d1085494a416ff9f2ec2c1ce0b
Author: Shane Curcuru <as...@shanecurcuru.org>
AuthorDate: Mon Jun 3 10:18:20 2019 -0400
Move mailer to server-side; sample erb template
---
www/roster/models/mailer.rb | 59 ++++++++++++++++++++++++++++++
www/roster/templates/ldap_notification.erb | 9 +++++
www/roster/views/utils.js.rb | 48 ------------------------
3 files changed, 68 insertions(+), 48 deletions(-)
diff --git a/www/roster/models/mailer.rb b/www/roster/models/mailer.rb
new file mode 100644
index 0000000..11050b3
--- /dev/null
+++ b/www/roster/models/mailer.rb
@@ -0,0 +1,59 @@
+#
+# Provide simple way to send notification emails.
+#
+# Once tested, this code could migrate into whimsy/asf, and be available
+# for all Rack application (e.g., secmail, board/agenda, roster)
+#
+
+# provide methods to encapsulate LDAP update
+module ASF
+ class Mailer
+ # Deliver an LDAP notification email, e.g. pmc/ppmc/group/etc. changes
+ # Uses creates and mail.deliver! using a template for body
+ # Includes X-headers to mark as Whimsy email
+ # DEBUGGING: options: true to prevent actual mail sending
+ def self.mail_ldap_notification(
+ from: nil,
+ to: nil,
+ cc: nil,
+ bcc: nil,
+ subject: nil,
+ template: nil,
+ data: nil, # Hash for ERB
+ options: nil
+ )
+ # TODO how should this method report problems?
+ raise ArgumentError, "From must not be nil or blank" if from.nil? || ''.eql?(from)
+ raise ArgumentError, "To or CC must not be nil" if to.nil? && cc.nil?
+ raise ArgumentError, "Subject must not be nil or blank" if subject.nil? || ''.eql?(subject)
+ template ||= 'ldap_notification.erb'
+
+ path = File.expand_path("../templates/#{template}", __FILE__.untaint)
+ file = File.join(path, template)
+ puts "-> #{path} #{template} #{file}"
+ tmplt = File.read(file.untaint).untaint
+ mail = Mail.new do
+ from from
+ to to
+ cc cc
+ bcc bcc
+ subject subject
+ b = binding # Bind the passed in data hash to send to ERB
+ data.each do |key, val|
+ b.local_variable_set(key.to_sym, val)
+ end
+ body ERB.new(tmplt).result(b)
+ end
+
+ # Header for root@ mail filters, per request infra
+ mail.header['X-For-Root'] = 'yes'
+ # Header to denote automated mail from whimsy
+ mail.header['X-Mailer'] = 'whimsy/www/roster/utils(0.1)'
+
+ # Deliver email
+ mail.delivery_method :test if options # TODO DEBUGGING don't actually send mail, just log it
+ mail.deliver!
+ return mail
+ end
+ end
+end
diff --git a/www/roster/templates/ldap_notification.erb b/www/roster/templates/ldap_notification.erb
new file mode 100644
index 0000000..296c8c5
--- /dev/null
+++ b/www/roster/templates/ldap_notification.erb
@@ -0,0 +1,9 @@
+The current roster for this group can be found at
+
+ https://whimsy.apache.org/roster/<%= type %>/<%= name %>
+
+LDAP update details:
+
+ <%= details %>
+
+This message was generated from Whimsy.
diff --git a/www/roster/views/utils.js.rb b/www/roster/views/utils.js.rb
index 2ec2e94..f27962d 100644
--- a/www/roster/views/utils.js.rb
+++ b/www/roster/views/utils.js.rb
@@ -24,52 +24,4 @@ class Utils
end
end
end
-
- # Deliver an LDAP notification email, e.g. pmc/ppmc/group/etc. changes
- # Uses creates and mail.deliver! using a template for body
- # Includes X-headers to mark as Whimsy email
- # DEBUGGING: options: true to prevent actual mail sending
- def self.mail_ldap_notification(
- from,
- to,
- cc,
- bcc,
- subject,
- template,
- data, # Hash for ERB
- options
- )
- # TODO how should this method report problems?
- raise ArgumentError, "From must not be nil or blank" if from.nil? || from == ''
- raise ArgumentError, "To or CC must not be nil" if to.nil? && cc.nil?
- raise ArgumentError, "Subject must not be nil or blank" if subject.nil? || subject == ''
- raise ArgumentError, "Template must not be nil or blank" if template.nil? || template == ''
-
- path = File.expand_path("../../templates/#{template}", __FILE__.untaint)
- file = File.join(path, template)
- puts "-> #{path} #{template} #{file}"
- tmplt = File.read(file.untaint).untaint
- mail = Mail.new do
- from from
- to to
- cc cc
- bcc bcc
- subject subject
- b = binding # Bind the passed in data hash to send to ERB
- data.each do |key, val|
- b.local_variable_set(key.to_sym, val)
- end
- body ERB.new(tmplt).result(b)
- end
-
- # Header for root@ mail filters, per request infra
- mail.header['X-For-Root'] = 'yes'
- # Header to denote automated mail from whimsy
- mail.header['X-Mailer'] = 'whimsy/www/roster/utils(0.1)'
-
- # Deliver email
- mail.delivery_method :test if options # TODO DEBUGGING don't actually send mail, just log it
- mail.deliver!
- return mail
- end
end