使用rails 3.2.5中的action mailer发送电子邮件

时间:2023-01-16 10:10:07

I know that this is a simple question and asked by many more many times and i also asked this because all the previous one i have checked is based on rails 3.0.0 version and i am using the latest one. I have a user registration form which contain name and email fields. I want to do that when a user click on the submit button an email should be sent to the specified email address by the user i am using rails 3.2.5 version and gem 'mail' version 2.4.4 development log file shows that mail is sent to the email address but its not find in the inbox. I also know that during the development mode action mailer doesn't sent a mail to any address but i want to do this during the development phase. My code is :

我知道这是一个简单的问题,被问过很多次了,我也问过这个问题,因为我之前检查过的所有问题都是基于rails 3.0.0版本的,我正在使用最新的版本。我有一个包含姓名和电子邮件字段的用户注册表。我想做的,当用户单击submit按钮的电子邮件应该发送到用户指定的电子邮件地址我用rails 3.2.5版本和宝石的邮件版本2.4.4开发日志文件显示邮件发送到电子邮件地址,但是找不到的收件箱。我还知道,在开发模式下,邮件发送器不会向任何地址发送邮件,但我想在开发阶段进行此操作。我的代码是:

/config/initializers/setup_mail.rb

/ config /初始化/ setup_mail.rb

ActionMailer::Base.smtp_settings = {
:address              => "smtp.gmail.com",
:port                 => 587,
:domain               => "asciicasts.com",
:user_name            => "asciicasts",
:password             => "secret",
:authentication       => "plain",
:enable_starttls_auto => true
}

/app/mailers/user_mailer.rb

/ app /邮件/ user_mailer.rb

class UserMailer < ActionMailer::Base
default :from => "eifion@asciicasts.com"

def registration_confirmation(user)
mail(:to => user.email, :subject => "Registered")
end
end

/app/controllers/users_controller.rb

/ app / controllers / users_controller.rb

def create
@user = User.new(params[:user])

respond_to do |format|
if @user.save
UserMailer.registration_confirmation(@user).deliver
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
format.xml  { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

so anyone please help me.

所以请大家帮帮我。

1 个解决方案

#1


2  

I'd run into something like this recently as well.

我最近也遇到过类似的事情。

From config/environments/development.rb:

从配置/环境/ development.rb:

config.action_mailer.perform_deliveries = true # This entry made me think the emails should be sent, they were prior to an upgrade by a fellow developer.

config.action_mailer。perform_delivery = true #这个条目让我觉得应该发送电子邮件,它们是在开发人员升级之前发出的。

I also found these emails were not being sent. Digging around, I found config/email.yml It looks like this:

我还发现这些邮件没有发送。我到处找,找到了配置/电子邮件。它是这样的:

development:
  :delivery_method: test
  :settings:
    :address: <email_server_address>
    :port: 25

When I changed :delivery_method: test to :delivery_method: sendmail (which is what we use at my company), it began working.

当我更改:delivery_method: test to:delivery_method: sendmail(这是我们在公司中使用的)时,它开始工作了。

To summarize, I found that the settings in config/email.yml are evaluated last and will trump whatever's in your individual environment file.

总结一下,我发现配置/邮件中的设置。yml是最后评估的,它将胜过您个人环境文件中的任何内容。

#1


2  

I'd run into something like this recently as well.

我最近也遇到过类似的事情。

From config/environments/development.rb:

从配置/环境/ development.rb:

config.action_mailer.perform_deliveries = true # This entry made me think the emails should be sent, they were prior to an upgrade by a fellow developer.

config.action_mailer。perform_delivery = true #这个条目让我觉得应该发送电子邮件,它们是在开发人员升级之前发出的。

I also found these emails were not being sent. Digging around, I found config/email.yml It looks like this:

我还发现这些邮件没有发送。我到处找,找到了配置/电子邮件。它是这样的:

development:
  :delivery_method: test
  :settings:
    :address: <email_server_address>
    :port: 25

When I changed :delivery_method: test to :delivery_method: sendmail (which is what we use at my company), it began working.

当我更改:delivery_method: test to:delivery_method: sendmail(这是我们在公司中使用的)时,它开始工作了。

To summarize, I found that the settings in config/email.yml are evaluated last and will trump whatever's in your individual environment file.

总结一下,我发现配置/邮件中的设置。yml是最后评估的,它将胜过您个人环境文件中的任何内容。