4

我在注册时设置 ActionMailer 时遇到困难:

config/initializers/setup_mail.rb

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

ActionMailer::Base.default_url_options[:host] = "localhost:3000"
Mail.register_interceptor( DevelopmentMailInterceptor ) if Rails.env.development?

users_controller.rb

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

  respond_to do |format|
    if @user.save
      UserMailer.welcome_email( @user ).deliver

      format.html{ redirect_to( @user, :notice => 'Account successfully created.' ) }
      format.json{ render :xml => @user, :status => :created, :location => @user }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
    end
end

app/mailers/user_mailer.rb

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

  def welcome_email( user )
    @user = user
    @url = "http://localhost:3000/login"
    mail( :to => user.email, :subject => 'Welcome to app' )
  end
end

lib/development_mail_interceptor.rb

class DevelopmentMailInterceptor
  def self.delivering_email(message)
    message.subject = "#{message.to} #{message.subject}"
    message.to = "mydump@email.com"
  end
end

我没有收到任何电子邮件发送到我的转储电子邮件中DevelopmentMailInterceptor

4

1 回答 1

1

gmail是肛门!无论如何,您可以使用不同的 smtp 主机吗?我认为,由于过于激进的垃圾邮件检测,他们会默默地拒绝他们认为是“潜在垃圾邮件”的 smpt 请求。在您的情况下,潜在的垃圾邮件是指来自“mail.gmail.com”以外的地址的电子邮件,在您的情况下是“email.com”。

我认为使用 gmail 作为 Rails 应用程序的 SMTP 主机是一个失败的提议,我将 godaddy.com (smtpout.secureserver.net) 用于我的 Rails 应用程序,并且没有问题。

于 2012-05-03T03:33:49.273 回答