难以配置SMTP服务器发送电子邮件

时间:2019-07-23 16:14:50

标签: ruby-on-rails email

我想从我自己的服务器(而不是gmail)发送电子邮件,并且在配置环境文件时遇到了困难。

使用gmail凭据时,我可以从开发计算机发送电子邮件

  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp

  host = 'mysite.com'
  config.action_mailer.default_url_options = { host: host, protocol: 'http' }
  Rails.application.routes.default_url_options[:host] = host

  config.action_mailer.perform_caching = false

  config.action_mailer.smtp_settings = { 
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'example.com',
    user_name:            ENV["GMAIL_USERNAME"],
    password:             ENV["GMAIL_PASSWORD"],
    authentication:       'plain',
    enable_starttls_auto: true }

这很好。但是,如果我尝试通过服务器更改设置,则无法连接。

  config.action_mailer.smtp_settings = { 
    address:              'mail.mysite.com',
    port:                 465,
    user_name:            ENV["MYSITE_USERNAME"],
    password:             ENV["MYSITE_PASSWORD"],
    authentication:       'plain',
    enable_starttls_auto: true }


The configuration for the emails on my server are:

-------------------------
Secure SSL/TLS Settings (Recommended)
Username: test@mysite.com
Password: Use the email account’s password.
Incoming Server: mail.mysite.com
IMAP Port: 993 POP3 Port: 995
Outgoing Server: mail.mysite.com
SMTP Port: 465
IMAP, POP3, and SMTP require authentication.
-------------------------

我可以使用上述设置通过Thunderbird连接到发球区,但无法与Rails连接。 我使用了正确的用户名和密码。 我不知道我在做什么错。

2 个答案:

答案 0 :(得分:0)

smtp_settings地址必须来自电子邮件客户端,例如Sendgrid或其他。您当前正在指定自己的域(mysite.com)。他们应该提供必要的凭据,以便您能够发送电子邮件。

答案 1 :(得分:0)

您需要一项服务来发送您的电子邮件,我建议您使用sendgrid,它非常易于配置,每月最多有2000封电子邮件是免费的。并且不会在凭据文件中设置的环境变量中设置用户名和密码。

ActionMailer::Base.smtp_settings = {
  :user_name => 'your_sendgrid_username',
  :password => 'your_sendgrid_password',
  :domain => 'yourdomain.com',
  :address => 'smtp.sendgrid.net',
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}
相关问题