使用gmail在rails中发送smtp

时间:2022-04-27 20:29:21

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. here is the

我正在尝试在开发模式下发送激活电子邮件,我使用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

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

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

3 个解决方案

#1


6  

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
}

#2


2  

setup mailer to send emails

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

in 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

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 for pro

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.... :)

希望能帮助到你.... :)

#3


0  

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'
  }

#1


6  

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
}

#2


2  

setup mailer to send emails

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

in 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

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 for pro

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.... :)

希望能帮助到你.... :)

#3


0  

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'
  }