27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

340 CHAPTER 12 Sending email<br />

12.4 Receiving emails<br />

You’d now like to add a feature to Ticketee where users can reply to the email notifications<br />

for a new comment on a ticket, and by replying create a new comment with their<br />

text. Many other applications do this by having an email such as this:<br />

== ADD YOUR REPLY ABOVE THIS LINE ==<br />

Bob has just updated the "Due date" ticket for "TextMate 2"<br />

Text above the “ADD YOUR REPLY ABOVE THIS LINE” will be parsed out and turned<br />

into a new object. In Ticketee, this would be a comment.<br />

In the previous section, you learned how you could connect to a Gmail account to<br />

check to see if there was an email that had a subject beginning with “[ticketee].” You<br />

can use the same method in order to check for replies to your emails too, but you<br />

need to make one small modification.<br />

To determine what ticket and project the reply is directed at, you need to tag the<br />

emails in a certain way. The best way to do this is to add the tags to the email addresses<br />

themselves, so that an email address with a tag looks like ticketee+tag@gmail.com,<br />

where the +tag part of the email is ignored and the email arrives in ticketee<br />

@gmail.com’s mailbox. For your emails, you’ll set a reply-to address such as ticketee<br />

+61+23@gmail.com, where the first number is the project ID and the second number<br />

is the ticket ID.<br />

You’re not going to post comments straight from emails. You need to check to see<br />

if the user has permission to view the project where the ticket is, which means that<br />

they would be able to create a comment for that ticket too. If they’re unable to post a<br />

comment to that ticket, you assume the user is trying to do something malicious and<br />

just ignore their email.<br />

To parse these emails, you’ll be using the receive method in an ActionMailer<br />

class, which takes an email object and allows you to process it.<br />

A quick summary: you’re going to use the gmail gem to check for emails in your<br />

inbox that are replies to comment notifications and then parse them using Action<br />

Mailer into new Comment objects. If a user is restricted from viewing a project, then<br />

you’ll ignore their emails.<br />

First, you want to check that the outgoing email contains the tag on the “from”<br />

address, so that when a user replies to it you know what project and ticket they’re<br />

replying to.<br />

12.4.1 Setting a reply-to address<br />

By having a different “from” address set on the outgoing email, you’ll be able to determine<br />

what project and ticket the user’s reply comment should be created on. To<br />

ensure that all outgoing emails from the comment_updated method in Notifier have<br />

this set, you’re going to write a simple test.<br />

Let’s open spec/mailers/notifier_spec.rb and change this whole file to what’s<br />

shown in the following listing.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!