ror突进-rails中使用Mailgun

首先,先到mailgun.com注册账户,按照其要求填写primary key,和dns互相设置通

如果通知信内容极少非常简单的话,可以不用附到rails中去,只需要官网的ruby客户端即可
https://github.com/mailgun/mailgun-ruby

也可以使用smtp的方式来发

先找到mailgun里的smtp设置

首先把密码等写在yml文件中

production:
   AWS_ACCESS_KEY_ID: ******
   AWS_SECRET_ACCESS_KEY: ******

   # mailgun私钥

   MAILGUN_SMTP_PORT: '587'
   MAILGUN_SMTP_SERVER: smtp.mailgun.org
   MAILGUN_SMTP_LOGIN: postmaster@whenmgone.com
   MAILGUN_SMTP_PASSWORD: *******
   DOMAIN_NAME: 'http://law-firm.herokuapp.com'

development:

   MAILGUN_SMTP_PORT: 587
   MAILGUN_SMTP_SERVER: smtp.mailgun.org
   MAILGUN_SMTP_LOGIN: postmaster@whenmgone.com
   MAILGUN_SMTP_PASSWORD: *******
   DOMAIN_NAME: 'http://localhost:3000'

调整环境设置config/environments/下文件,填写设置引用application.yml中的密码等设置(注意develop和production都要做)

  # 邮件测试

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :port           => ENV['MAILGUN_SMTP_PORT'],
    :address        => ENV['MAILGUN_SMTP_SERVER'],
    :user_name      => ENV['MAILGUN_SMTP_LOGIN'],
    :password       => ENV['MAILGUN_SMTP_PASSWORD'],
    :domain         => 'whenmgone.com', #eg: 'yourappname.herokuapp.com'

    :authentication => :plain,
  }  # 

建立一个新的mailer叫model_mailer

rails g mailer model_mailer

然后在生成的mailer中,写函数 (内部无法调用ENV)

  # 邮件系统-注册密码

  def send_password_mail(user,password)
    reciver_obj = User.find(user)
    @password = password
    mail to: reciver_obj.email, subject: "Lawyer法律咨询平台-注册"
  end
  #

在需要的地方调用发信函数即可,比如welcome_controller.rb

ModelMailer.send_password_mail(user.id,generated_password).deliver