Sendgrid /无法收到电子邮件

时间:2013-12-18 03:07:06

标签: ruby-on-rails ruby-on-rails-3 heroku actionmailer sendgrid

现在我正在尝试设置联系表单并删除所有错误。但我无法收到电子邮件。你能给我一些建议吗?

☆heroku日志

 2013-12-18T06:20:34.807594+00:00 app[web.1]: Started POST "/contacts" for 118.237.94.47 at 2013-12-18 06:20:34 +0000
2013-12-18T06:20:34.813037+00:00 app[web.1]: Processing by ContactsController#create as HTML
2013-12-18T06:20:34.813037+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"2e9zz3cX0tZwYTZhaPbRRYQufff31fZk4OjjF7sdeo=", "contact"=>{"name"=>"じゃじゃじゃ", "email"=>"jaja@gmail.com", "content"=>"じゃじゃじゃ"}, "commit"=>"Create Contact"}
2013-12-18T06:20:34.843217+00:00 app[web.1]:   Rendered contact_mailer/sent.text.erb (0.5ms)
2013-12-18T06:20:35.265031+00:00 heroku[router]: at=info method=POST path=/contacts host=www.tsundoku-buster.jp fwd="118.237.94.47" dyno=web.1 connect=4ms service=464ms status=302 bytes=116
2013-12-18T06:20:35.259745+00:00 app[web.1]: 
2013-12-18T06:20:35.259745+00:00 app[web.1]: Sent mail to myemail@gmail.com (412ms)
2013-12-18T06:20:35.260551+00:00 app[web.1]: Redirected to http://www.tsundoku-buster.jp/static_pages/contact
2013-12-18T06:20:35.260715+00:00 app[web.1]: Completed 302 Found in 448ms (ActiveRecord: 11.4ms) 

到目前为止,我所做的工作如下:

①app/邮寄者/ contact_mailer.rb

class ContactMailer < ActionMailer::Base
  default from: "myemail@gmail.com"

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.contact_mailer.sent.subject
  #
  def sent(contact)
    @contact = contact

    mail(:to => "myemail@gmail.com", :subject => 'TsundokuBuster発お問い合わせ')
  end
end

②app/视图/ contact_mailer / sent.text.erb

以下の内容でお問い合わせ頂きました。
お名前
<%= @contact.name %>

メールアドレス
<%= @contact.email %>

お問い合わせ内容
<%= @contact.content %>

③contact.rb

class Contact < ActiveRecord::Base
  attr_accessible :name, :email, :content
  validates :name, :email, :content , :presence => true
end

比照http://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html

④contacts_controller.rb

 #encoding: utf-8 
class ContactsController < ApplicationController
      def create
       @contact = Contact.new(params[:contact])
       if @contact.save
        ContactMailer.sent(@contact).deliver
        redirect_to static_pages_contact_path, :notice => 'お問い合わせありがとうございました。'
      else
        render static_pages_contact_path, :alert => 'お問い合わせに不備があります。'
      end
     end
end

⑤在heroku仪表板上添加Sendgrid插件

⑥搜索我的SENDGRID_USERNAME和SENDGRID_PASSWORD

比照https://devcenter.heroku.com/articles/sendgrid

⑦编辑config / environment.rb

ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => ENV['myappusername@heroku.com'],
  :password       => ENV['mypassword'],
  :domain         => 'www.mydomain.jp',
  :enable_starttls_auto => true
}
ActionMailer::Base.delivery_method = :smtp

⑧git添加。 git commit git push heroku master

1 个答案:

答案 0 :(得分:1)

适合所有初学者。

在命令中执行“heroku config”。 然后你会得到这样的东西。

SENDGRID_PASSWORD:          xxxxxxxxx
SENDGRID_USERNAME:          xxxxxxx@heroku.com

这是哈希。因此,如果您想隐藏密码和用户名,您应该编写像

这样的代码
:password => ENV['SENDGRID_PASSWORD'] 
:user_name => ENV['SENDGRID_USERNAME']

如果您不介意显示密码,可以按如下方式编写;

:password => xxxxxxxxx
:user_name => xxxxxxx@heroku.com