代码之家  ›  专栏  ›  技术社区  ›  IvanHid

Rails邮件“已发送”,但从未收到

  •  0
  • IvanHid  · 技术社区  · 7 年前

    您好,我正在尝试从我的rails应用程序发送一封电子邮件,我尝试了多个类似于此问题的解决方案,但仍然不起作用,这是我的用户邮箱:

    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\u电子邮件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帐户。 我怎样才能解决这个问题?

    2 回复  |  直到 7 年前
        1
  •  3
  •   Mrunal Selokar    7 年前

    这是因为您可能没有配置 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 . 在“/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

    谢谢

        2
  •  0
  •   razvans    7 年前

    如果您正在开发中,您需要设置action mailer以实际发送电子邮件。你可以用你的gmail来做到这一点。请参见此处: Action mailer SMTP google apps

    或者您可以使用 letter_opener ,可能还有其他人喜欢 mailcatcher . 我成功地使用了letter\u opener。