query: tag:ActionMailer

複数アプリを動かしている場合、アプリ毎にメールアドレスが欲しくなることがあります。
今回はpostfixのvirtual domain、virtual aliasを使って実現しました。1つのサーバ(host名:x.s21g.com)で、a.s21g.com, b.s21g.comのメール受信ができるようになります。流れとしては、

  1. satoko@b.s21g.comで受信したメールを
  2. satoko_at_b@a.s21g.comに転送
  3. それをscript/runncerで処理ということになります。

###mailnameの用意
myoriginパラメータで使う名前を設定します。
shell>>

cat /etc/mailname

a.s21g.com
<<--

###virtual aliasesの用意
メール受信時どういう処理をするかです: railsの場合script/runnerに食わせます。
PATHはそれぞれ書き換えてください。
shell>>

cat /etc/aliases

postmaster: root

#post_at_b: "| cat $1 > /tmp/mail_test.txt" #test用コード
post_at_b: "| /PATH_TO_RUBY/ruby /PATH_TO_RAILS_ROOT/script/runner -e production 'TestMailer.receive(STDIN.read)'"
<<--

###virtual domainの用意
virtual domainに来た場合どうするかを個別を書きます。
shell>>

cat /etc/postfix/virtual

b.s21g.com anything
post@b.s21g.com post_at_b
<<--

###postfixの設定を変更
mydestinationにb.s21g.comを設定、myorigin, virtual alias、virtual domainも指定します。

shell>>
#/etc/postfix/main.cf
mydestination = localhost, localhost.localdomain, localhost, b.s21g.com
myorigin = /etc/mailname
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
virtual_alias_maps = hash:/etc/postfix/virtual
<<--

###各設定を反映
shell>>

postalias /etc/aliases

postmap /etc/postfix/virtual

/etc/rc.d/init.d/postfix restart

<<--

(注意) a.s21g.comはDNSに登録してください。メール送信時、インターネット上でDNSが牽ける名前か相手サーバからチェックされており、その時点で蹴られてしまうからです。
(注意) スパムに使われないようにリレーを設定します:main.cf
relayhost =
mynetworks = 127.0.0.0/8

###Refs
http://blog.s21g.com/articles/569
http://www.tmtm.org/postfix/tutorial/index.html
http://furukawa.blogdns.com/furukawa/2006/04/postfix.html
http://www.jitaku-server.net/postfix_virtual_domain.html

posted by satoko satoko on Tue 13 May 2008 at 16:57 with 0 comments

script/generate mailerでgenerateされるテストコードの@expectedはTMailのインスタンスらしく、生すぎなことがあります。
というわけで、お手軽にテストする方法をぐぐりました。

via http://sablog.com/archives/2006/03/14/how-to-test-actionmailer-in-ruby-on-rails

rails>>
class SampleMailerTest < ActionMailer::TestCase
tests SampleMailer

def setup
# テスト時に配送したメールの配列を保存する。
ActionMailer::Base.deliveries = []
end

def test_welcome
to = "satoko@s21g.com"
sender = to
sender_name = "satoko"

SampleMailer.deliver_welcome(to, sender, sender_name)
assert !ActionMailer::Base.deliveries.empty?

sent = ActionMailer::Base.deliveries.first
assert_equal [to], sent.to
assert_equal "expected title", sent.subject
assert sent.body =~ /^Recommended by #{sender_name}. Enjoy!$/

end
end
<<--

###Refs
http://wota.jp/ac/?date=20050731

posted by satoko satoko on Tue 22 Apr 2008 at 16:34 with 0 comments

いつも忘れるのでメモ。

###mailerオプション
script/generate mailer class_name method_name
shell>>
$ rails -v
Rails 2.0.2
$ script/generate mailer TestMailer welcome
exists app/models/
create app/views/test_mailer
exists test/unit/
create test/fixtures/test_mailer
create app/models/test_mailer.rb
create test/unit/test_mailer_test.rb
create app/views/test_mailer/welcome.erb
create test/fixtures/muji_mailer/welcome
<<--
viewは***.erb**形式なんですね。

###添付とbodyにURLを追加する時の注意

  • URLをbodyに渡すにはurl_forが使えます。

  • attachmentはメソッドなので@はいりません(ココはまりました)。

  • ファイルを複数添付の時はattachmentを複数回呼べばOKです。
    rails>>
    @body = {:sender => "satoko",
    :message => "welcome",
    :s21g_url => url_for(:host => "www.s21g.com", :controller => "top", :action => "index")
    }

    attachment :content_type => "image/png",
    :body => File.read("an-image.png"),
    :filename = "test.png"

    attachment :content_type => "image/png",
    :body => File.read("an-image2.png"),
    :filename = "test2.png"

<<--

###generatorのmailerヘルプ
下記でコマンドでヘルプが表示できます
shell>>
$ script/generate mailer --help
<<--

###Refs
http://wota.jp/ac/?date=20050731
http://rails-recipebook.g.hatena.ne.jp/rrbk/?word=*%5BMail%5D

posted by satoko satoko on Thu 17 Apr 2008 at 11:14 with 0 comments