在Rails中配置多个邮件服务器发邮件

Friday, August 27, 2010

在项目中不同的mailer用不同的邮件地址发送邮件


  1. 新建一个配置文件(#{RAILS_ROOT}/config/action_mailer.yml)
  2. noreply:
      domain: lxneng.com
      user_name: noreply@lxneng.com
      password: noreply
      address: smtp.gmail.com
      port: 587
      authentication: :plain
    bug:
      domain: lxneng.com
      user_name: service@lxneng.com
      password: service
      address: smtp.gmail.com
      port: 587
      authentication: :plain
    
  3. 然后在Mailer中加载设置
  4. class UserMailer < ActionMailer::Base
      #-----------------loding smtp setting------------------------
      def load_settings
        options = YAML.load_file("#{RAILS_ROOT}/config/action_mailer.yml")["noreply"]
        @@smtp_settings = {
          :address => options["address"],
          :port => options["port"],
          :domain => options["domain"],
          :authentication => options["authentication"],
          :user_name => options["user_name"],
          :password => options["password"]
        }
      end
      #-----------------deliver method-----------------------------
      def welcome_email(recipient, sent_at = Time.now)
        load_settings
        subject  'Thank you for visiting'
        recipients  recipient
        from  'lxneng <noreply@lxneng.com>'
        sent_on  sent_at
      end
    end
    

This entry was tagged Rails

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top