【问题标题】:Rails mails "Sent" but never recievedRails 邮件“已发送”但从未收到
【发布时间】:2018-01-31 20:19:19
【问题描述】:

您好,我正在尝试从我的 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)

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

【问题讨论】:

  • 您使用的是什么“发件人:”地址?我没有看到这里分配了一个,如果你忽略了很多提供商会直接拒绝你的电子邮件。
  • 它发送了电子邮件 - 您是在本地主机上还是在连接了 Sendgrid 之类的生产环境中?
  • 如果您在开发环境中,使用 MailCatcher 之类的工具可能会更好。
  • @tadman 我应该指定哪个 From: 地址?
  • @gwalshington 日志来自本地主机环境,但我已经尝试过开发和生产环境

标签: ruby-on-rails ruby email actionmailer


【解决方案1】:

这是因为您可能没有正确配置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。您现在应该可以看到所有通过您的应用发送和接收的电子邮件,网址为:http://127.0.0.1:1080

谢谢!

【讨论】:

  • Ruby 1.9 引入了一种新的哈希样式,它更简洁,更像 JavaScript:{ address: "localhost", port: 1025 }。这样可以避免在各处使用“哈希火箭”=&gt;
  • 非常感谢您解决了这个问题!
【解决方案2】:

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

或者您可以使用letter_opener,也可以使用mailcatcher 之类的其他名称。我成功使用了 letter_opener。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-15
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2014-07-20
    • 1970-01-01
    相关资源
    最近更新 更多