Rails 封邮件 "Sent" 但从未收到

Rails mails "Sent" but never recieved

你好,我正在尝试从我的 rails 应用程序发送一封电子邮件,我已经尝试了多种解决方案来解决与这个问题类似的问题,但它仍然不起作用,这是我的 UserMailer:

class UserMailer < ActionMailer::Base
    def welcome_email(user)
        @user = user
        @url = 'http://fundacionccb2017.herokuapp.com'
        mail(to: 'hidalgoivanh@hotmail.com', subject: 'Bienvenido a la aplicación de ccb')
    end
end

这是我的welcome_emailhtml

    <!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to example.com, <%= @user.name %></h1>
    <p>
      You have successfully signed up to example.com,
      your username is: Pancho.<br>
    </p>
    <p>
      To login to the site, just follow this link:
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

下面是我在我的控制器中(在我的用户控制器中)调用它的方式:

def create
        @user = User.new(user_params)
        if @user.save
                        UserMailer.welcome_email(@user).deliver_later
            session[:user_id] = @user.id
            redirect_to user_path(@user)
        else
            render 'new'
        end
    end

这是我在控制台中的消息:

Started POST "/users" for 127.0.0.1 at 2018-01-31 14:10:31 -0600
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"COkRNuVqcaqn2Wrso24y7lmWyKt1OX0/gai2FlXfeLBg9R61RmJj81JDsXdiuBZgBxKi4nW6WErl99bDMVp0kA==", "user"=>{"name"=>"Usuario", "last_name"=>"Apellido1", "second_last_name"=>"Apellido2", "mail"=>"ejemplo7@ejemplo.com", "username"=>"Usuario6", "telephone_number"=>"555555555555", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
Unpermitted parameter: password_confirmation
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."mail") = LOWER(?) LIMIT ?  [["mail", "ejemplo7@ejemplo.com"], ["LIMIT", 1]]
  User Exists (0.1ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."username") = LOWER(?) LIMIT ?  [["username", "Usuario6"], ["LIMIT", 1]]
  SQL (1.0ms)  INSERT INTO "users" ("name", "mail", "telephone_number", "last_name", "second_last_name", "username", "password_digest", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["name", "Usuario"], ["mail", "ejemplo7@ejemplo.com"], ["telephone_number", "555555555555"], ["last_name", "Apellido1"], ["second_last_name", "Apellido2"], ["username", "Usuario6"], ["password_digest", "a$jdHJTDiCxL9NPg6ajMye9eZ251RVwfVBAUb2YZEKNvc05lYpqGnFa"], ["created_at", 2018-01-31 20:10:31 UTC], ["updated_at", 2018-01-31 20:10:31 UTC]]
   (4.5ms)  commit transaction
[ActiveJob] Enqueued ActionMailer::DeliveryJob (Job ID: 922ae0c1-b5ed-4339-b25b-40752f651b9e) to Async(mailers) with arguments: "UserMailer", "welcome_email", "deliver_now", #<GlobalID:0x007f8f94ebd9a0 @uri=#<URI::GID gid://fundacion-ccb/User/7>>
Redirected to http://localhost:3000/users/7
Completed 302 Found in 567ms (ActiveRecord: 6.8ms)


  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 7], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e] Performing ActionMailer::DeliveryJob from Async(mailers) with arguments: "UserMailer", "welcome_email", "deliver_now", #<GlobalID:0x007f8f94e04a90 @uri=#<URI::GID gid://fundacion-ccb/User/7>>
Started GET "/users/7" for 127.0.0.1 at 2018-01-31 14:10:32 -0600
Processing by UsersController#show as HTML
  Parameters: {"id"=>"7"}
  User Load (1.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 7], ["LIMIT", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 7], ["LIMIT", 1]]
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e]   Rendering user_mailer/welcome_email.html.erb
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e]   Rendered user_mailer/welcome_email.html.erb (0.9ms)
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e]   Rendering user_mailer/welcome_email.text.erb
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e]   Rendered user_mailer/welcome_email.text.erb (0.3ms)
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e] UserMailer#welcome_email: processed outbound mail in 356.4ms
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e] Sent mail to hidalgoivanh@hotmail.com (3.6ms)
[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e] Date: Wed, 31 Jan 2018 14:10:32 -0600
To: hidalgoivanh@hotmail.com
Message-ID: <5a7222b866548_c343fc7ca75eb68909a7@Ivan-VM.mail>
Subject: =?UTF-8?Q?Bienvenido_a_la_aplicaci=C3=B3n_de_ccb?=
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5a7222b8645e5_c343fc7ca75eb689082b";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_5a7222b8645e5_c343fc7ca75eb689082b
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: base64

QmllbnZlbmlkbyBhIGxhIGZ1bmRhY2nDs24hCj09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PQpFc3RlIG1lbnNhamUgaGEgc2lkbyBlbnRyZWdhZG8g
ZXhpc3Rvc2FtZW50ZS4K

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

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to example.com, Usuario</h1>
    <p>
      You have successfully signed up to example.com,
      your username is: Pancho.<br>
    </p>
    <p>
      To login to the site, just follow this link:
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

----==_mimepart_5a7222b8645e5_c343fc7ca75eb689082b--

[ActiveJob] [ActionMailer::DeliveryJob] [922ae0c1-b5ed-4339-b25b-40752f651b9e] Performed ActionMailer::DeliveryJob from Async(mailers) in 373.06ms
  Rendering users/show.html.erb within layouts/application
   (0.1ms)  SELECT COUNT(*) FROM "projects" WHERE "projects"."user_id" = ?  [["user_id", 7]]
  Rendered users/show.html.erb within layouts/application (476.8ms)
  Rendered layouts/_navigation.html.erb (1.7ms)
Completed 200 OK in 1027ms (Views: 632.3ms | ActiveRecord: 2.2ms)

如您所见,电子邮件是 'sent',但我从未在收件箱中收到它,我已尝试使用@gmail 和@hotmail 帐户。 我该如何解决这个问题?

如果您正在开发中,则需要设置操作邮件程序以实际发送电子邮件。您可以使用您的 gmail 来做到这一点。看这里:Action mailer SMTP google apps

或者您可以使用 letter_opener, and probably others like mailcatcher。我用letter_opener成功了

这是因为您可能没有正确配置 AcionMailer。 根据您所在的环境,在文件“/config/environments/production.rb”或“/config/environments/development.rb”中,添加以下行:

config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
 :address              => "smtp.gmail.com",
 :port                 => 587,
 :user_name            => 'your_gmail_username',
 :password             => 'your_gmail_password',
 :authentication       => "plain",
 :enable_starttls_auto => true
}

您还可以使用名为 mailcatcher 的 gem。 在您的“/config/environments/development.rb”文件中,添加以下行:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }

重新启动所有服务。在您的控制台中,移至您的项目目录并键入 mailcatcher。您现在应该能够通过您的应用在 url 查看所有发送和接收的电子邮件:http://127.0.0.1:1080

谢谢!