ruby - 使用 Pony Mail 和 Sinatra 时连接被拒绝

标签 ruby email heroku sinatra pony

我在让 Pony Mail gem 与我的 Sinatra 网站(部署在 Heroku 上)一起工作时遇到了很多麻烦。我的目标是有一个基本的联系表格,访问者可以在其中填写电子邮件和主题,他们的电子邮件和电话号码,此表格会将信息发送到客户的电子邮件。

表单代码如下:

<form action="/emailform" method="post">
<p>We'd love to hear from you!</p>
<input type="text" placeholder="Your Full Name" name="full_name">
<input type="text" placeholder="Your E-mail        *required" name="email">
<input type="text" placeholder="Your Phone Number" name="phonenumber">
<input type="text" placeholder="Subject" name="subject">
<textarea rows="5" cols="30" name="comments" placeholder="Comments"></textarea>

<input type="submit" value="Submit" placeholder="Send">
</form>

这是我的 actions.rb 文件(POST 请求)的代码(3 个点代表更多参数/表单字段)

post '/emailform' do
Pony.mail to: 'xxx@gmail.com',
        from: 'xxx@gmail.com',
        subject: 'e-mail from: ' + params[:full_name] + params[:email],
        body: "subject: " + params[:subject] + "phone-number: " + params[:phonenumber]...
redirect '/'

end

post '/homeevaluation' do
  Pony.mail to: 'xxx@gmail.com',
        from: 'xxx@gmail.com',
        subject: 'Home Evaluation E-mail from: ' + params[:full_name],
        body: 'phone number: ' + params[:phonenumber] + params[:comments]...
redirect '/'
end

我现在得到的错误是

Errno::ECONNREFUSED - Connection refused - connect(2):

我通过查看 Heroku 日志看到了这一点。

我已经为这个问题苦恼了一段时间,在网上找不到任何有用的信息。如果有人使用过 gem 或知道另一种方法来完成相同的任务,我很乐意听到它!

最佳答案

这看起来像是设置问题。您是否在使用某种 Heroku 插件来发送电子邮件?我们使用 Sendgrid,所以我们在代码中的某处需要这些行:

Pony.options[:via_options][:user_name] = ENV['SENDGRID_PASSWORD']
Pony.options[:via_options][:password]  = ENV['SENDGRID_USERNAME']

编辑:更详细的选项

Pony.options = {
  via: :smtp,
  via_options: {
    address:              "smtp.sendgrid.net",
    port:                 "587",
    domain:               "heroku.com",
    authentication:       :plain,
    enable_starttls_auto: true,
    user_name:            "foo@heroku.com",
    password:             "foo"
  }
}

关于ruby - 使用 Pony Mail 和 Sinatra 时连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26265358/

相关文章:

java - 从 Scala(和 Java)访问 DRb 对象(例如 Ruby 队列)的最佳方式是什么?

ruby-on-rails - 使用本地 :path before deploy 检查 gem

php - 用 PHPMailer 发送 FPDF 文档;

java - 通过MAPI获取电子邮件的UID

mysql - Rails 应用程序部署到 Heroku 应用程序错误

ruby-on-rails - rails 服务器命令的问题

objective-c - 在应用程序邮件中预先添加文本 Objective-c

heroku - Grunt Shell + Heroku Push = 无标准输出

node.js - 处理或防止错误导致我在 Heroku 上的整个 node.js 应用程序崩溃

ruby - 类方法 : describe "#my_class_method" or describe "#self.my_class_method"?