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.

336 CHAPTER 12 Sending email<br />

Listing 12.12 features/gmail.feature<br />

Scenario: Receiving a real-world email<br />

Given I am signed in as "alice@ticketee.com"<br />

Given I am on the homepage<br />

When I follow "TextMate 2"<br />

And I follow "Release date"<br />

And I fill in "Text" with "Posting a comment!"<br />

And I press "Create Comment"<br />

Then I should see "Comment has been created."<br />

When I log into gmail with:<br />

| username | password |<br />

| youraccount@gmail.com | your_password |<br />

Then there should be an email from Ticketee in my inbox<br />

When you run this feature using bin/cucumber features/gmail.feature, you see that<br />

your feature fails when alice@ticketee.com clicks the Create Comment button:<br />

And I press "Create Comment"<br />

Connection refused - connect(2) (Errno::ECONNREFUSED)<br />

.../net/smtp.rb:551:in `initialize'<br />

Remember before how it was mentioned that Action Mailer would (by default) try to<br />

connect to an SMTP server running on localhost? That’s what is happening here,<br />

because when a comment is updated a notification will be sent out. You didn’t see<br />

this previously because it’s only now that you’ve switched delivery_method to :smtp,<br />

You don’t have one running locally 12 so it’s unable to connect. You can tell that it’s<br />

now using SMTP, because the first line of the stacktrace points to net/smtp.rb in<br />

Ruby’s standard library, which is what Action Mailer (by way of Mail) uses to connect<br />

to SMTP servers. Therefore, you must change something in order to make this work<br />

once more.<br />

12.3.2 Configuring Action Mailer<br />

To fix this error, you must tell Action Mailer to connect to your Gmail server so that it<br />

has a way to send out emails. You can create a new file in config/initializers that provides<br />

Action Mailer with the necessary information it needs. But what would this information<br />

be? Well, let’s hop on over to Google’s “Configuring other mail clients” 13 page,<br />

where you’ll see the table from figure 12.9.<br />

You’re trying to send email, so you want to use the Outgoing Mail section, which<br />

tells you to use smtp.gmail.com as the server. You’ll connect to it using TLS, so you’ll<br />

connect on port 587. The account name and password should be the Gmail address<br />

and password for your email address. With these settings, create a config/initializers/<br />

mail.rb file that looks like the following listing.<br />

12 Unless you’ve got it set up from some other place that’s not this book.<br />

13 http://mail.google.com/support/bin/answer.py?hl=en&answer=13287.

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

Saved successfully!

Ooh no, something went wrong!