Rails教程ch11 accoun激活。为什么我的激活邮件不能与sendgrid一起发送?

时间:2017-12-03 06:22:55

标签: ruby-on-rails heroku sendgrid railstutorial.org

目前正在关注第11章的michael hartl的rails教程,帐户激活。我正在使用sendgrid在生产中发送帐户激活电子邮件。但我没有收到电子邮件。代码似乎正在工作,并在heroku日志中生成一个电子邮件模板,但我永远不会发送。在说我的sendgrid帐户未经验证之前我遇到了问题,我已经解决了这个问题。 (除了白色标记阶段,不确定这是否会产生差异)。发送网格帐户未激活,但我仍然在heroku日志中遇到各种错误,我需要帮助来理解它们。

heroku日志:从下面的heroku日志中可以看出。 Testuser1使用电子邮件地址testuser1@testemail.com创建。然后激活电子邮件生成但从未到达(这不是一个真正的电子邮件,但我尝试过多个真实的电子邮件,并得到相同的错误,我只是用这个电子邮件发布在这里)..然后有一些恐怖在结束时我不知道该怎么处理。

heroku日志:

D, [2017-12-03T06:00:55.256730 #6] DEBUG -- : [b4f34ac2-105f-474e-aece-91b2ced26276]   SQL (1.1ms)  INSERT INTO "users" ("name", "email", "created_at", "updated_at", "password_digest", "activation_digest") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["name", "testuser1"], ["email", "testuser1@testemail1.com"], ["created_at", "2017-12-03 06:00:55.183243"], ["updated_at", "2017-12-03 06:00:55.183243"], ["password_digest", "$2a$10$SVA7hgBW7lkm63KwUR.gc.W4LFVb.7Zn8oLTg50deBOmJGljcIUv2"], ["activation_digest", "$2a$10$tWF2n2vJI92GDsKX9NfyUuVM/rxghRfPjuJRCvFC/IZ9ooXWymM1W"]]
D, [2017-12-03T06:00:55.258773 #6] DEBUG -- : [b4f34ac2-105f-474e-aece-91b2ced26276]    (1.5ms)  COMMIT
I, [2017-12-03T06:00:55.266930 #6]  INFO -- : [b4f34ac2-105f-474e-aece-91b2ced26276]   Rendering user_mailer/account_activation.html.erb within layouts/mailer
I, [2017-12-03T06:00:55.269541 #6]  INFO -- : [b4f34ac2-105f-474e-aece-91b2ced26276]   Rendered user_mailer/account_activation.html.erb within layouts/mailer (2.4ms)
I, [2017-12-03T06:00:55.271006 #6]  INFO -- : [b4f34ac2-105f-474e-aece-91b2ced26276]   Rendering user_mailer/account_activation.text.erb within layouts/mailer
I, [2017-12-03T06:00:55.271902 #6]  INFO -- : [b4f34ac2-105f-474e-aece-91b2ced26276]   Rendered user_mailer/account_activation.text.erb within layouts/mailer (0.7ms)
2017-12-03T06:00:55.494735+00:00 heroku[router]: at=info method=POST path="/users" host=still-beyond-10331.herokuapp.com request_id=b4f34ac2-105f-474e-aece-91b2ced26276 fwd="5.62.43.97" dyno=web.1 connect=2ms service=475ms status=500 bytes=1891 protocol=https
D, [2017-12-03T06:00:55.425916 #6] DEBUG -- : [b4f34ac2-105f-474e-aece-91b2ced26276] UserMailer#account_activation: processed outbound mail in 165.9ms
I, [2017-12-03T06:00:55.491479 #6]  INFO -- : [b4f34ac2-105f-474e-aece-91b2ced26276] Sent mail to testuser1@testemail1.com (65.4ms)
D, [2017-12-03T06:00:55.491572 #6] DEBUG -- : [b4f34ac2-105f-474e-aece-91b2ced26276] Date: Sun, 03 Dec 2017 06:00:55 +0000
From: noreply@example.com
To: testuser1@testemail1.com
Message-ID: <5a23931768fbc_6333d7d87257c@60d510ea-02dc-4670-96aa-397d82d5d64a.mail>
Subject: Account activation
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5a23931767b1c_6333d7d8724e9";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


Content-Type: text/plain;
----==_mimepart_5a23931767b1c_6333d7d8724e9
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Hi testuser1,

Welcome to the Sample App! Click on the link below to activate your account:

https://still-beyond-10331.herokuapp.com/account_activations/0MwIEqEXZaLzmWJb8KhGOg/edit?email=testuser1%40testemail1.com

----==_mimepart_5a23931767b1c_6333d7d8724e9
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <h1>Sample App</h1>

<p>Hi testuser1,</p>

Welcome to the Sample App! Click on the link below to activate your account:
<p>
</p>

  </body>
</html>

<a href="https://still-beyond-10331.herokuapp.com/account_activations/0MwIEqEXZaLzmWJb8KhGOg/edit?email=testuser1%40testemail1.com">Activate</a>

----==_mimepart_5a23931767b1c_6333d7d8724e9--
I, [2017-12-03T06:00:55.491875 #6]  INFO -- : [b4f34ac2-105f-474e-aece-91b2ced26276] Completed 500 Internal Server Error in 454ms (ActiveRecord: 22.3ms)
F, [2017-12-03T06:00:55.492410 #6] FATAL -- : [b4f34ac2-105f-474e-aece-91b2ced26276]   
):
F, [2017-12-03T06:00:55.492467 #6] FATAL -- : [b4f34ac2-105f-474e-aece-91b2ced26276] Net::SMTPAuthenticationError (535 Authentication failed: account disabled
F, [2017-12-03T06:00:55.492512 #6] FATAL -- : [b4f34ac2-105f-474e-aece-91b2ced26276]   
F, [2017-12-03T06:00:55.492563 #6] FATAL -- : [b4f34ac2-105f-474e-aece-91b2ced26276] app/models/user.rb:64:in `send_activation_email'
[b4f34ac2-105f-474e-aece-91b2ced26276] app/controllers/users_controller.rb:25:in `create'

accountactıvatıonscontroller:

class AccountActivationsController < ApplicationController

  def edit
    user = User.find_by(email: params[:email])
    if user && !user.activated? && user.authenticated?(:activation, params[:id])
      user.activate
      log_in user
      flash[:success] = "Account activated!"
      redirect_to user
    else
      flash[:danger] = "Invalid activation link"
      redirect_to root_url
    end
  end
end

用户控制器:

class UsersController < ApplicationController

    before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
    before_action :correct_user,   only: [:edit, :update]
    before_action :admin_user,     only: :destroy

  def index
    @users = User.where(activated: true).paginate(page: params[:page]) 

  end

  def show
    @user = User.find(params[:id])
    redirect_to root_url and return unless @user.activated?
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email #listing 11.36, replaced in user model by send_activation_email-->UserMailer.account_activation(@user).deliver_now
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
      render 'new'
    end
  end

  def edit
   User.find(params[:id])
  end

  def update
   User.find(params[:id])
    if @user.update_attributes(user_params)
      # Handle a successful update.
      flash[:success] = "Profile updated"
      redirect_to @user

    else
      render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User deleted"
    redirect_to users_url
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

    # Before filters

    # Confirms a logged-in user.
    def logged_in_user
      unless logged_in?
        store_location
        flash[:danger] = "Please log in."
        redirect_to login_url
      end
    end

       # Confirms the correct user.
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

        # Confirms an admin user.
    def admin_user
      redirect_to(root_url) unless current_user.admin?
    end
end

用户模型:

class User < ApplicationRecord

  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true


# Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

    # Returns a random token.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end


    # Returns true if the given token matches the digest.
  def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
  end

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

    # Activates an account.
  def activate
    update_columns(activated: true, activated_at: Time.zone.now)
  end

  # Sends activation email.
  def send_activation_email
    UserMailer.account_activation(self).deliver_now 
  end

    # Sets the password reset attributes.
  def create_reset_digest
    self.reset_token = User.new_token
    update_attribute(:reset_digest,  User.digest(reset_token))
    update_attribute(:reset_sent_at, Time.zone.now)
  end

  # Sends password reset email.
  def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
  end

  private

    # Converts email to all lower-case.
    def downcase_email
      #self.email = email.downcase
      email.downcase!
    end

    # Creates and assigns the activation token and digest.
    def create_activation_digest
      self.activation_token  = User.new_token
      self.activation_digest = User.digest(activation_token)
    end
end

smpt设置:

config.action_mailer.perform_caching = false

  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  host = 'https://still-beyond-10331.herokuapp.com'
  config.action_mailer.default_url_options = { host: host }
  ActionMailer::Base.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => '587',
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :domain         => 'heroku.com',
    :enable_starttls_auto => true
  }

从上面的heroku日志中提取的错误:

2017-12-03T06:00:55.492517+00:00 app[web.1]: F, [2017-12-03T06:00:55.492467 #6] FATAL -- : [b4f34ac2-105f-474e-aece-91b2ced26276] Net::SMTPAuthenticationError (535 Authentication failed: account disabled
2017-12-03T06:00:55.492558+00:00 app[web.1]: F, [2017-12-03T06:00:55.492512 #6] FATAL -- : [b4f34ac2-105f-474e-aece-91b2ced26276]   
2017-12-03T06:00:55.492618+00:00 app[web.1]: F, [2017-12-03T06:00:55.492563 #6] FATAL -- : [b4f34ac2-105f-474e-aece-91b2ced26276] app/models/user.rb:64:in `send_activation_email'
2017-12-03T06:00:55.492619+00:00 app[web.1]: [b4f34ac2-105f-474e-aece-91b2ced26276] app/controllers/users_controller.rb:25:in `create'

这可能是代码问题还是sendgrid设置问题?本教程不会详细介绍如何设置sendgrid。我的发送网格帐户现在处于活动状态,但正如我所提到的,我还没有设置白名单,因为我不知道它是什么。因为我假设没有它,sendgrid将默认为其标准设置。有没有人完成之前的轨道tuto的spart?欢迎任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

电子邮件位于您的heroku服务器日志中

private static ENCX.S57Draw _draw;

 Savedrfpt.Lat = Convert.ToDouble(24.34567);
 Savedrfpt.Lon = Convert.ToDouble(54.32456);
 ENCX.PixelPoint Saved_rfpixpts = _draw.GeoPix.Point(Savedrfpt);

double Distance_meters = Convert.ToDouble(2) * 1000;
double pixelpermeters = getPixelSizeInMeters();
Double d = Distance_meters / pixelpermeters;
gr.DrawEllipse(greenPen, (float)Saved_rfpixpts.X - (float)d, (float)Saved_rfpixpts.Y - (float)d, (float)d + (float)d, (float)d + (float)d);
gr.FillEllipse(brush, (float)Saved_rfpixpts.X - (float)d, (float)Saved_rfpixpts.Y - (float)d, (float)d + (float)d, (float)d + (float)d);


 public double getPixelSizeInMeters()
        {
            double sizem = _draw.PixelSizeMetres;
            double disscale = _draw.GeoPix.DisplayScale;
            return _draw.PixelSizeMetres * _draw.GeoPix.DisplayScale;
        }

要发送电子邮件,您需要配置动作邮件

这是SendGrid official documentation,在how to configure Rails ActionMailer

上也有许多在线讨论