Rails邮件"已发送"但从未收到过

时间:2018-01-31 20:19:19

标签: ruby-on-rails ruby email actionmailer

您好我试图从我的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_email html

    <!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", "$2a$10$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)

正如您所看到的那样,电子邮件已发送给&#39;但是我从来没有在我的收件箱中收到它,我已经尝试了@gmail和@hotmail帐户。 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

这是因为您可能没有正确配置AcionMailer。 在文件&#34; /config/environments/production.rb"或&#34; /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。 在你的&#34; /config/environments/development.rb"文件,添加以下行:

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

重启所有服务。在控制台中,移动到项目目录并键入mailcatcher。您现在应该可以通过您的应用在网址上查看所有已发送和已接收的电子邮件:http://127.0.0.1:1080

谢谢!

答案 1 :(得分:0)

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

或者您可以使用letter_opener,可能还有其他人使用mailcatcher。我成功地使用了letter_opener。