- 在config/environments/development.rb文件中追加邮件设置代码。
- 拷贝两个model文件到models目录下。[user_notify.rb,iso2022jp_mailer.rb]
- 在user_notify.rb文件中编写发送邮件的代码。
- 编写邮件模板。
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "xxxxx",
:domain => "xxxx",
:port => 25,
:authentication => :login,
:user_name => "rails",
:password => "rails",
}
class UserNotify < Iso2022jpMailer
def ip_not_match(ip, user)
@recipients = user.email
@from = APP_CONFIG[:from_email]
@sent_on = Time.now
@headers['Content-Type'] = "text/plain; charset=iso2022-JP; format=flowed"
@subject = base64("Login Ip not match")
@body["login"] = user.login
@body["ip"] = ip
@body["last_ip"] = user.ip
end
end
<%= @login %>
Your login ip not match with last login ip.
Last IP: <%= @last_ip%>
This IP: <%= @ip %>
def self.send_email(ip, admin)
UserNotify.deliver_ip_not_match(ip, admin)
end