2014-08-29 119 views
1

我试图让用户一旦登录到我的网络应用程序,就会从他们的个人资料页面向应用程序外的某个人发送电子邮件。邮件为什么发送失败?

当我按下模式中的“发送电子邮件”时,我收到通知'电子邮件失败'。任何想法为什么它失败?我一直试图解决这一个星期,所以真的很感谢一些帮助!谢谢!

路线:

devise_for :users 

    resources :profiles do 
    put :email_profile 
    end 

档案控制器:

def email_profile 
    @profile = Profile.find(params[:profile_id]) 
    destination = params[:to] 
    share = Share.profile(@profile, destination) 
    if destination =~ /@/ && share.deliver 
     redirect_to @profile, notice: 'email sent' 
    else 
     redirect_to @profile, notice: 'email failed' 
    end 
    end 

资料显示视图:

<!-- Button trigger modal --> 
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> 
    Email 
</button> 

<!-- Modal --> 
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
     <h4 class="modal-title" id="myModalLabel">Share my Info with someone</h4> 
     </div> 
     <div class="modal-body"> 
     <%= form_tag profile_email_profile_path(@profile), method: :put do %> 
      <%= label_tag :destination, "What email address would you like to send this to?" %> 
      <%= text_field_tag :destination %> 
     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button> 
     <%= submit_tag "Send Email", class: "btn btn-primary" %> 
     <% end %> 
     </div> 
    </div> 
    </div> 
</div> 

分享邮件:

class Share < ActionMailer::Base 
    default_url_options[:host] = "localhost:3000" 
    default from: "[email protected]" 

    def profile(profile, destination) 
    @profile = profile 
    mail(to: destination, subject: "sent you stuff") 
    end 
end 

profile.htmnl.erb

<p>sent you their information.</p> 

profile.text.erb

sent you their information. 

环境/开发:

# Don't care if the mailer can't send. 
    config.action_mailer.raise_delivery_errors = true 

    config.action_mailer.delivery_method = :smtp 

    # Gmail SMTP server setup 
    ActionMailer::Base.smtp_settings = { 
    :address => "smtp.gmail.com", 
    :enable_starttls_auto => true, 
    :port => 587, 
    :authentication => :plain, 
    :user_name => "[email protected]com", 
    :password => '46292839' 
    } 
# Raises error for missing translations 
    # config.action_view.raise_on_missing_translations = true 
    config.action_mailer.default_url_options = { :host => 'localhost:3000' } 

    # false prevents mail from being sent in development environment 
    config.action_mailer.perform_deliveries = true 

终端:

Started PUT "/profiles/1/email_profile" for 127.0.0.1 at 2014-08-29 12:12:44 +1000 
Processing by ProfilesController#email_profile as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"lNgA2/SNZCifVx33Hp53YAQxRPRW7lRsHsJuETSNKgE=", "destination"=>"[email protected]", "commit"=>"Send Email", "profile_id"=>"1"} 
    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1 
    Profile Load (0.1ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = ? LIMIT 1 [["id", 1]] 
    Rendered share/profile.html.erb (0.3ms) 
    Rendered share/profile.text.erb (0.2ms) 

Share#profile: processed outbound mail in 11.0ms 
Redirected to http://localhost:3000/profiles/1 
Completed 302 Found in 16ms (ActiveRecord: 0.3ms) 


Started GET "/profiles/1" for 127.0.0.1 at 2014-08-29 12:12:44 +1000 
Processing by ProfilesController#show as HTML 
    Parameters: {"id"=>"1"} 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1 
    Profile Load (0.1ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = ? LIMIT 1 [["id", 1]] 
    Rendered profiles/show.html.erb within layouts/application (0.9ms) 
    Profile Load (0.1ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = ? LIMIT 1 [["user_id", 1]] 
Completed 200 OK in 10ms (Views: 8.8ms | ActiveRecord: 0.3ms) 
+0

在你的'email_profile'行动你有'目标=参数[ :to]'但不应该是'destination = params [:description]'? – mmichael 2014-08-29 05:10:28

+0

为什么会这样:描述?描述从哪里来? – sss333 2014-08-29 05:27:16

+1

我的歉意,这是深夜! “目的地”而不是“描述”。在你的'form_tag'中你有'<%= text_field_tag:destination%>'这将使你的控制器可以通过'params [:destination]'访问该值。 – mmichael 2014-08-29 13:55:42

回答

0

在你的控制器,你拥有这个 行:

destination = params[:to]

但你没有与名称to任何PARAM所以更改线路是:

destination = params[:destination]