使用 gmail 在 rails 中发送 smtp
Sending smtp in rails using gmail
我正在尝试在开发模式下发送激活电子邮件,我正在使用 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
在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
}
在您的 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'
}
设置邮件程序发送电子邮件
在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
设置 keys/passwords,使用 gmail.. 用于开发,但使用 mailchimp/mandrill 用于 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
用于发送电子邮件的用户邮件程序
UserMailer.welcome(current_user).deliver
希望对您有所帮助.... :)
我正在尝试在开发模式下发送激活电子邮件,我正在使用 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
在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
}
在您的 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'
}
设置邮件程序发送电子邮件
在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
设置 keys/passwords,使用 gmail.. 用于开发,但使用 mailchimp/mandrill 用于 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
用于发送电子邮件的用户邮件程序
UserMailer.welcome(current_user).deliver
希望对您有所帮助.... :)