#6 ActionMailで会員登録時にメール送信する
会員登録時にメールを送信し、クリック後会員登録完了とすることで
メールアドレスのチェックをおこなう。
railsではActionMailerという仕組みにより、簡単にテンプレートメールを送信できます。
以下5つのSTEPで実装できます。
1) Rails環境ファイルにメールサーバを設定
以下environment.rbファイルにメールサーバの設定をする
- mytask/config/environment.rbに以下追加(下線部は環境によって変更する)
# Include your application configuration below
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
:address => "address",
:port => 25,
:domain => 'domain',
:user_name => 'user_name',
:password => 'password',
:authentication => :login
}
2) generateを使ってActionMailerを作成する
モデルとして作成されるので注意する。テンプレートファイルはviewファイルとして生成される
$ ruby script/generate mailer AccountMailer sent
exists app/models/
create app/views/account_mailer
exists test/unit/
create test/fixtures/account_mailer
create app/models/account_mailer.rb
create test/unit/account_mailer_test.rb
create app/views/account_mailer/sent.rhtml
create test/fixtures/account_mailer/sent
3) model(accout_mailer)の編集
※app/models/account_mailer.rbに以下メソッドを追加する
$ ruby script/generate mailer AccountMailer sent
exists app/models/
create app/views/account_mailer
exists test/unit/
create test/fixtures/account_mailer
create app/models/account_mailer.rb
create test/unit/account_mailer_test.rb
create app/views/account_mailer/sent.rhtml
create test/fixtures/account_mailer/sent
4) メールのテンプレートファイルを編集する
※mytask/app/views/account_mailer/sent.rhtml を編集する5) controllerからメールモデルを呼び出す
def sendmail
@user = User.find(params[:id])
AccountMailer.deliver_sent(@user)
end
これで完了、5分くらいでできちゃいます。
トラックバック(0)
このブログ記事に対するトラックバックURL(トラックバックは承認後に公開されます)


コメントする