2016-12-14 26 views
0

ArgumentError at /contacts An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.的SMTP解決的是需要發送消息/參數Errror

好日子夥計們,我無法調試此我自己和消耗瞭如此多的時間,我需要你的幫助,並解釋如何,爲什麼它沒沒有工作。

事情我已經做了: 1.添加寶石「binding_of_caller」 2.bundle安裝

目標: 是訪問者提交應該聯絡表格會自動給我一個電子郵件。

我的悲哀/困惑:

1.How你設置你的電子郵件中secrets.yml或者直接把你的電子郵件,其中您配置的連接形式要求您的電子郵件 2.什麼我所做的是把 到:myemail.com> secrets.yml在生產和開發 3.Am我對不對?

請向我解釋這一點,因爲我正在深入研究軌道上的紅寶石。

+0

有關如何設置電子郵件的Rails指南非常清楚調用程序的綁定與電子郵件無關。顯示1. Mailer應該採取什麼行動(有時這是在模型中,有時在控制器中)向我們展示控制器和模型,以便捕獲訪問者信息的位置。是否填充:users表?首先發布這些信息和確切的錯誤信息,然後你會從我或其他人那裏得到幫助。 –

+0

好吧,先生,謝謝,我會 – aspiring

回答

0

您需要先生成郵件程序。

rails g mailer example_mailer 

app/mailers/example_mailer.rb該文件將由該行生成。

然後,您需要在郵件程序中創建一個方法,您可以在其中定義發送郵件的人。你可以給直接的電子郵件ID,或者你可以從數據庫中獲取。正如我在給出的例子中,你需要將用戶傳遞給這個方法,它會發送電子郵件給該用戶的電子郵件。

def sample_email(user) 
    @user = user 
    mail(to: @user.email, subject: 'Sample Email') 
    end 

創建您的郵件會如何看

應用程序/視圖/ example_mailer/sample_email.html.erb

<!DOCTYPE html> 
<html> 
    <head> 
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> 
    </head> 
    <body> 
    <h1>Hi <%= @user.name %></h1> 
    <p> 
     Sample mail sent using smtp. 
    </p> 
    </body> 
</html> 

然後,你需要配置SMTP一個HTML模板在/config/environments/production.rb或/config/environments/development.rb中,無論您使用何種環境。

config.action_mailer.delivery_method = :smtp 
# SMTP settings for gmail 
config.action_mailer.smtp_settings = { 
:address    => "smtp.gmail.com", 
:port     => 587, 
:user_name   => '[email protected]', 
:password    => 'Gmail password', 
:authentication  => "plain", 
:enable_starttls_auto => true 
} 

然後你需要觸發你的郵件動作從你發送郵件的任何方法。

ExampleMailer.sample_email(@user).deliver 
+0

謝謝先生,仍然在學習曲線上,但是我需要閱讀我已經注意到了這個問題,隨着我的進展,我將在稍後解決這個問題。謝謝你的幫助 – aspiring

相關問題