RFC2822 specifies that you use parentheses to denote a mailbox list. Use the addr-name spec to specify a display name and the from address.
Check against the raw headers of the mail as @mail.from excludes the display name Extract shared logic between notifier methods
This commit is contained in:
parent
6b5928ca09
commit
814eb7b426
9 changed files with 31 additions and 60 deletions
|
|
@ -5,10 +5,10 @@ module NotificationMailers
|
||||||
attr_accessor :comment
|
attr_accessor :comment
|
||||||
|
|
||||||
def set_headers(comment_id)
|
def set_headers(comment_id)
|
||||||
@comment = Comment.find_by_id(comment_id)
|
@comment = Comment.find_by_id(comment_id)
|
||||||
|
|
||||||
if mail?
|
if mail?
|
||||||
@headers[:from] = "#{@comment.author.name} (Diaspora*) <#{AppConfig[:smtp_sender_address]}>"
|
@headers[:from] = "[#{@comment.author.name} (Diaspora*)] <#{AppConfig[:smtp_sender_address]}>"
|
||||||
@headers[:subject] = truncate(@comment.parent.comment_email_subject, :length => TRUNCATION_LEN)
|
@headers[:subject] = truncate(@comment.parent.comment_email_subject, :length => TRUNCATION_LEN)
|
||||||
@headers[:subject] = "Re: #{@headers[:subject]}"
|
@headers[:subject] = "Re: #{@headers[:subject]}"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,16 @@ module NotificationMailers
|
||||||
TRUNCATION_LEN = 70
|
TRUNCATION_LEN = 70
|
||||||
|
|
||||||
class Base
|
class Base
|
||||||
attr_accessor :recipient, :recipient_name, :sender
|
attr_accessor :recipient, :sender
|
||||||
|
|
||||||
def initialize(recipient_id, sender_id=nil, *args)
|
def initialize(recipient_id, sender_id=nil, *args)
|
||||||
@headers = {}
|
@headers = {}
|
||||||
@recipient = User.find_by_id(recipient_id)
|
@recipient = User.find_by_id(recipient_id)
|
||||||
@recipient_name = @recipient.profile.first_name
|
|
||||||
@sender = Person.find_by_id(sender_id) if sender_id.present?
|
@sender = Person.find_by_id(sender_id) if sender_id.present?
|
||||||
|
|
||||||
log_mail(recipient_id, sender_id, self.class.to_s.underscore)
|
log_mail(recipient_id, sender_id, self.class.to_s.underscore)
|
||||||
|
|
||||||
with_locale do
|
with_recipient_locale do
|
||||||
set_headers(*args)
|
set_headers(*args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -22,19 +21,19 @@ module NotificationMailers
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def default_headers
|
def default_headers
|
||||||
headers = {
|
headers = {
|
||||||
|
:from => AppConfig[:smtp_sender_address],
|
||||||
:host => "#{AppConfig[:pod_uri]}",
|
:host => "#{AppConfig[:pod_uri]}",
|
||||||
:to => "\"#{@recipient.name}\" <#{@recipient.email}>"
|
:to => "\"#{@recipient.name}\" <#{@recipient.email}>"
|
||||||
}
|
}
|
||||||
|
|
||||||
headers[:from] = "\"#{@sender.name} (Diaspora)\" <#{AppConfig[:smtp_sender_address]}>" if @sender.present?
|
headers[:from] = "\"#{@sender.name} (Diaspora*)\" <#{AppConfig[:smtp_sender_address]}>" if @sender.present?
|
||||||
|
|
||||||
headers
|
headers
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_locale(&block)
|
def with_recipient_locale(&block)
|
||||||
I18n.with_locale(@recipient.language, &block)
|
I18n.with_locale(@recipient.language, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ module NotificationMailers
|
||||||
def set_headers(comment_id)
|
def set_headers(comment_id)
|
||||||
@comment = Comment.find(comment_id)
|
@comment = Comment.find(comment_id)
|
||||||
|
|
||||||
@headers[:from] = "#{@comment.author.name} (Diaspora*) <#{AppConfig[:smtp_sender_address]}>"
|
@headers[:from] = "[#{@comment.author.name} (Diaspora*)] <#{AppConfig[:smtp_sender_address]}>"
|
||||||
@headers[:subject] = truncate(@comment.parent.comment_email_subject, :length => TRUNCATION_LEN)
|
@headers[:subject] = truncate(@comment.parent.comment_email_subject, :length => TRUNCATION_LEN)
|
||||||
@headers[:subject] = "Re: #{@headers[:subject]}"
|
@headers[:subject] = "Re: #{@headers[:subject]}"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
module NotificationMailers
|
module NotificationMailers
|
||||||
class ConfirmEmail < NotificationMailers::Base
|
class ConfirmEmail < NotificationMailers::Base
|
||||||
def set_headers
|
def set_headers
|
||||||
@headers[:to] = "#{recipient_name} <#{@recipient.unconfirmed_email}>"
|
@headers[:to] = "#{@recipient.profile.first_name} <#{@recipient.unconfirmed_email}>"
|
||||||
@headers[:subject] = I18n.t('notifier.confirm_email.subject', :unconfirmed_email => @recipient.unconfirmed_email)
|
@headers[:subject] = I18n.t('notifier.confirm_email.subject', :unconfirmed_email => @recipient.unconfirmed_email)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ module NotificationMailers
|
||||||
@conversation = @message.conversation
|
@conversation = @message.conversation
|
||||||
@participants = @conversation.participants
|
@participants = @conversation.participants
|
||||||
|
|
||||||
@headers[:from] = "#{@message.author.name} (Diaspora*) <#{AppConfig[:smtp_sender_address]}>"
|
@headers[:from] = "[#{@message.author.name} (Diaspora*)] <#{AppConfig[:smtp_sender_address]}>"
|
||||||
@headers[:subject] = @conversation.subject.strip
|
@headers[:subject] = @conversation.subject.strip
|
||||||
@headers[:subject] = "Re: #{@headers[:subject]}" if @conversation.messages.size > 1
|
@headers[:subject] = "Re: #{@headers[:subject]}" if @conversation.messages.size > 1
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,6 @@ class Notifier < ActionMailer::Base
|
||||||
helper :markdownify
|
helper :markdownify
|
||||||
helper :notifier
|
helper :notifier
|
||||||
|
|
||||||
default :from => AppConfig[:smtp_sender_address]
|
|
||||||
|
|
||||||
include NotifierHelper
|
|
||||||
|
|
||||||
def self.admin(string, recipients, opts = {})
|
def self.admin(string, recipients, opts = {})
|
||||||
mails = []
|
mails = []
|
||||||
recipients.each do |rec|
|
recipients.each do |rec|
|
||||||
|
|
@ -24,70 +20,46 @@ class Notifier < ActionMailer::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def started_sharing(recipient_id, sender_id)
|
def started_sharing(recipient_id, sender_id)
|
||||||
@notification = NotificationMailers::StartedSharing.new(recipient_id, sender_id)
|
send_notification(:started_sharing, recipient_id, sender_id)
|
||||||
|
|
||||||
with_recipient_locale do
|
|
||||||
mail(@notification.headers)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def liked(recipient_id, sender_id, like_id)
|
def liked(recipient_id, sender_id, like_id)
|
||||||
@notification = NotificationMailers::Liked.new(recipient_id, sender_id, like_id)
|
send_notification(:liked, recipient_id, sender_id, like_id)
|
||||||
|
|
||||||
with_recipient_locale do
|
|
||||||
mail(@notification.headers)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def reshared(recipient_id, sender_id, reshare_id)
|
def reshared(recipient_id, sender_id, reshare_id)
|
||||||
@notification = NotificationMailers::Reshared.new(recipient_id, sender_id, reshare_id)
|
send_notification(:reshared, recipient_id, sender_id, reshare_id)
|
||||||
|
|
||||||
with_recipient_locale do
|
|
||||||
mail(@notification.headers)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def mentioned(recipient_id, sender_id, target_id)
|
def mentioned(recipient_id, sender_id, target_id)
|
||||||
@notification = NotificationMailers::Mentioned.new(recipient_id, sender_id, target_id)
|
send_notification(:mentioned, recipient_id, sender_id, target_id)
|
||||||
|
|
||||||
with_recipient_locale do
|
|
||||||
mail(@notification.headers)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def comment_on_post(recipient_id, sender_id, comment_id)
|
def comment_on_post(recipient_id, sender_id, comment_id)
|
||||||
@notification = NotificationMailers::CommentOnPost.new(recipient_id, sender_id, comment_id)
|
send_notification(:comment_on_post, recipient_id, sender_id, comment_id)
|
||||||
|
|
||||||
with_recipient_locale do
|
|
||||||
mail(@notification.headers)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def also_commented(recipient_id, sender_id, comment_id)
|
def also_commented(recipient_id, sender_id, comment_id)
|
||||||
@notification = NotificationMailers::AlsoCommented.new(recipient_id, sender_id, comment_id)
|
send_notification(:also_commented, recipient_id, sender_id, comment_id)
|
||||||
|
|
||||||
with_recipient_locale do
|
|
||||||
mail(@notification.headers) if @notification.mail?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def private_message(recipient_id, sender_id, message_id)
|
def private_message(recipient_id, sender_id, message_id)
|
||||||
@notification = NotificationMailers::PrivateMessage.new(recipient_id, sender_id, message_id)
|
send_notification(:private_message, recipient_id, sender_id, message_id)
|
||||||
|
|
||||||
with_recipient_locale do
|
|
||||||
mail(@notification.headers)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def confirm_email(recipient_id)
|
def confirm_email(recipient_id)
|
||||||
@notification = NotificationMailers::ConfirmEmail.new(recipient_id)
|
send_notification(:confirm_email, recipient_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def send_notification(type, *args)
|
||||||
|
@notification = NotificationMailers.const_get(type.to_s.camelize).new(*args)
|
||||||
|
|
||||||
with_recipient_locale do
|
with_recipient_locale do
|
||||||
mail(@notification.headers)
|
mail(@notification.headers)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
def with_recipient_locale(&block)
|
def with_recipient_locale(&block)
|
||||||
I18n.with_locale(@notification.recipient.language, &block)
|
I18n.with_locale(@notification.recipient.language, &block)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
%p
|
%p
|
||||||
= t('notifier.hello', :name => @notification.recipient_name)
|
= t('notifier.hello', :name => @notification.recipient.profile.first_name)
|
||||||
%p
|
%p
|
||||||
!= t('notifier.confirm_email.click_link', :unconfirmed_email => @notification.recipient.unconfirmed_email)
|
!= t('notifier.confirm_email.click_link', :unconfirmed_email => @notification.recipient.unconfirmed_email)
|
||||||
%br
|
%br
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
!= t('notifier.hello', :name => @notification.recipient_name)
|
!= t('notifier.hello', :name => @notification.recipient.profile.first_name)
|
||||||
|
|
||||||
!= t('notifier.confirm_email.click_link', :unconfirmed_email => @notification.recipient.unconfirmed_email)
|
!= t('notifier.confirm_email.click_link', :unconfirmed_email => @notification.recipient.unconfirmed_email)
|
||||||
!= confirm_email_url(:token => @notification.recipient.confirm_email_token)
|
!= confirm_email_url(:token => @notification.recipient.confirm_email_token)
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ describe Notifier do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".started_sharing" do
|
describe ".started_sharing" do
|
||||||
let!(:request_mail) {Notifier.started_sharing(bob.id, person.id)}
|
let!(:request_mail) { Notifier.started_sharing(bob.id, person.id) }
|
||||||
|
|
||||||
it 'goes to the right person' do
|
it 'goes to the right person' do
|
||||||
request_mail.to.should == [bob.email]
|
request_mail.to.should == [bob.email]
|
||||||
|
|
@ -172,7 +172,7 @@ describe Notifier do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "FROM: contains the sender's name" do
|
it "FROM: contains the sender's name" do
|
||||||
@mail.from.should == "[#{@cnv.author.name} (Diaspora)] <#{AppConfig[:smtp_sender_address]}>"
|
@mail["From"].to_s.should == "[#{@cnv.author.name} (Diaspora*)] <#{AppConfig[:smtp_sender_address]}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'SUBJECT: has a snippet of the post contents' do
|
it 'SUBJECT: has a snippet of the post contents' do
|
||||||
|
|
@ -207,7 +207,7 @@ describe Notifier do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "FROM: contains the sender's name" do
|
it "FROM: contains the sender's name" do
|
||||||
comment_mail.from.should == "[#{eve.name} (Diaspora)] <#{AppConfig[:smtp_sender_address]}>"
|
comment_mail["From"].to_s.should == "[#{eve.name} (Diaspora*)] <#{AppConfig[:smtp_sender_address]}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'SUBJECT: has a snippet of the post contents' do
|
it 'SUBJECT: has a snippet of the post contents' do
|
||||||
|
|
@ -248,7 +248,7 @@ describe Notifier do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'FROM: has the name of person commenting as the sender' do
|
it 'FROM: has the name of person commenting as the sender' do
|
||||||
comment_mail.from.should == "[#{eve.name} (Diaspora)] <#{AppConfig[:smtp_sender_address]}>"
|
comment_mail["From"].to_s.should == "[#{eve.name} (Diaspora*)] <#{AppConfig[:smtp_sender_address]}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'SUBJECT: has a snippet of the post contents' do
|
it 'SUBJECT: has a snippet of the post contents' do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue