# 获取SMTP配置推荐方法

**URL:** https://meta.discourse.org/t/recommended-way-to-get-smtp-configuration/90433
**Category:** Development
**Created:** [2018年六月21日 23:34 UTC](https://meta.discourse.org/t/recommended-way-to-get-smtp-configuration/90433 "2018-06-21T23:34:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2018年六月21日 23:34 UTC](https://meta.discourse.org/t/recommended-way-to-get-smtp-configuration/90433/1 "2018-06-21T23:34:39Z")

</div>

I’m working on [Discourse Doctor](https://github.com/jjaffeux/discourse-doctor).

As part of that, I’m adding a rake task to `lib/tasks/emails.rake` to test connectivity to the mail server. The way that @j.jaffeux did it in a stand-alone ruby script was

```ruby
def check_env_var(var)
  if ENV[var].nil? || ENV[var].empty?
    error("#{var} is blank, edit containers/app.yml variables")
  end
end
check_env_var("DISCOURSE_SMTP_ADDRESS")
check_env_var("DISCOURSE_SMTP_PORT")
check_env_var("DISCOURSE_SMTP_USER_NAME")
check_env_var("DISCOURSE_SMTP_PASSWORD")

Net::SMTP.start(ENV["DISCOURSE_SMTP_ADDRESS"], ENV["DISCOURSE_SMTP_PORT"])
             .auth_login(ENV["DISCOURSE_SMTP_USER_NAME"], ENV["DISCOURSE_SMTP_PASSWORD"])

```

That’s working, but is there a more Railsy/straightforward way to get those SMTP values from Rails rather than ENV?

I’m also not sure that the rake task needs to mention `app.yml`, as I think that now `discourse-setup` does a pretty good job of checking that up front, so I think that I can just remove the `check_env_var` part.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [2018年六月21日 23:49 UTC](https://meta.discourse.org/t/recommended-way-to-get-smtp-configuration/90433/2 "2018-06-21T23:49:51Z")

</div>

Nice to see this getting some 💘, the doctor 👩‍⚕️ is in!

---

<div class="post-metadata">

### Author: ![brahn](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/brahn/32/109267_2.png) [@brahn](https://meta.discourse.org/u/brahn)
#### Post date: [2018年六月22日 02:12 UTC](https://meta.discourse.org/t/recommended-way-to-get-smtp-configuration/90433/3 "2018-06-22T02:12:42Z")

</div>

> [@pfaffman](#):
>
> That’s working, but is there a more Railsy/straightforward way to get those SMTP values from Rails rather than ENV?

Is this the right place?

`Discourse::Application.config.action_mailer.smtp_settings`

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2018年六月26日 20:59 UTC](https://meta.discourse.org/t/recommended-way-to-get-smtp-configuration/90433/4 "2018-06-26T20:59:11Z")

</div>

> [@brahn](#):
>
> Is this the right place?

Looks like it! It’s obvious when you know. 🙂

Many thanks!
