スポンサーサイト
2013.07.04 Thursday
一定期間更新がないため広告を表示しています
はじめてのRuby on Rails、トラブル記録
sendgridのメールがgmailでスパム扱いされる
2013.07.04 Thursday
今公開してるRailsプロジェクトでは送信メールにGmailを使っているのだけれど、1日の送信件数が限界を超えそうなのでSendgridに移行するつもりでした。 なんだかんだ設定もすませてdevelopment環境で送信テストしたら……送信できたはずなのにメールが届かない(-_-; おかしいと思って迷惑メールフォルダを見たら、案の定スパム扱いにされてました……。 調べてみると、どうやらGmailでだけスパム扱いされるという問題がある模様↓ それ、すっごく困るんですが。。。使えないじゃん(-_-; 他をあたるしかないか……。 RailsでGmailのSMTP使ってメールを送る(action_mailer_tls)
2009.10.26 Monday
メールを送る機能を作りたいと思っていろいろ調べてみました。 ActionMailerを使って設定とかはできたんだけど、結局SMTPとか使わないと実際に送ることはできないし。 で、一番手軽にできるのがGmailっぽかったのでこれを使ってみることに。 1日500通までっていうほかには特に制限はないみたいです。 SSLで接続しなきゃいけないので、そこがちょっと工夫が必要だったけど、便利コードを書いてくれた人がいたので楽にできました やり方はもう完全にここに書いてある通りなんだけど、消えたりすると困るので転載させていただきます。(shareって言ってるからいいよね?) 【1】メーラーモデルを作るruby script¥generate mailer モデル名 【2】環境設定config/environments/development.rb(またはenvironment.rb)に以下のコードを追加 ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = { :address => "smtp.gmail.com", :port => 587, :domain => "localhost.localdomain", # ドメイン設定してなければコメントアウトしてOK :authentication => :login, :user_name => "*************@gmail.com", :password => "user_nameで設定したgmailのログインパスワード" } 【3】action_mailer_tlsのファイルを作るvendor¥pluginの下に"action_mailer_tls"というフォルダを作成。 その下にinit.rbというファイルを新しく作成。 以下のコードをinit.rbに書く。 require_dependency 'smtp_tls' vendor¥plugin¥action_mailer_tlsの下に"lib"というフォルダを作成。 その下にsmtp_tls.rbというファイルを新しく作成。 以下のコードをsmtp_tls.rbに書く。 require "openssl" require "net/smtp" Net::SMTP.class_eval do private def do_start(helodomain, user, secret, authtype) raise IOError, 'SMTP session already started' if @started #check_auth_args user, secret, authtype if user or secret sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) } @socket = Net::InternetMessageIO.new(sock) @socket.read_timeout = 60 #@read_timeout @socket.debug_output = STDERR #@debug_output check_response(critical { recv_response() }) do_helo(helodomain) raise 'openssl library not installed' unless defined?(OpenSSL) starttls ssl = OpenSSL::SSL::SSLSocket.new(sock) ssl.sync_close = true ssl.connect @socket = Net::InternetMessageIO.new(ssl) @socket.read_timeout = 60 #@read_timeout @socket.debug_output = STDERR #@debug_output do_helo(helodomain) authenticate user, secret, authtype if user @started = true ensure unless @started # authentication failed, cancel connection. @socket.close if not @started and @socket and not @socket.closed? @socket = nil end end def do_helo(helodomain) begin if @esmtp ehlo helodomain else helo helodomain end rescue Net::ProtocolError if @esmtp @esmtp = false @error_occured = false retry end raise end end def starttls getok('STARTTLS') end def quit begin getok('QUIT') rescue EOFError end end end 【4】メールの中身設定【1】で作ったメールモデルにメールのヘッダとボディを設定 たとえばこんな感じ。 class Mailform < ActionMailer::Base def mailform(mail) recipients "受取メールアドレス" subject mail[:subject] from mail[:from] body :name => mail[:name], :message => mail[:message] end end コントローラーとかからメールを送る。 クラス名.create_メソッド名(引数) でメール作成。クラス名.deliver.メソッド名(引数) でメール送信。内容によってはメール送信のほうだけでOK。 作成してから送信する例。 @mail = Mailform.create_mailform(params[:mail]) Mailform.deliver(@mail) これでメール送信ができます。 ローカルのdevelopmentからでも送れて便利 |
Rails3レシピブック 190の技
ついにRails3対応版が出ました!!
WEB+DB PRESS Vol.58
Rails2系から3への移行時に知りたいことがひとまとまりになっててよかった!色々ググるよりこれを読む方が早い。
Rubyレシピブック 第3版 303の技
Rubyやるならこのリファレンスは必要。
Search this site
|