如果您將通過Mail
的文檔進一步閱讀,你會發現一個很好的替代解決方案,將工作。而不是使用:
Mail.deliver do
to @recipient # throws error as this is undefined
from '[email protected]'
subject 'testing sendmail'
body 'testing sendmail'
end
可以使用郵件的new()
方法,傳遞參數,並忽略塊:
Mail.new(
to: @recipient,
from: '[email protected]',
subject: 'testing sendmail',
body: 'testing sendmail'
).deliver!
或備選哈希元素定義:
Mail.new(
:to => @recipient,
:from => '[email protected]',
:subject => 'testing sendmail',
:body => 'testing sendmail'
).deliver!
在撬,或irb你會看到:
pry(main)> Mail.new(
pry(main)* to: '[email protected]',
pry(main)* from: '[email protected]' << `hostname`.strip,
pry(main)* subject: 'test mail gem',
pry(main)* body: 'this is only a test'
pry(main)*).deliver!
=> #<Mail::Message:59273220, Multipart: false, Headers: <Date: Fri, 28 Oct 2011 09:01:14 -0700>, <From: [email protected]>, <To: [email protected]>, <Message-ID: <[email protected]>>, <Subject: test mail gem>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>
new
方法有多種可供使用的變體。這是從文檔還,而且可能效果更好:
作爲一個側面說明,你也可以創建一個新的電子郵件,通過直接創建郵件:: Message對象,然後通過串,符號或直接在價值傳遞方法調用。有關更多信息,請參閱Mail :: Message。
mail = Mail.new
mail.to = '[email protected]'
mail[:from] = '[email protected]'
mail['subject'] = 'This is an email'
mail.body = 'This is the body'
其次mail.deliver!
。
另請注意,在前面的示例中,有多種方式可以訪問郵件信封中的各種標題。這是一個靈活的寶石,似乎被深思熟慮,很好地遵循Ruby的方式。
你確定你的問題是與實例var,而不是'params [:email]'?你嘗試過輸出嗎?在這裏也應該有一個局部變量,因爲塊無論如何都是關閉的。 –