Merge pull request #2647 from riking/multiple-addresses

Allow processing of email sent to multiple addresses
This commit is contained in:
Sam 2014-08-22 10:32:42 +10:00
commit ac4f6d9c97
17 changed files with 117 additions and 42 deletions

View file

@ -43,6 +43,8 @@ module Jobs
message_template = :email_reject_parsing
when Email::Receiver::EmailLogNotFound
message_template = :email_reject_reply_key
when Email::Receiver::BadDestinationAddress
message_template = :email_reject_destination
when ActiveRecord::Rollback
message_template = :email_reject_post_error
when Email::Receiver::InvalidPost

View file

@ -1472,6 +1472,13 @@ en:
The provided reply key is invalid or unknown, so we don't know what this email is in reply to. Contact a staff member.
email_reject_destination:
subject_template: "Email issue -- Bad Destination Address(es)"
text_body_template: |
We're sorry, but your email message to %{destination} (titled %{former_title}) didn't work.
None of the destination addresses are recognized by the forum. Please make sure that the forum is in the To: line (not CC or BCC), and that you are sending to the email address provided by the forum administrators.
email_error_notification:
subject_template: "Email issue -- POP authentication error"
text_body_template: |

View file

@ -13,10 +13,11 @@ module Email
class EmptyEmailError < ProcessingError; end
class UserNotFoundError < ProcessingError; end
class UserNotSufficientTrustLevelError < ProcessingError; end
class BadDestinationAddress < ProcessingError; end
class EmailLogNotFound < ProcessingError; end
class InvalidPost < ProcessingError; end
attr_reader :body, :reply_key, :email_log
attr_reader :body, :email_log
def initialize(raw)
@raw = raw
@ -37,10 +38,26 @@ module Email
discourse_email_parser
raise EmailUnparsableError if @body.blank?
if is_in_email?
@user = User.find_by_email(@message.from.first)
dest_info = {type: :invalid, obj: nil}
@message.to.each do |to_address|
if dest_info[:type] == :invalid
dest_info = check_address to_address
end
end
raise BadDestinationAddress if dest_info[:type] == :invalid
if dest_info[:type] == :category
raise BadDestinationAddress unless SiteSetting.email_in
category = dest_info[:obj]
@category_id = category.id
@allow_strangers = category.email_in_allow_strangers
user_email = @message.from.first
@user = User.find_by_email(user_email)
if @user.blank? && @allow_strangers
wrap_body_in_quote
wrap_body_in_quote user_email
# TODO This is WRONG it should register an account
# and email the user details on how to log in / activate
@user = Discourse.system_user
@ -51,22 +68,32 @@ module Email
create_new_topic
else
@reply_key = @message.to.first
@email_log = dest_info[:obj]
# Extract the `reply_key` from the format the site has specified
tokens = SiteSetting.reply_by_email_address.split("%{reply_key}")
tokens.each do |t|
@reply_key.gsub!(t, "") if t.present?
end
# Look up the email log for the reply key
@email_log = EmailLog.for(reply_key)
raise EmailLogNotFound if @email_log.blank?
create_reply
end
end
def check_address(address)
category = Category.find_by_email(address)
return {type: :category, obj: category} if category
regex = Regexp.escape SiteSetting.reply_by_email_address
regex = regex.gsub(Regexp.escape('%{reply_key}'), "(.*)")
regex = Regexp.new regex
match = regex.match address
if match && match[1].present?
reply_key = match[1]
email_log = EmailLog.for(reply_key)
return {type: :reply, obj: email_log}
end
{type: :invalid, obj: nil}
end
private
def parse_body
@ -135,21 +162,8 @@ module Email
@body.strip!
end
def is_in_email?
@allow_strangers = false
return false unless SiteSetting.email_in
category = Category.find_by_email(@message.to.first)
return false unless category
@category_id = category.id
@allow_strangers = category.email_in_allow_strangers
true
end
def wrap_body_in_quote
@body = "[quote=\"#{@message.from.first}\"]
def wrap_body_in_quote(user_email)
@body = "[quote=\"#{user_email}\"]
#{@body}
[/quote]"
end

View file

@ -181,7 +181,6 @@ greatest show ever created. Everyone should watch it.
expect(receiver.body).to eq(reply_body)
expect(receiver.email_log).to eq(email_log)
expect(receiver.reply_key).to eq(reply_key)
attachment_email = fixture_file("emails/attachment.eml")
attachment_email = fill_email(attachment_email, "test@test.com", to)
@ -209,7 +208,7 @@ greatest show ever created. Everyone should watch it.
# no email in for user
expect{
process_email(from: "cobb@dob.com", to: "invalid@address.com")
}.to raise_error(Email::Receiver::EmailLogNotFound)
}.to raise_error(Email::Receiver::BadDestinationAddress)
# valid target invalid user
expect{

View file

@ -10,7 +10,7 @@ Mime-Version: 1.0 (1.0)
Date: Wed, 24 Jul 2013 15:59:10 +0100
Message-ID: <4597127794206131679@unknownmsgid>
Subject: Re: [Discourse] new reply to your post in 'Crystal Blue'
To: walter via Discourse <discourse-reply+cd480e301683c9902891f15968bf07a5@discourse.org>
To: walter via Discourse <reply+cd480e301683c9902891f15968bf07a5@appmail.adventuretime.ooo>
Content-Type: multipart/alternative; boundary=20cf301cc47ada510404f040b262
--20cf301cc47ada510404f040b262

View file

@ -9,7 +9,7 @@ Message-ID: <CADkmRcL=2aPV7jOcs2i00QGZNwmjYj3qMaJTsKnB7DTP5sgyFQ@mail.gmail.com>
Subject: Re: [Adventure Time] jake mentioned you in 'peppermint butler is
missing'
From: Finn the Human <finn@adventuretime.ooo>
To: jake via Adventure Time <math+96dcd9072ba9072d06226009d4223a6e@tree.mail>
To: jake via Adventure Time <reply+96dcd9072ba9072d06226009d4223a6e@appmail.adventuretime.ooo>
Content-Type: multipart/alternative; boundary=001a11c206a073876a04df81d2a9
--001a11c206a073876a04df81d2a9

View file

@ -10,7 +10,7 @@ Mime-Version: 1.0 (1.0)
Date: Wed, 24 Jul 2013 15:59:10 +0100
Message-ID: <4597127794206131679@unknownmsgid>
Subject: Re: [Discourse] new reply to your post in 'Crystal Blue'
To: walter via Discourse <discourse-reply+cd480e301683c9902891f15968bf07a5@discourse.org>
To: walter via Discourse <reply+cd480e301683c9902891f15968bf07a5@appmail.adventuretime.ooo>
Content-Type: multipart/alternative; boundary=001a11c20edc15a39304e2432790
Dit is een antwoord in het Nederlands.

View file

@ -5,7 +5,7 @@ Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <reply
Received: by 10.0.0.1 with HTTP; Thu, 13 Jun 2013 14:03:48 -0700
Date: Thu, 13 Jun 2013 17:03:48 -0400
From: Jake the Dog <jake@adventuretime.ooo>
To: reply+59d8df8370b7e95c5a49fbf86aeb2c93@discourse.example.com
To: reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo
Message-ID: <CADkmRc+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
Subject: re: [Discourse Meta] eviltrout posted in 'Adventure Time Sux'
Mime-Version: 1.0

View file

@ -10,7 +10,7 @@ Mime-Version: 1.0 (1.0)
Date: Wed, 24 Jul 2013 15:59:10 +0100
Message-ID: <4597127794206131679@unknownmsgid>
Subject: Re: [Discourse] new reply to your post in 'Crystal Blue'
To: walter via Discourse <discourse-reply+cd480e301683c9902891f15968bf07a5@discourse.org>
To: walter via Discourse <reply+cd480e301683c9902891f15968bf07a5@appmail.adventuretime.ooo>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64

View file

@ -38,7 +38,7 @@ Subject: Re: [Discourse Meta] [PM] re: Regarding your post in "Site
Customization not working"
From: aaron@breakingbad.com
In-Reply-To: <51c238655a394_5f4e3ce6690667bd@tiefighter2.mail>
To: discourse+20c1b0a8bd1a63c0163cc7e7641ca06b@breakingbad.com
To: reply+20c1b0a8bd1a63c0163cc7e7641ca06b@appmail.adventuretime.ooo
ReSent-Date: Thu, 20 Jun 2013 11:53:08 -0400 (EDT)
ReSent-From: Aaron <aaron@breakingbad.com>
ReSent-Subject: Re: [Discourse Meta] [PM] re: Regarding your post in "Site

View file

@ -10,7 +10,7 @@ Mime-Version: 1.0 (1.0)
Date: Wed, 24 Jul 2013 15:59:10 +0100
Message-ID: <4597127794206131679@unknownmsgid>
Subject: Re: [Discourse] new reply to your post in 'Crystal Blue'
To: walter via Discourse <discourse-reply+cd480e301683c9902891f15968bf07a5@discourse.org>
To: walter via Discourse <reply+cd480e301683c9902891f15968bf07a5@appmail.adventuretime.ooo>
Content-Type: multipart/alternative; boundary=001a11c20edc15a39304e2432790
Thanks!

View file

@ -3,7 +3,7 @@ Date: Wed, 19 Jun 2013 18:18:58 -0400
From: Anakin Skywalker <evildad@darthvader.ca>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6
MIME-Version: 1.0
To: Han Solo via Death Star <discourse+603775f8f5f68006461890a3eadf94cf@darthvader.ca>
To: Han Solo via Death Star <reply+603775f8f5f68006461890a3eadf94cf@appmail.adventuretime.ooo>
Subject: Re: [Death Star] [PM] re: Regarding your post in "Site Customization
not working"
References: <51d23d33f41fb_5f4e4b35d7d60798@xwing.mail>

View file

@ -0,0 +1,40 @@
Return-Path: <jake@adventuretime.ooo>
Received: from iceking.adventuretime.ooo ([unix socket]) by iceking (Cyrus v2.2.13-Debian-2.2.13-19+squeeze3) with LMTPA; Thu, 13 Jun 2013 17:03:50 -0400
Received: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b0:4001:c03::234]) by iceking.adventuretime.ooo (8.14.3/8.14.3/Debian-9.4) with ESMTP id r5DL3nFJ016967 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for <reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 17:03:50 -0400
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo>; Thu, 13 Jun 2013 14:03:48 -0700
Received: by 10.0.0.1 with HTTP; Thu, 13 Jun 2013 14:03:48 -0700
Date: Thu, 13 Jun 2013 17:03:48 -0400
From: Jake the Dog <jake@adventuretime.ooo>
To: finn@adventuretime.ooo, reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo
Message-ID: <CADkmRc+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
Subject: re: [Discourse Meta] eviltrout posted in 'Adventure Time Sux'
Mime-Version: 1.0
Content-Type: text/plain;
charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Sieve: CMU Sieve 2.2
X-Received: by 10.0.0.1 with SMTP id n7mr11234144ipb.85.1371157428600; Thu,
13 Jun 2013 14:03:48 -0700 (PDT)
X-Scanned-By: MIMEDefang 2.69 on IPv6:2001:470:1d:165::1
I could not disagree more. I am obviously biased but adventure time is the
greatest show ever created. Everyone should watch it.
- Jake out
On Sun, Jun 9, 2013 at 1:39 PM, eviltrout via Discourse Meta
<reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo> wrote:
>
>
>
> eviltrout posted in 'Adventure Time Sux' on Discourse Meta:
>
> ---
> hey guys everyone knows adventure time sucks!
>
> ---
> Please visit this link to respond: http://localhost:3000/t/adventure-time-sux/1234/3
>
> To unsubscribe from these emails, visit your [user preferences](http://localhost:3000/user_preferences).
>

View file

@ -10,7 +10,7 @@ Mime-Version: 1.0 (1.0)
Date: Wed, 24 Jul 2013 15:59:10 +0100
Message-ID: <4597127794206131679@unknownmsgid>
Subject: Re: [Discourse] new reply to your post in 'Crystal Blue'
To: walter via Discourse <discourse-reply+cd480e301683c9902891f15968bf07a5@discourse.org>
To: walter via Discourse <reply+cd480e301683c9902891f15968bf07a5@appmail.adventuretime.ooo>
Content-Type: multipart/alternative; boundary=001a11c20edc15a39304e2432790
This will not include the previous discussion that is present in this email.

View file

@ -10,7 +10,7 @@ Mime-Version: 1.0 (1.0)
Date: Wed, 24 Jul 2013 15:59:10 +0100
Message-ID: <4597127794206131679@unknownmsgid>
Subject: Re: [Discourse] new reply to your post in 'Crystal Blue'
To: walter via Discourse <discourse-reply+cd480e301683c9902891f15968bf07a5@discourse.org>
To: walter via Discourse <reply+cd480e301683c9902891f15968bf07a5@appmail.adventuretime.ooo>
Content-Type: multipart/alternative; boundary=001a11c20edc15a39304e2432790
Hello this email has content!

View file

@ -5,7 +5,7 @@ Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <reply
Received: by 10.0.0.1 with HTTP; Thu, 13 Jun 2013 14:03:48 -0700
Date: Thu, 13 Jun 2013 17:03:48 -0400
From: Jake the Dog <jake@adventuretime.ooo>
To: reply+QQd8df8370b7e95c5a49fbf86aeb2c93@discourse.example.com
To: reply+QQd8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo
Message-ID: <CADkmRc+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
Subject: re: [Discourse Meta] eviltrout posted in 'Adventure Time Sux'
Mime-Version: 1.0

View file

@ -167,8 +167,21 @@ describe Jobs::PollMailbox do
email.should be_deleted
end
it "works with multiple To addresses" do
email = MockPop3EmailObject.new fixture_file('emails/multiple_destinations.eml')
expect_success
poller.handle_mail(email)
new_post = Post.find_by(topic: topic, post_number: 2)
assert new_post.present?
assert_equal expected_post.strip, new_post.cooked.strip
email.should be_deleted
end
describe "with the wrong reply key" do
let(:email) { MockPop3EmailObject.new fixture_file('emails/wrong_reply_key.eml')}
let(:email) { MockPop3EmailObject.new fixture_file('emails/wrong_reply_key.eml') }
it "raises an EmailLogNotFound error" do
expect_exception Email::Receiver::EmailLogNotFound