【问题标题】:Sending smtp in rails using gmail使用 gmail 在 Rails 中发送 smtp
【发布时间】:2015-08-19 02:26:14
【问题描述】:

我正在尝试在开发模式下发送激活电子邮件,我正在使用 application.yml 输入我的 smtp 凭据,不确定我缺少什么,但由于某种原因我无法发送激活电子邮件。这是

application.yml

SMTP_PORT: 587
  SMTP_DOMAIN: localhost:3000
  SMTP_ADDRESS: smtp.gmail.com
  SMTP_USERNAME: myemail@gmail.com
  SMTP_PASSWORD: myemailpassword
  SMTP_AUTHENTICATION: 'plain'
  enable_starttls_auto: true

开发.rb

  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.delivery_method = :file
  config.action_mailer.file_settings = { location: 'tmp/mails' }

  config.action_mailer.default_url_options = { :host => ENV["URL_HOST"] }

token_mailer.rb

  def activation(email, token)
    @token_url = edit_activation_url token
    mail to: email
  end

【问题讨论】:

标签: ruby-on-rails ruby email actionmailer


【解决方案1】:

在 development.rb 中使用它

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
     :address => "smtp.gmail.com",
     :port => 587,
     :user_name => "your mail",
     :password => "your password",
     :authentication => :plain,
     :enable_starttls_auto => true
}

【讨论】:

  • 谢谢你的朋友,即使我遇到了同样的情况,这对我来说很有效。
  • 现在在2017年你必须激活关于不安全应用的选项myaccount.google.com/lesssecureapps打开这个选项就可以了
  • :plain 是什么意思?
【解决方案2】:

设置邮件程序以发送电子邮件

在 app/mailers/user_mailer.rb 中

class UserMailer < ActionMailer::Mailer
  default from: "no-reply@myapp.com"

def welcome(user_email)
   @user=User.find_by_email user_email
   Rails.logger.info "==========sending welcome email to ==> #{@user.email}"
   mail(:to => @user.email, :subject => "Hi #{@user.username},Welcome to #{configatron.app_name}")
end
 end

设置视图文件以电子邮件形式发送

app/views/user_emailer/welcome.html.erb

<p>Hi <%= @user.username %>,Welcome to myapp.com</p>

设置电子邮件配置

在 config/initializers/email_setup.rb 中

if Rails.env != 'test'
  email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
  ActionMailer::Base.smtp_settings = email_settings[Rails.env] unless email_settings[Rails.env].nil?
end

设置密钥/密码,使用 gmail.. 进行开发,但使用 mailchimp/mandrill 进行专业版

in config/email.yml

    development:
      :address: smtp.gmail.com
      :port: 587
      :authentication: plain
      :user_name: milind@gmail.com
      :password: password
      :enable_starttls_auto: true
    production:
      :address: smtp.gmail.com
      :port: 587
      :authentication: plain
      :user_name: milind@gmail.com
      :password: password
      :enable_starttls_auto: true

用户邮件发送电子邮件

UserMailer.welcome(current_user).deliver

希望对您有所帮助.... :)

【讨论】:

    【解决方案3】:

    在你的 development.rb 文件中试试这个

      config.action_mailer.default_url_options = { :host => 'localhost:3000' }
      config.action_mailer.delivery_method = :smtp
      config.action_mailer.smtp_settings = {
          :address => "smtp.gmail.com",
          :port => 587,
          :authentication => :plain,
          :domain => 'yourdomain.com',
          :user_name => 'your_gmail_email',
          :password => 'your_password'
      }
    

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 2015-09-28
      • 2014-12-24
      相关资源
      最近更新 更多