-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
person_mailer.rb
371 lines (329 loc) · 16.5 KB
/
person_mailer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# encoding: utf-8
include ApplicationHelper
include ListingsHelper
include TruncateHtmlHelper
class PersonMailer < ActionMailer::Base
include MailUtils
# Enable use of method to_date.
require 'active_support/core_ext'
require "truncate_html"
default :from => APP_CONFIG.sharetribe_mail_from_address
layout 'email'
add_template_helper(EmailTemplateHelper)
def conversation_status_changed(transaction, community)
@email_type = (transaction.status == "accepted" ? "email_when_conversation_accepted" : "email_when_conversation_rejected")
recipient = transaction.other_party(transaction.listing.author)
set_up_layout_variables(recipient, community, @email_type)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@transaction = transaction
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => t("emails.conversation_status_changed.your_request_was_#{transaction.status}"))
end
end
def new_message_notification(message, community)
@email_type = "email_about_new_messages"
recipient = message.conversation.other_party(message.sender)
set_up_layout_variables(recipient, community, @email_type)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@message = message
sending_params = {:to => recipient.confirmed_notification_emails_to,
:subject => t("emails.new_message.you_have_a_new_message", :sender_name => PersonViewUtils.person_display_name(message.sender, community)),
:from => community_specific_sender(community)}
premailer_mail(sending_params)
end
end
def transaction_confirmed(conversation, community)
@email_type = "email_about_completed_transactions"
@conversation = conversation
recipient = conversation.seller
set_up_layout_variables(conversation.seller, community, @email_type)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => t("emails.transaction_confirmed.request_marked_as_#{@conversation.status}"))
end
end
def transaction_automatically_confirmed(conversation, community)
@email_type = "email_about_completed_transactions"
@conversation = conversation
recipient = conversation.buyer
set_up_layout_variables(recipient, community, @email_type)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:template_path => 'person_mailer/automatic_confirmation',
:subject => t("emails.transaction_automatically_confirmed.subject"))
end
end
def booking_transaction_automatically_confirmed(transaction, community)
@email_type = "email_about_completed_transactions"
@transaction = transaction
recipient = @transaction.buyer
set_up_layout_variables(recipient, community, @email_type)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
mail(:to => @recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:template_path => 'person_mailer/automatic_confirmation',
:subject => t("emails.booking_transaction_automatically_confirmed.subject"))
end
end
def new_testimonial(testimonial, community)
@email_type = "email_about_new_received_testimonials"
recipient = testimonial.receiver
set_up_layout_variables(recipient, community, @email_type)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@testimonial = testimonial
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => t("emails.new_testimonial.has_given_you_feedback_in_kassi", :name => PersonViewUtils.person_display_name(testimonial.author, community)))
end
end
# Remind user to fill in payment details
def payment_settings_reminder(listing, recipient, community)
set_up_layout_variables(recipient, community)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@listing = listing
@recipient = recipient
if community.payments_in_use?
@payment_settings_link = paypal_account_settings_payment_url(recipient, @url_params.merge(locale: recipient.locale))
end
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => t("emails.payment_settings_reminder.remember_to_add_payment_details")) do |format|
format.html {render :locals => {:skip_unsubscribe_footer => true} }
end
end
end
# Remind users of conversations that have not been accepted or rejected
def confirm_reminder(conversation, recipient, community, days_to_cancel)
@email_type = "email_about_confirm_reminders"
recipient = conversation.buyer
set_up_layout_variables(recipient, community, @email_type)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@conversation = conversation
@days_to_cancel = days_to_cancel
template = "confirm_reminder"
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => t("emails.confirm_reminder.remember_to_confirm_request")) do |format|
format.html { render template }
end
end
end
# Remind users to give feedback
def testimonial_reminder(conversation, recipient, community)
@email_type = "email_about_testimonial_reminders"
set_up_layout_variables(recipient, community, @email_type)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@conversation = conversation
@other_party = @conversation.other_party(recipient)
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => t("emails.testimonial_reminder.remember_to_give_feedback_to", :name => PersonViewUtils.person_display_name(@other_party, community)))
end
end
def new_comment_to_own_listing_notification(comment, community)
@email_type = "email_about_new_comments_to_own_listing"
recipient = comment.listing.author
set_up_layout_variables(recipient, community, @email_type)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@comment = comment
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => t("emails.new_comment.you_have_a_new_comment", :author => PersonViewUtils.person_display_name(comment.author, community)))
end
end
def new_comment_to_followed_listing_notification(comment, recipient, community)
set_up_layout_variables(recipient, community)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@comment = comment
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => t("emails.new_comment.listing_you_follow_has_a_new_comment", :author => PersonViewUtils.person_display_name(comment.author, community)))
end
end
def new_update_to_followed_listing_notification(listing, recipient, community)
set_up_layout_variables(recipient, community)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@listing = listing
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => t("emails.new_update_to_listing.listing_you_follow_has_been_updated"))
end
end
def new_listing_by_followed_person(listing, recipient, community)
set_up_layout_variables(recipient, community)
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@listing = listing
@no_recipient_name = true
@author_name = PersonViewUtils.person_display_name(listing.author, community)
@listing_url = listing_url(@url_params.merge({:id => listing.id}))
@translate_scope = [ :emails, :new_listing_by_followed_person ]
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => t("emails.new_listing_by_followed_person.subject",
:author_name => @author_name,
:community => community.full_name_with_separator(recipient.locale)))
end
end
def invitation_to_kassi(invitation)
@invitation = invitation
mail_locale = @invitation.inviter.locale
@invitation_code_required = invitation.community.join_with_invite_only
set_up_layout_variables(nil, invitation.community)
@url_params[:locale] = mail_locale
@url_params[:code] = invitation.code
@invitation_community = invitation.community.full_name_with_separator(invitation.inviter.locale)
with_locale(mail_locale, invitation.community.locales.map(&:to_sym), invitation.community.id) do
subject = t("emails.invitation_to_kassi.you_have_been_invited_to_kassi", :inviter => PersonViewUtils.person_display_name(invitation.inviter, invitation.community), :community => @invitation_community)
premailer_mail(:to => invitation.email,
:from => community_specific_sender(invitation.community),
:subject => subject,
:reply_to => invitation.inviter.confirmed_notification_email_to)
end
end
# A message from the community admin to a single community member
def community_member_email(sender, recipient, email_subject, email_content, community)
@email_type = "email_from_admins"
set_up_layout_variables(recipient, community, @email_type)
sender_address = EmailService::API::Api.addresses.get_sender(community_id: community.id).data
if sender_address[:type] == :default
sender_name = sender.name(community)
sender_email = sender.confirmed_notification_email_to
reply_to = "\"#{sender_name}\"<#{sender_email}>"
else
reply_to = sender_address[:smtp_format]
end
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@email_content = email_content
@no_recipient_name = true
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => email_subject,
:reply_to => reply_to)
end
end
# Used to send notification to marketplace admins when somebody
# gives feedback on marketplace throught the contact us button in menu
def new_feedback(feedback, community)
subject = t("feedback.feedback_subject", service_name: community.name(I18n.locale))
premailer_mail(
:to => mail_feedback_to(community, APP_CONFIG.feedback_mailer_recipients),
:from => community_specific_sender(community),
:subject => subject,
:reply_to => feedback.email) do |format|
format.html {
render locals: {
author_name_and_email: feedback_author_name_and_email(feedback.author, feedback.email, community),
community_name: community.name(I18n.locale),
content: feedback.content
}
}
end
end
def mail_feedback_to(community, platform_admin_email)
if community.admin_emails.any?
community.admin_emails.join(",")
else
platform_admin_email
end
end
# Old layout
def new_member_notification(new_member, community, admin)
@community = community
@no_settings = true
@person = new_member
@email = new_member.emails.last.address
with_locale(admin.locale, community.locales.map(&:to_sym), community.id) do
address = admin.confirmed_notification_emails_to
if address.present?
premailer_mail(:to => address,
:from => community_specific_sender(community),
:subject => "New member in #{@community.full_name(@person.locale)}",
:template_name => "new_member_notification")
end
end
end
def email_confirmation(email, community)
@current_community = community
@no_settings = true
@resource = email.person
@confirmation_token = email.confirmation_token
@host = community.full_domain
@show_branding_info = !PlanService::API::Api.plans.get_current(community_id: community.id).data[:features][:whitelabel]
with_locale(email.person.locale, community.locales.map(&:to_sym), community.id) do
email.update_attribute(:confirmation_sent_at, Time.now)
premailer_mail(:to => email.address,
:from => community_specific_sender(community),
:subject => t("devise.mailer.confirmation_instructions.subject"),
:template_path => 'devise/mailer',
:template_name => 'confirmation_instructions')
end
end
def reset_password_instructions(person, email_address, reset_token, community)
set_up_layout_variables(nil, community) # Using nil as recipient, as we don't want auth token here.
@person = person
@no_settings = true
premailer_mail(
to: email_address,
from: community_specific_sender(@community),
subject: t("devise.mailer.reset_password_instructions.subject")) do |format|
format.html {
render layout: false, locals: { reset_token: reset_token,
host: @community.full_domain}
}
end
end
def welcome_email(person, community, regular_email=nil, test_email=false)
@recipient = person
recipient = person
with_locale(recipient.locale, community.locales.map(&:to_sym), community.id) do
@current_community = community
@regular_email = regular_email
@url_params = {}
@url_params[:host] = "#{community.full_domain}"
@url_params[:locale] = recipient.locale
@url_params[:ref] = "welcome_email"
@url_params.freeze # to avoid accidental modifications later
@test_email = test_email
@show_branding_info = !PlanService::API::Api.plans.get_current(community_id: community.id).data[:features][:whitelabel]
subject = if @recipient.has_admin_rights?(@current_community) && !@test_email
t("emails.welcome_email_marketplace_creator.welcome_email_subject_for_marketplace_creator")
else
t("emails.welcome_email.welcome_email_subject", :community => community.full_name(recipient.locale), :person => PersonViewUtils.person_display_name_for_type(person, "first_name_only"))
end
premailer_mail(:to => recipient.confirmed_notification_emails_to,
:from => community_specific_sender(community),
:subject => subject) do |format|
format.html { render :layout => 'email_blank_layout' }
end
end
end
# A message from the community admin to a community member
def self.community_member_email_from_admin(sender, recipient, community, email_subject, email_content, email_locale)
if recipient.should_receive?("email_from_admins") && (email_locale.eql?("any") || recipient.locale.eql?(email_locale))
begin
MailCarrier.deliver_now(community_member_email(sender, recipient, email_subject, email_content, community))
rescue => e
# Catch the exception and continue sending the emails
ApplicationHelper.send_error_notification("Error sending email to all the members of community #{community.full_name(email_locale)}: #{e.message}", e.class)
end
end
end
def premailer_mail(opts, &block)
premailer(mail(opts, &block))
end
private
def feedback_author_name_and_email(author, email, community)
present = ->(x) {x.present?}
case [author, email]
when matches([present, present])
"#{PersonViewUtils.person_display_name(author, community)} (#{email})"
when matches([nil, present])
"#{t("feedback.unlogged_user")} (#{email})"
else
"#{t("feedback.anonymous_user")}"
end
end
end