【问题标题】:Send email via Ruby code on Heroku在 Heroku 上通过 Ruby 代码发送电子邮件
【发布时间】:2012-06-14 02:19:36
【问题描述】:

我在通过我的 Ruby 代码发送电子邮件时遇到问题。你可以看到我的full code on GitHub

更新:以下代码已被修改以反映@Gaurish 的建议

UPDATE2:看起来 gmail 拒绝了登录尝试 - 我收到了一封来自他们的电子邮件,警告我一些未知的应用程序试图访问我的帐户,但他们禁用了它

具体的类在这里:

require 'net/smtp'

=begin
    http://apidock.com/ruby/v1_9_3_125/Net/SMTP/start/class
    https://devcenter.heroku.com/articles/config-vars

    I added the following config vars to Heroku
    heroku config:add GM_USR=xxxx
    heroku config:add GM_PSW=xxxx
=end

class Email
    def initialize (to, from, subject, body)
            @to = to
            @from = from
            @subject = subject
            @body = body
            @message = <<MESSAGE_CONTENT
                From: User <#{@from}>
                To: Integralist <#{@to}>
                MIME-Version: 1.0
                Content-type: text/html
                Subject: #{@subject}
                #{@body}
MESSAGE_CONTENT

            @smtp = Net::SMTP.new('smtp.gmail.com', 587)
        end

        def send_email
            @smtp.enable_starttls
            @smtp.start('furious-wind-9309.herokuapp.com', ENV['GM_USR'], ENV['GM_PSW'], :login) do |smtp|
            @smtp.send_message(@message, @from, @to)
        end
    end
end

我这样称呼它:

email = Email.new('myemail@gmail.com', params[:email], 'test subject', params[:message]);
email.send_mail

但是当我执行代码时,屏幕上显示错误:535-5.7.1 Please log in with your web browser and then try again. Learn more at

我检查了日志,我得到了......

2012-06-13T08:01:08+00:00 heroku[router]: POST furious-wind-9309.herokuapp.com/contact dyno=web.1 queue=0 wait=0ms service=628ms status=500 bytes=2060
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/logger.rb:15:in `call'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `block in call'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1416:in `synchronize'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `call'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:80:in `block in pre_process'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:78:in `catch'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/thin-1.3.1/lib/thin/connection.rb:78:in `pre_process'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:1060:in `call'
2012-06-13T08:01:08+00:00 app[web.1]:   /app/vendor/bundle/ruby/1.9.1/gems/eventmachine-0.12.10/lib/eventmachine.rb:1060:in `block in spawn_threadpool'
2012-06-13T08:01:08+00:00 app[web.1]: 82.69.39.185 - - [13/Jun/2012 08:01:08] "POST /contact HTTP/1.1" 500 2060 0.6254

我知道人们可能会建议使用 ActionMailer 或 Pony,但我宁愿不使用它们,或者请让那些建议给我。相反,我想要一个有助于修复上述代码的解决方案。

【问题讨论】:

    标签: ruby email heroku sinatra


    【解决方案1】:

    [更新 1]

    如果 gmail 不适合您,您可以使用 SendGrid Addon,每天最多可免费发送 200 封电子邮件。

    这是一个示例(取自docs),介绍如何将他们的STMP API 与mail gem 一起使用

    require 'mail'
    Mail.defaults do
      delivery_method :smtp, { :address   => "smtp.sendgrid.net",
                               :port      => 587,
                               :domain    => "yourdomain.com",
                               :user_name => "yourusername@domain.com",
                               :password  => "yourPassword",
                               :authentication => 'plain',
                               :enable_starttls_auto => true }
    end
    
    mail = Mail.deliver do
      to 'yourRecipient@domain.com'
      from 'Your Name <name@domain.com>'
      subject 'This is the subject of your email'
      text_part do
        body 'Hello world in text'
      end
      html_part do
        content_type 'text/html; charset=UTF-8'
        body '<b>Hello world in HTML</b>'
      end
    end
    

    使用 Sendgrid 是更好的解决方案,因为您还可以访问 gmail 不提供的高级报告和分析。此外,sendgrid 中的“发件人”地址也没有限制。


    使用 Heroko,您不能直接从localhost 发送电子邮件,因为Heroku does not provide an outgoing mail service

    因此您必须考虑使用外部 smtp 服务器来发送您的电子邮件。流行的是Gmail & Sendgrid

    这只是使用像 heroku 这样的云计算平台的一种权衡。

    使用 Gmail,尝试执行以下操作:

    require 'net/smtp'
    
        msg = "your message goes here"
        smtp = Net::SMTP.new 'smtp.gmail.com', 587
        smtp.enable_starttls
        smtp.start(YourDomain, YourAccountName, YourPassword, :login) do
          smtp.send_message(msg, FromAddress, ToAddress)
        end
    

    【讨论】:

    • 谢谢,我明天试试。但是,如果我还没有设置,YourDomain 参数会是什么?例如,我正在通过 Heroku 提供的 URL(目前)访问该站点,所以我会完整指定:appname.heroku.com 吗?
    • 更多请咨询API docs
    • 恐怕这行不通。我更新了我的问题以反映我正在使用的新代码。也许您可以就我可能出错的地方提出建议。谢谢
    • 您可以使用 sendgrid,因为我更新了关于如何完成的答案
    【解决方案2】:

    如果你尝试:

    @email = Email.new('mark.mcdx@gmail.com', params[:email], 'test subject', params[:message])
    @email.send_email
    

    【讨论】:

      【解决方案3】:

      只需将 SendGrid 加载为附加组件,它就可以开箱即用。此外,您还可以获得一整套分析工具,用于解决退回、交付等问题...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-29
        • 2018-02-15
        • 2013-02-24
        • 2018-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多