简体   繁体   English

使用gmail在rails中发送SMTP

[英]Sending smtp in rails using gmail

I am trying to send an activation email in development mode, i am using application.yml to enter my smtp credentials not sure what I am missing but for some reason i'm not able to send the activation email. 我正在尝试以开发模式发送激活电子邮件,我正在使用application.yml输入我的smtp凭据,不确定我丢失了什么,但由于某种原因我无法发送激活电子邮件。 here is the 这里是

application.yml 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

development.rb development.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 token_mailer.rb

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

Use this in development.rb 在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
}

setup mailer to send emails 设置邮件发送者发送电子邮件

in app/mailers/user_mailer.rb 在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

setup view file to send as email 设置视图文件以电子邮件形式发送

app/views/user_emailer/welcome.html.erb

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

setup email configurations 设置电子邮件配置

in config/initializers/email_setup.rb 在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

setup keys/passwords,using gmail..for development but use mailchimp/mandrill for pro 设置密钥/密码,使用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

user mailer to send email 用户邮件发送者发送电子邮件

UserMailer.welcome(current_user).deliver

HOPE IT HELPS.... :) 希望能帮助到你.... :)

try this in your development.rb file 在您的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'
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM