trouble with email with one click

谁说胖子不能爱 提交于 2020-01-17 04:08:33

问题


I just launched my rails app with heroku (http://fast-reaches-9399.herokuapp.com/). I want people to be able to type stuff in the textarea on the top, and then click the submit button, and then have it email me what is in the textarea. I'm really having trouble with it though. It redirects in the development environment, but doesn't seem to send an email. And I get a 500 error in production. Here's some relevant code...

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

  def email
    UserMailer.idea_bar(params[:message]).deliver
    redirect_to "/"  # how can I redirect to the page the user was just on?
  end
end

/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  default from: "anonymous@collegeanswers.com", to: "info@collegeanswerz.com", subject: "collegeanswers"

  def idea_bar(message)
    mail(:body => message)
  end
end

application.html.erb (layout file)

<div class="idea_bar">
    <div class="left_idea_bar">
        <p>Ideas, comments, questions, suggestions?  Help us help you!</p>
        <%= form_tag("/application_controller/email", :method => "post", :id => "idea_bar_form") do %>
          <%= text_area_tag('message', nil, :size => "60x4", :id => "message", :name => "message") %> <br />
          <%= label_tag(:email, "• include your email if you want a response:") %>
          <%= text_field_tag(:email, nil, :id => "email", :size => "36") %>
    </div>
          <button id="submit" type="submit" form="idea_bar_form"><span id="big_submit">Send</span>
          <span id="small_submit"><br />&#8226 one click<br />&#8226 anonymous</span></button>
        <% end %>
</div>

/user_mailer/idea_bar.html.erb (test file. I want the email to be what's in the textarea, not this)

<html>
    <head></head>
    <body>
        <p>hello</p>
    </body>
</html>

/user_mailer/idea_bar.text.erb

hello

logs

~/collegeanswerz >>  heroku logs -n 100
2013-06-24T02:10:39.511211+00:00 app[web.1]: [2013-06-24 02:10:39] INFO  WEBrick::HTTPServer#start: pid=2 port=37425
2013-06-24T02:10:39.628181+00:00 heroku[web.1]: State changed from starting to up
2013-06-24T02:10:39.971477+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-06-24T02:10:41.046475+00:00 app[web.1]: [2013-06-24 02:10:41] ERROR SignalException: SIGTERM
2013-06-24T02:10:41.046475+00:00 app[web.1]:    /app/vendor/ruby-1.9.3/lib/ruby/1.9.1/webrick/server.rb:98:in `select'
2013-06-24T02:10:46.399057+00:00 app[web.1]: Started GET "/" for 150.212.18.15 at 2013-06-24 02:10:46 +0000
2013-06-24T02:10:46.588166+00:00 heroku[router]: at=info method=GET path=/ host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=1ms service=198ms status=200 bytes=14988
2013-06-24T02:10:46.547918+00:00 app[web.1]: Processing by StaticPagesController#home as HTML
2013-06-24T02:10:46.581761+00:00 app[web.1]:   Rendered static_pages/home.html.erb within layouts/application (3.8ms)
2013-06-24T02:10:46.584211+00:00 app[web.1]: Completed 200 OK in 36ms (Views: 35.8ms | ActiveRecord: 0.0ms)
2013-06-24T02:10:46.827096+00:00 heroku[router]: at=info method=GET path=/assets/home-73d942132cfdcc305dabf385494f8201.css host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=1ms service=6ms status=200 bytes=1884
2013-06-24T02:10:46.814074+00:00 heroku[router]: at=info method=GET path=/assets/layout-eae006a3ffc93619e067580f95bb8a9a.css host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=3ms service=10ms status=200 bytes=1643
2013-06-24T02:10:46.834802+00:00 heroku[router]: at=info method=GET path=/assets/application-e432bc54db845c729a762e0d298d3579.js host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=2ms service=15ms status=200 bytes=156605
2013-06-24T02:10:47.262693+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=4ms service=6ms status=200 bytes=0
2013-06-24T02:10:49.760439+00:00 heroku[web.1]: Stopping remaining processes with SIGKILL
2013-06-24T02:10:49.760205+00:00 heroku[web.1]: Error R12 (Exit timeout) -> At least one process failed to exit within 10 seconds of SIGTERM
2013-06-24T02:10:52.386481+00:00 heroku[web.1]: Process exited with status 137
2013-06-24T02:15:55.505285+00:00 app[web.1]: Started GET "/" for 67.171.66.44 at 2013-06-24 02:15:55 +0000
2013-06-24T02:15:55.510268+00:00 app[web.1]: Processing by StaticPagesController#home as HTML
2013-06-24T02:15:55.512568+00:00 app[web.1]:   Rendered static_pages/home.html.erb within layouts/application (1.6ms)
2013-06-24T02:15:55.514297+00:00 app[web.1]: Completed 200 OK in 4ms (Views: 3.7ms | ActiveRecord: 0.0ms)
2013-06-24T02:15:55.518380+00:00 heroku[router]: at=info method=GET path=/ host=fast-reaches-9399.herokuapp.com fwd="67.171.66.44" dyno=web.1 connect=7ms service=23ms status=200 bytes=14988
2013-06-24T02:15:55.739788+00:00 heroku[router]: at=info method=GET path=/assets/layout-eae006a3ffc93619e067580f95bb8a9a.css host=fast-reaches-9399.herokuapp.com fwd="67.171.66.44" dyno=web.1 connect=3ms service=6ms status=200 bytes=1643
2013-06-24T02:15:55.786186+00:00 heroku[router]: at=info method=GET path=/assets/home-73d942132cfdcc305dabf385494f8201.css host=fast-reaches-9399.herokuapp.com fwd="67.171.66.44" dyno=web.1 connect=2ms service=5ms status=200 bytes=1884
2013-06-24T02:15:55.854372+00:00 heroku[router]: at=info method=GET path=/assets/application-e432bc54db845c729a762e0d298d3579.js host=fast-reaches-9399.herokuapp.com fwd="67.171.66.44" dyno=web.1 connect=3ms service=26ms status=200 bytes=156605
2013-06-24T02:19:33.509774+00:00 app[web.1]: Started GET "/colleges" for 150.212.18.15 at 2013-06-24 02:19:33 +0000
2013-06-24T02:19:33.513257+00:00 app[web.1]: Processing by StaticPagesController#colleges as HTML
2013-06-24T02:19:33.523421+00:00 heroku[router]: at=info method=GET path=/colleges host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=2ms service=21ms status=304 bytes=0
2013-06-24T02:19:33.524174+00:00 app[web.1]: Completed 200 OK in 11ms (Views: 10.4ms | ActiveRecord: 0.0ms)
2013-06-24T02:19:33.522339+00:00 app[web.1]:   Rendered static_pages/colleges.html.erb within layouts/application (7.7ms)
2013-06-24T02:19:33.753391+00:00 heroku[router]: at=info method=GET path=/assets/application-e432bc54db845c729a762e0d298d3579.js host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=2ms service=5ms status=304 bytes=0
2013-06-24T02:19:33.735957+00:00 heroku[router]: at=info method=GET path=/assets/layout-eae006a3ffc93619e067580f95bb8a9a.css host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=2ms service=6ms status=304 bytes=0
2013-06-24T02:19:33.811360+00:00 heroku[router]: at=info method=GET path=/assets/colleges-7adcb0138796cf2dd39ad973ccd58699.css host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=5ms service=6ms status=200 bytes=232
2013-06-24T02:19:33.972423+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=1ms service=6ms status=304 bytes=0
2013-06-24T02:19:35.619992+00:00 app[web.1]: Started GET "/university-of-pittsburgh" for 150.212.18.15 at 2013-06-24 02:19:35 +0000
2013-06-24T02:19:35.624209+00:00 app[web.1]:   Parameters: {"college"=>"university-of-pittsburgh"}
2013-06-24T02:19:35.624209+00:00 app[web.1]: Processing by CollegePagesController#college_page as HTML
2013-06-24T02:19:35.876371+00:00 app[web.1]:   Rendered college_pages/college_page.html.erb within layouts/application (20.9ms)
2013-06-24T02:19:35.878070+00:00 app[web.1]: Completed 200 OK in 254ms (Views: 50.0ms | ActiveRecord: 22.2ms)
2013-06-24T02:19:35.902402+00:00 heroku[router]: at=info method=GET path=/university-of-pittsburgh host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=7ms service=289ms status=200 bytes=29578
2013-06-24T02:19:35.993171+00:00 heroku[router]: at=info method=GET path=/assets/college_pages-72b79bbce46f6e1c4a7b947a635cb3ef.css host=fast-reaches-9399.herokuapp.com fwd="150.212.18.15" dyno=web.1 connect=2ms service=8ms status=200 bytes=4711
2013-06-24T03:23:03.750562+00:00 heroku[web.1]: Idling
2013-06-24T03:23:06.124578+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2013-06-24T03:23:06.590444+00:00 app[web.1]:    /app/vendor/ruby-1.9.3/lib/ruby/1.9.1/webrick/server.rb:98:in `select'
2013-06-24T03:23:06.590444+00:00 app[web.1]: [2013-06-24 03:23:06] ERROR SignalException: SIGTERM
2013-06-24T03:23:15.819421+00:00 heroku[web.1]: Stopping remaining processes with SIGKILL
2013-06-24T03:23:15.819273+00:00 heroku[web.1]: Error R12 (Exit timeout) -> At least one process failed to exit within 10 seconds of SIGTERM
2013-06-24T03:23:17.542955+00:00 heroku[web.1]: Process exited with status 137
2013-06-24T03:23:17.555718+00:00 heroku[web.1]: State changed from up to down
2013-06-24T04:43:11.725754+00:00 heroku[web.1]: State changed from down to starting
2013-06-24T04:43:11.725389+00:00 heroku[web.1]: Unidling
2013-06-24T04:43:15.022448+00:00 heroku[web.1]: Starting process with command `bundle exec rails server -p 20121`
2013-06-24T04:43:19.525120+00:00 app[web.1]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/config/environment.rb:5)
2013-06-24T04:43:19.525120+00:00 app[web.1]: DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /app/config/environment.rb:5)
2013-06-24T04:43:28.871809+00:00 app[web.1]: => Call with -d to detach
2013-06-24T04:43:28.871809+00:00 app[web.1]: Connecting to database specified by DATABASE_URL
2013-06-24T04:43:28.871809+00:00 app[web.1]: => Rails 3.2.13 application starting in production on http://0.0.0.0:20121
2013-06-24T04:43:28.871809+00:00 app[web.1]: => Booting WEBrick
2013-06-24T04:43:28.871809+00:00 app[web.1]: => Ctrl-C to shutdown server
2013-06-24T04:43:29.530107+00:00 app[web.1]: [2013-06-24 04:43:29] INFO  WEBrick 1.3.1
2013-06-24T04:43:29.530107+00:00 app[web.1]: [2013-06-24 04:43:29] INFO  ruby 1.9.3 (2013-05-15) [x86_64-linux]
2013-06-24T04:43:29.530878+00:00 app[web.1]: [2013-06-24 04:43:29] INFO  WEBrick::HTTPServer#start: pid=2 port=20121
2013-06-24T04:43:30.022925+00:00 heroku[web.1]: State changed from starting to up
2013-06-24T04:43:31.838145+00:00 app[web.1]: Started GET "/" for 150.212.13.47 at 2013-06-24 04:43:31 +0000
2013-06-24T04:43:32.678465+00:00 app[web.1]: Processing by StaticPagesController#home as HTML
2013-06-24T04:43:32.889354+00:00 app[web.1]:   Rendered static_pages/home.html.erb within layouts/application (31.0ms)
2013-06-24T04:43:32.932875+00:00 app[web.1]: Completed 200 OK in 254ms (Views: 246.6ms | ActiveRecord: 0.0ms)
2013-06-24T04:43:32.949968+00:00 heroku[router]: at=info method=GET path=/ host=fast-reaches-9399.herokuapp.com fwd="150.212.13.47" dyno=web.1 connect=2ms service=1132ms status=304 bytes=0
2013-06-24T04:43:53.901496+00:00 app[web.1]: Started GET "/" for 24.131.255.163 at 2013-06-24 04:43:53 +0000
2013-06-24T04:43:53.905840+00:00 app[web.1]: Processing by StaticPagesController#home as HTML
2013-06-24T04:43:53.918816+00:00 app[web.1]:   Rendered static_pages/home.html.erb within layouts/application (1.1ms)
2013-06-24T04:43:53.924969+00:00 app[web.1]: Completed 200 OK in 19ms (Views: 7.6ms | ActiveRecord: 0.0ms)
2013-06-24T04:43:53.927908+00:00 heroku[router]: at=info method=GET path=/ host=fast-reaches-9399.herokuapp.com fwd="24.131.255.163" dyno=web.1 connect=1ms service=44ms status=200 bytes=14988
2013-06-24T04:43:54.371502+00:00 heroku[router]: at=info method=GET path=/assets/layout-eae006a3ffc93619e067580f95bb8a9a.css host=fast-reaches-9399.herokuapp.com fwd="24.131.255.163" dyno=web.1 connect=4ms service=12ms status=304 bytes=0
2013-06-24T04:43:54.400418+00:00 heroku[router]: at=info method=GET path=/assets/home-73d942132cfdcc305dabf385494f8201.css host=fast-reaches-9399.herokuapp.com fwd="24.131.255.163" dyno=web.1 connect=3ms service=27ms status=304 bytes=0
2013-06-24T04:43:54.404305+00:00 heroku[router]: at=info method=GET path=/assets/application-e432bc54db845c729a762e0d298d3579.js host=fast-reaches-9399.herokuapp.com fwd="24.131.255.163" dyno=web.1 connect=1ms service=18ms status=304 bytes=0
2013-06-24T04:43:54.987065+00:00 heroku[router]: at=info method=GET path=/favicon.ico host=fast-reaches-9399.herokuapp.com fwd="24.131.255.163" dyno=web.1 connect=2ms service=6ms status=304 bytes=0
2013-06-24T04:44:00.837309+00:00 app[web.1]: Started POST "/application_controller/email" for 24.131.255.163 at 2013-06-24 04:44:00 +0000
2013-06-24T04:44:00.845288+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"MubEQddsKvR/QBLZtZKCt8TF9EiUSHjSTpxtSB5mAhE=", "message"=>"testing", "email"=>""}
2013-06-24T04:44:00.845096+00:00 app[web.1]: Processing by ApplicationController#email as HTML
2013-06-24T04:44:00.946113+00:00 app[web.1]: 
2013-06-24T04:44:00.946113+00:00 app[web.1]: Sent mail to info@collegeanswerz.com (16ms)
2013-06-24T04:44:00.946346+00:00 app[web.1]: Completed 500 Internal Server Error in 101ms
2013-06-24T04:44:00.948747+00:00 app[web.1]: 
2013-06-24T04:44:00.948747+00:00 app[web.1]: Errno::ECONNREFUSED (Connection refused - connect(2)):
2013-06-24T04:44:00.948747+00:00 app[web.1]:   app/controllers/application_controller.rb:5:in `email'
2013-06-24T04:44:00.948747+00:00 app[web.1]: 
2013-06-24T04:44:00.948747+00:00 app[web.1]: 
2013-06-24T04:44:00.952511+00:00 heroku[router]: at=info method=POST path=/application_controller/email host=fast-reaches-9399.herokuapp.com fwd="24.131.255.163" dyno=web.1 connect=1ms service=126ms status=500 bytes=643
2013-06-24T04:44:13.657925+00:00 app[web.1]: Started POST "/application_controller/email" for 24.131.255.163 at 2013-06-24 04:44:13 +0000
2013-06-24T04:44:13.662188+00:00 app[web.1]: Processing by ApplicationController#email as HTML
2013-06-24T04:44:13.662374+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"MubEQddsKvR/QBLZtZKCt8TF9EiUSHjSTpxtSB5mAhE=", "message"=>"testing with test email", "email"=>"test email"}
2013-06-24T04:44:13.690119+00:00 heroku[router]: at=info method=POST path=/application_controller/email host=fast-reaches-9399.herokuapp.com fwd="24.131.255.163" dyno=web.1 connect=3ms service=42ms status=500 bytes=643
2013-06-24T04:44:13.690067+00:00 app[web.1]:   app/controllers/application_controller.rb:5:in `email'
2013-06-24T04:44:13.690067+00:00 app[web.1]: 
2013-06-24T04:44:13.686636+00:00 app[web.1]: 
2013-06-24T04:44:13.686636+00:00 app[web.1]: Sent mail to info@collegeanswerz.com (15ms)
2013-06-24T04:44:13.690067+00:00 app[web.1]: Errno::ECONNREFUSED (Connection refused - connect(2)):
2013-06-24T04:44:13.690067+00:00 app[web.1]: 
2013-06-24T04:44:13.690067+00:00 app[web.1]: 
2013-06-24T04:44:13.687154+00:00 app[web.1]: Completed 500 Internal Server Error in 25ms
~/collegeanswerz


回答1:


You're likely seeing the connection refused error because you're not being allowed to connect to the local SMTP server on Heroku, which is not allowed.

You'll need to use an external SMTP service. Heroku has plug-ins for several providers which offer external SMTP service listed on their addons page: (https://addons.heroku.com/#email-sms). Each add-on provides instructions on how to configure your application

You could also use a standard mail service for which you have an account and configure it through ActionMailer. You can try something like this in your config/environments/production.rb if you have a GMail account.

http://guides.rubyonrails.org/action_mailer_basics.html#example-action-mailer-configuration




回答2:


From log supplied it's clear that you get an error because Rails ActionMailer is unable to connect to SMTP server. To debug this problem in production you can change config in development.rb to this:

config.action_mailer.raise_delivery_errors = true

so you will see mailer exceptions in development. When you have exact error msg you can update log in question for further help.



来源:https://stackoverflow.com/questions/17264325/trouble-with-email-with-one-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!