1

Ruby コードを介して電子メールを送信する際に問題があります。私の完全なコードは GitHub で見ることができます。

更新: 以下のコードは、@Gaurish からの提案を反映するように修正されました。

更新 2: 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 を提案する可能性が高いことはわかっていますが、私はそれらを使用したくないか、提案してもらいたいと思っています。代わりに、上記のコードを修正するのに役立つソリューションが欲しいです。

4

3 に答える 3

4

【アップデート1】

Gmailが機能しない場合は、SendGridアドオンを使用できます。SendGridアドオンを使用すると、1日あたり最大200通のメールを無料で提供できます。

これは、メールgemでSTMP APIを使用する方法のサンプル(ドキュメントから取得)です。

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の「from」アドレスに制限はありません。


Herokoでは、Herokuが送信メールサービスを提供していないため、localhostから直接メールを送信することはできません。

したがって、電子メールを送信するための外部SMTPサーバーを検討する必要があります。人気のあるものはGmailSendgridです

これは、herokuのようなクラウドコンピューティングプラットフォームを使用することのトレードオフの1つにすぎません。

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
于 2012-06-11T18:41:01.367 に答える
0

SendGridをアドオンとしてロードするだけで、箱から出してすぐに機能します。さらに、バウンスや配信などのトラブルシューティングを行うための一連の分析を利用できます。

于 2012-06-11T19:54:03.473 に答える
0

試してみると:

@email = Email.new('mark.mcdx@gmail.com', params[:email], 'test subject', params[:message])
@email.send_email
于 2012-06-11T18:27:39.973 に答える