2016-04-14 22 views
1

我真的很爲此苦苦掙扎。任何人都可以看到這裏有什麼問題?我似乎無法使郵件工作......我正在嘗試在我的網站上創建聯繫表單,以便用戶可以向我發送網站中的電子郵件。我創建一個名稱,電子郵件,主題和正文的「聯繫人」,然後將其作爲電子郵件發送。在導軌中設置郵件0_o

控制器/ contacts_controller.rb

class ContactsController < ApplicationController 
    def index 
    end 

    def new 
    @contact = Contact.new 
    end 

    def create 
    @contact = Contact.new(contact_params) 
    respond_to do |format| 
     if @contact.save 
     UserMailer.mail_jill(@contact).deliver 

     format.html { redirect_to(@contact, :notice => 'User was successfully created.') } 
     else 
     format.html { render :action => "new" } 
     format.json { render :json => @contact.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 


    def contact_params 
    params.require(:contact).permit(:name, :email, :subject, :body) 
    end 
end 

郵寄/ user_mailer.rb

class UserMailer < ActionMailer::Base 
    def mail_jill(message) 
    @name = message.name 
    @email = message.email 
    @subject = message.subject 
    @body = message.body 

    mail(to: '<my_email_address>', subject: @subject, body: @body, from: @email) 
    end 
end 

的意見/ user_mailer文件/ mail_jill.html.erb:

<h1>From, <%= @contact.name %></h1> 
<p> 
@contact.body 
</p> 
<p>-<%= @contact.email %></p> 

$ RAILS_ENV.rb:

ActionMailer::Base.delivery_method = :smtp 
ActionMailer::Base.sendmail_settings = { 
    :address    => "smtp.gmail.com", 
    :port     => "465", 
    :domain    => "gmail.com", 
    :user_name   => "<my_gmail_address>", 
    :password    => "<my_gmail_password>", 
    :authentication  => "plain", 
    :enable_starttls_auto => true 
} 

配置/環境/

development.rb:

# mailer 
    config.active_support.deprecation = :log 
    config.action_mailer.delivery_method = :smtp 
    config.action_mailer.perform_deliveries = true 
    config.action_mailer.default_url_options = { :host => "localhost:3000" } 
    config.action_mailer.delivery_method = :smtp 

    config.action_mailer.smtp_settings = { 
    :address    => ENV['address'], 
    :port     => ENV['port'], 
    :user_name   => ENV['gmail_username'], 
    :password    => ENV['gmail_password'], 
    :authentication  => ENV['authentication'], 
    :enable_starttls_auto => true 

命令行輸出完整的錯誤從活動網站提交的電子郵件後:

2016-04-15T23:40:12.932560+00:00 app[web.1]: Sent mail to [email protected] (348.5ms) 
2016-04-15T23:40:12.932577+00:00 app[web.1]: Date: Fri, 15 Apr 2016 23:40:12 +0000 
2016-04-15T23:40:12.932579+00:00 app[web.1]: From: [email protected] 
2016-04-15T23:40:12.932579+00:00 app[web.1]: To: [email protected] 
2016-04-15T23:40:12.932581+00:00 app[web.1]: Message-ID: <[email protected]27267.mail> 
2016-04-15T23:40:12.932583+00:00 app[web.1]: Subject: hi 
2016-04-15T23:40:12.932583+00:00 app[web.1]: Mime-Version: 1.0 
2016-04-15T23:40:12.932584+00:00 app[web.1]: Content-Type: text/plain; 
2016-04-15T23:40:12.932585+00:00 app[web.1]: charset=UTF-8 
2016-04-15T23:40:12.932585+00:00 app[web.1]: Content-Transfer-Encoding: 7bit 
2016-04-15T23:40:12.932586+00:00 app[web.1]: 
2016-04-15T23:40:12.932587+00:00 app[web.1]: hiiiii 
2016-04-15T23:40:12.932687+00:00 app[web.1]: Completed 500 Internal Server Error in 367ms 
2016-04-15T23:40:12.933562+00:00 app[web.1]: 
2016-04-15T23:40:12.933579+00:00 app[web.1]: Net::SMTPAuthenticationError (530-5.5.1 Authentication Required. Learn more at 
2016-04-15T23:40:12.933580+00:00 app[web.1]:): 
2016-04-15T23:40:12.933581+00:00 app[web.1]: app/controllers/contacts_controller.rb:12:in `create' 
2016-04-15T23:40:12.933582+00:00 app[web.1]: 
2016-04-15T23:40:12.933583+00:00 app[web.1]: 
+0

當您嘗試發送郵件時發生了什麼?你有錯誤嗎? – sixty4bit

+0

@ sixty4bit我添加了完整的錯誤輸出^ –

+0

端口號是否正確? – hypern

回答

2

試試這個

config.action_mailer.smtp_settings = { 
    address:    'smtp.gmail.com', 
    port:     587, 
    domain:    'gmail.com', 
    user_name:   ENV['gmail_username'], 
    password:    ENV['gmail_password'], 
    authentication:  'plain', 
    enable_starttls_auto: true 
} 
+0

謝謝!這在當地有效,但在生產站點仍然沒有運氣:{ –

+0

可能是防火牆? –

+0

嗯,好吧,我已經更新了我的錯誤日誌,以反映當我從活動網站發送電子郵件時會發生什麼...... –

1

得到它的工作。感謝志良的回答讓我更加接近。最後,我的$ RAILS_ENV文件錯了。我從我的環境文件夾中刪除了該文件,並將smtp_settings.rb文件添加到初始化文件夾。以下是初始化程序/ smtp_settings.rb的內容:

ActionMailer::Base.smtp_settings = { 
    :address => "smtp.gmail.com", 
    :port => 587, 
    :domain => "gmail.com", 
    :user_name => "<my_email_address>", 
    :password => "<my_gmail_password>", 
    :authentication => :plain, 
    :enable_starttls_auto => true 
}