Question

Now I am trying to set up a contact form and removed all the errors.But I can't get email.Could you give me some advice?

☆heroku logs

 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) 

What I have done so far is as follows.

①app/mailers/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/views/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

cf.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

⑤Adding Sendgrid add-on at heroku dashboard

⑥Search for my SENDGRID_USERNAME and SENDGRID_PASSWORD

cf.https://devcenter.heroku.com/articles/sendgrid

⑦Editing 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 add. git commit git push heroku master

Was it helpful?

Solution

For all beginners.

Just do "heroku config" on your command. Then you will get something like this.

SENDGRID_PASSWORD:          xxxxxxxxx
SENDGRID_USERNAME:          xxxxxxx@heroku.com

This is hash. So if you want to hide your password as well as username, you should write the code like

:password => ENV['SENDGRID_PASSWORD'] 
:user_name => ENV['SENDGRID_USERNAME']

If you don't mind to show your password, you can write this as follows;

:password => xxxxxxxxx
:user_name => xxxxxxx@heroku.com
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top