Rails 3:无法使用自定义发送电子邮件:from =>地址

时间:2011-12-04 19:15:52

标签: ruby-on-rails ruby-on-rails-3 smtp gmail actionmailer

我正在尝试发送电子邮件,但电子邮件永远不会使用我的:from =>代码,而是电子邮件始终使用我的gmail smtp设置的“发件人”来到。例如,电子邮件到达并说FROM:email@gmail.com而不是FROM:support@mydomain.com,并在日志中显示使用FROM:contact@mydomain.com发送的电子邮件,该电子邮件来自我的设计初始化程序。

我该如何解决这个问题?它真的令人心烦意乱:(感谢您的帮助。

我正在使用Gmail并设置我的smtp设置:

配置/初始化/ mailer_setup

ActionMailer::Base.smtp_settings = {
     :address              => "smtp.gmail.com",
     :port                 => 587,
     :domain               => "mydomain.com",
     :user_name            => "email@gmail.com",
     :password             => "mypassword",
     :authentication       => "plain",
     :enable_starttls_auto => true
     }

·邮寄包装/ user_mailer.rb

   def reset_password_instructions(user)
     @user = user
     @url  = "http://localhost:3000"
     mail(:to => user.email,
          :from => "support@mydomain.com",
          :subject => "recover your password"
          )
   end
end

日志

Started GET "/users/password/new" for 127.0.0.1 at 2011-12-04 12:03:22 -0700
  Processing by Devise::PasswordsController#new as HTML
Rendered devise/shared/_links.erb (1.7ms)
Rendered devise/passwords/new.html.erb within layouts/application (2103.3ms)
Completed 200 OK in 2122ms (Views: 2112.1ms | ActiveRecord: 0.8ms)


Started POST "/users/password" for 127.0.0.1 at 2011-12-04 12:03:27 -0700
  Processing by Devise::PasswordsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"hEYh1/G9P0noE+VobBbgremT+rpt+fJFez7H99dOGNM=", "user"=>{"email"=>"barry269@gmail.com"}, "commit"=>"Send me reset password instructions"}
  User Load (1.7ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'barry269@gmail.com' LIMIT 1
Rendered devise/mailer/reset_password_instructions.html.erb (1.0ms)

Sent mail to barry269@gmail.com (3585ms)
Date: Sun, 04 Dec 2011 12:03:28 -0700
From: contact@mydomain.com
Reply-To: contact@mydomain.com
To: barry269@gmail.com
Message-ID: <4edbc4008ace9_5ab8153b1c470979@ihal-MacBook-Pro.local.mail>
Subject: Reset password instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<p>Hello barry269@gmail.com!</p>

<p>Someone has requested a link to change your password, and you can do this through the link below.</p>

<p><a href="http://localhost:3000/users/password/edit?reset_password_token=Gg3Aqe6sFD3rDLUAda47">Change my password</a></p>

<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
Redirected to http://localhost:3000/users/sign_in
Completed 302 Found in 4281ms


Started GET "/users/sign_in" for 127.0.0.1 at 2011-12-04 12:03:32 -0700
  Processing by Devise::SessionsController#new as HTML
Rendered devise/shared/_links.erb (1.7ms)
Rendered devise/sessions/new.html.erb within layouts/application (80.7ms)
Completed 200 OK in 340ms (Views: 90.6ms | ActiveRecord: 2.3ms)

1 个答案:

答案 0 :(得分:1)

Gmail正在改变你的。如果您不想修改,请获取您自己的邮件服务器。

在使用邮件界面和/或Google服务器时,如何设置不同的发件人有guide

  

对于IMAP / POP用户的注意事项:如果您通过POP或IMAP电子邮件客户端(例如Outlook)访问Gmail并希望使用自定义“发件人”地址发送邮件,则有两种选择。我们建议您为电子邮件客户端配置两个外发SMTP服务器,一个用于Gmail,另一个用于其他地址。或者,您可以使用具有不同“来自”地址的Gmail的出站服务器。如果您已经配置了备用地址,则无论您选择哪种自定义设置,您的邮件都将从以下位置发送:otheraddress @ domain.com,sender:username@gmail.com。

您必须先为gmail启用第三方地址。查看上面链接的指南。 IIRC gmail将设置不同的地址,但是用户仍会看到它来自gmail。