How to send email using rails 4 -
i trying send email , follow tutorial
http://guides.rubyonrails.org/action_mailer_basics.html
and how proceed:
my emailer.rb
class emailer < actionmailer::base default from: "r.karoui@gmail.com" def emprunt_mail(user) @user = user @url = 'http://example.com/login' mail(to: @user.email, subject: 'emprunt de livre') end end
my emrunt_mail.erb.html
<!doctype html> <html> <head> <meta content='text/html; charset=utf-8' http-equiv='content-type' /> </head> <body> <h1>bonjour, <%= current_user.f_name %></h1> <p> vous venez d'emprunter united nations livre </p> <p> vous avez une durée d'une semaine pour le rendre ! </p> <p>bonne journée!</p> </body> </html>
in books_controller.rb
def emprunter if (@book.state == false) if @book.update(state: 1) flash[:notice]='vous aver united nations délai de 7 jours pour rendre le livre' emailer.create_emprunt_mail(current_user) emailer.deliver_emprunt_mail(current_user) redirect_to books_list_path else redirect_to admin_manage_path end else flash[:notice]='ce livre est déja pris' redirect_to books_list_path end end
i add together confguration mt environment/developpment.rb
config.assets.raise_runtime_errors = true config.action_mailer.raise_delivery_errors =true config.action_mailer.default_url_options ={ :host => 'localhost:3000'} config.action_mailer.smtp_settings= { adress: "smtp.gmail.com", port: "587", domain: "gmail.com", authentication: "plain", enable_startlls_auto: true, user_name: 'username', password: '*********' }
and did not add together route strage bihavior in emprunter method pass else of first if , should come in block if .
update
in server have this
" rendered emailer/emprunt_mail.html.erb (0.0ms)
emailer#emprunt_mail: processed outbound mail service in 4.1ms sent mail service rawia.seller@gmail.com (3.3ms) date: tue, 11 nov 2014 11:40:31 +0100 from: karoui.rawia@gmail.com to: rawia.seller@gmail.com message-id: <5461e79fd9674_4b33ffe529d82a44d0@imac-de-imac.home.mail> subject: emprunt de livre mime-version: 1.0 content-type: text/html; charset=utf-8 content-transfer-encoding: quoted-printable <!doctype html> <html> <head> <meta content=3d'text/html; charset=3dutf-8' http-equiv=3d'content-type= ' /> </head> <body> <h1>bonjour, </h1> <p> vous venez d'emprunter united nations livre </p> <p> vous avez une dur=c3=a9e d'une semaine pour le rendre ! </p> <p>bonne journ=c3=a9e!</p> </body> </html>= redirected http://localhost:3000/books/show completed 302 found in 14ms (activerecord: 1.2ms)
but when check mail service did not find new one!! please tell me problem
this new configuration in environment/developpment.rb:
config.action_mailer.default_url_options = { :host => 'localhost:3000' } # actionmailer config # setup production - deliveries, no errors raised config.action_mailer.delivery_method = :smtp config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = false config.action_mailer.default :charset => "utf-8" config.action_mailer.smtp_settings= { adress: "smtp.gmail.com", port: 587, domain: "gmail.com", authentication: "plain", enable_starttls_auto: true, user_name: 'username', password: '*****'
}
ruby-on-rails-4.1
No comments:
Post a Comment