emails: server sends but a user doesn't receive

痴心易碎 提交于 2019-12-12 02:47:32

问题


I'm facing a weird bug.

Trying to set up welcome email after a user's registration.

According to server logs, my code sends an email but a user doesn't receive it.

Here is my code:

models/user.rb

after_create :welcome_message

  private
    def welcome_message
      UserMailer.welcome_email(self).deliver
    end

and mailers/user_mailer.rb

def welcome_email(user)
    @user = user
    mail to: @user.email, subject: t("mailer.welcome_email.welcome")
  end

here are environments. The error occurs in both of them

config/environments/development.rb

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:              'smtp.mandrillapp.com',
  port:                 '587',
  domain:               'localhost:3000',
  user_name:            'welcome@mysite.com',
  password:             'blahblahblah',
  authentication:       'plain',
  enable_starttls_auto: true
}

and config/environments/production.rb

config.action_mailer.default_url_options = { :host => 'mysite.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:              'smtp.mandrillapp.com',
  port:                 '587',
  domain:               'mysite.com',
  user_name:            'welcome@mysite.com',
  password:             'blahblahblah',
  authentication:       'plain',
  enable_starttls_auto: true
}

I'm using Devise to register users. So, also tried to do that via :confirmable. Outcome is the same – test user doesn't get a welcome email.

Gemfile

gem 'rails', '3.2.12'
gem 'devise', '3.0.3'
gem "mail"

and here are server logs

Sent mail to user@hismail.com (1920ms)
Date: Sat, 10 May 2014
From: welcome@mysite.com
Reply-To: welcome@mysite.com
To: user@hismail.com
Message-ID: <aaa@aaa.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit

<p>Welcome user@hismail.com!</p>

Yet eventually there is no welcome email at user@hismail.com

Any ideas?

UPD: on the internet I see that nobody else faces the same problem So, I guess I'm just missing something. But cannot figure out what and where. Please help me.


回答1:


Try this:-

Create a file named as setup_mail.rb in config/initializers/ folder so that it looks like

config/initializers/setup_mail.rb

and put the below code in that file:-

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
 :address => "smtp.mandrillapp.com",
 :domain => "mysite.com",
 :port => 80,
 :user_name => "username",
 :password => "blahblahblah",
 :authentication => :plain
}

ActionMailer::Base.default_url_options[:host] = "mysite.com"

Delete this from config/environments/development.rb and config/environments/production.rb

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
 address:              'smtp.mandrillapp.com',
 port:                 '587',
 domain:               'localhost:3000',
 user_name:            'welcome@mysite.com',
 password:             'blahblahblah',
 authentication:       'plain',
 enable_starttls_auto: true
}


来源:https://stackoverflow.com/questions/23581807/emails-server-sends-but-a-user-doesnt-receive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!