Ruby-on-rails 从本地主机发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1789032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:59:39  来源:igfitidea点击:

send email from localhost

ruby-on-railsemaillocalhost

提问by conspirisi

I'm try learn about email in rails. I'm developing something on localhost. Is it possible to send an email from localhost to say a normal mail account like gmail? Do I have a install a mail server? I've just got a standard rails installation at the moment for development.

我正在尝试了解 Rails 中的电子邮件。我正在本地主机上开发一些东西。是否可以从本地主机发送电子邮件以说像 gmail 这样的普通邮件帐户?我有安装邮件服务器吗?我现在刚刚安装了标准的导轨用于开发。

回答by Chun Yang

Update for rails 4.0
Now you need these code to make it work:

更新 rails 4.0
现在你需要这些代码来使它工作:

# I recommend using this line to show error
config.action_mailer.raise_delivery_errors = true

ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.gmail.com',
  :domain         => 'mail.google.com',
  :port           => 587,
  :user_name      => '[email protected]',
  :password       => '******',
  :authentication => :plain,
  :enable_starttls_auto => true
}

回答by Mikael S

You can set up ActionMailer to use Gmail's SMTP server using something like this in config/environment.rb:

您可以使用config/environment.rb 中的类似内容将 ActionMailer 设置为使用 Gmail 的 SMTP 服务器:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.server_settings = {
    :address        => 'smtp.gmail.com',
    :domain         => '<your domain>',
    :port           => 587,
    :user_name      => '<your gmail>',
    :password       => '<your password>',
    :authentication => :plain
}

Edit: If you experience any difficulties, set your config to display errors:

编辑:如果您遇到任何困难,请将您的配置设置为显示错误:

ActionMailer::Base.raise_delivery_errors = true

回答by Anand Shah

Have a look at ActionMailer. In RAILS_ROOT/config/environment/, there is a file for different environments (development, test, production) the configurable settings go in these files

看看ActionMailer。在 中RAILS_ROOT/config/environment/,有一个用于不同环境(开发、测试、生产)的文件,这些文件中的可配置设置

You specify the delivery_method like this,

您像这样指定 delivery_method,

ActionMailer::Base.delivery_method = :sendmail

or if you want

或者如果你想要

ActionMailer::Base.delivery_method = :smtp

A detailed example of the settings has been posted by Mikael S

Mikael S 已发布设置的详细示例

HTH

HTH

回答by Juan Tarquino

If I understand your situation correctly, you want to send an email from your local computer using a custom email address such as [email protected]. If you already registered the domain name for your email account ( mycompany.com ) is very likely that the company that is hosting your website, also has a POP/SMTP server. If so, you can use Mikael S's sample and change the address parameter to your Hosting company's smtp address and use your hosting company's username/password.

如果我正确理解您的情况,您希望使用自定义电子邮件地址(例如 [email protected])从本地计算机发送电子邮件。如果您已经为您的电子邮件帐户 ( mycompany.com ) 注册了域名,则托管您网站的公司很可能也有一个 POP/SMTP 服务器。如果是这样,您可以使用 Mikael S 的示例并将地址参数更改为您的托管公司的 smtp 地址并使用您的托管公司的用户名/密码。

If you have not register your custom domain or don't have a hosting provider, you can install a free email server in your local computer. If you use WindowsXP, you can add the IIS email server by going to add/remove programs->windows features. If you are using Linux, you can use any of the email servers available in the repositories. Once you install your local email server you will use Mikael S's sample code and use 127.0.0.1 or localhost in the address field. If you are using WindowsXP's email server, I think you don't have to enter username/password.

如果您尚未注册自定义域或没有托管服务提供商,您可以在本地计算机上安装免费电子邮件服务器。如果您使用 WindowsXP,您可以通过添加/删除程序->windows 功能来添加 IIS 电子邮件服务器。如果您使用的是 Linux,则可以使用存储库中可用的任何电子邮件服务器。安装本地电子邮件服务器后,您将使用 Mikael S 的示例代码并在地址字段中使用 127.0.0.1 或 localhost。如果您使用的是 WindowsXP 的电子邮件服务器,我认为您不必输入用户名/密码。

Hope it helps you.

希望对你有帮助。

回答by Tzury Bar Yochay

You can send it from localhost, you can even set the sender as a 'real' mailbox e.g. [email protected].

您可以从本地主机发送它,您甚至可以将发件人设置为“真实”邮箱,例如 [email protected]

However, some (or say most) servers will not accept this mail as part of their spam blocking strategy(inability to verify the sender identity). However, In the past, I have had something similar with python which worked on gmail.

但是,某些(或说大多数)服务器不会接受此邮件作为其垃圾邮件阻止策略的一部分(无法验证发件人身份)。然而,在过去,我有一些与 python 类似的东西,它在 gmail 上工作。

so good luck ;-)

祝你好运;-)