Skip to content

Instantly share code, notes, and snippets.

@NicolasHov
Created March 7, 2018 10:23
Show Gist options
  • Save NicolasHov/f2aa459e1b991ad725ba256f49f9ce1a to your computer and use it in GitHub Desktop.
Save NicolasHov/f2aa459e1b991ad725ba256f49f9ce1a to your computer and use it in GitHub Desktop.
TODO
* arriver à envoyer au moins un mail
* config pour prod et push heroku (voir tutos ci dessous)
* tester letter-opener
* Sur une branche du projet final :
* (ajout crédential dans figaro/dotenv) sur le site direct
* envoi mail contact / bourse
##### Gemfile
gem devise
gem mailjet
gem pg
gem figaro...
letter-opener...
##### Devise
`rails generate devise:install`
"At this point, a number of instructions will appear in the console. Among these instructions, you'll need to
set up the default URL options for the Devise mailer in each environment. Here is a possible configuration for
config/environments/development.rb:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }" <<< developpment /!\ In production,
:host should be set to the actual host of your application.
config.action_mailer.raise_delivery_errors = true >>> pour afficher les erreurs
`rails g devise:views`
(uncomment email stuff in migration)
`rails generate devise User`
(rails generate migration DeviseCreateUser)
unconfirm ## Confirmable dans la migration
ajouter dans le model user.rb
`rails db:migrate`
(restart server)
Tester l'envoi de mail de Devise
dans config de devise, dans initializer>devise.rb : attnetion à renseigner `config.mailer_sender = 'adresse mail ici'`
(tuto pour custom un mailer devise https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer)
## avec mailjet
rails generate mailjet:initializer
dans l'initializer :
# renseigner les clefs... (a protéger dans dotenv/figaro..)
Mailjet.configure do |config|
config.api_key = ''
config.secret_key = ''
config.default_from = ''
# application.rb
`config.action_mailer.delivery_method = :mailjet_api` (marche aussi avec `mailjet`)
###### Action Mailer
devise a donc un mailer intégré (pour les confirm de connexions, les changements sur le profil ou oubli mdp, etc) mais on doit utiliser application_mailer pour les autres mails
# si on veut générer un mailer
`rails generate mailer UserMailer`
on ajoute la méthode welcome_email(user) dans welcome_email.html.erb...bizarre
on fait la view du mail welcome_email.text.erb
on call le mailer dans le controller avec par ex `UserMailer.welcome_email(@user).deliver_later` >>> le deliver_later fonctionne bien en dev et pas en prod (cf deploy 1), sinon on met deliver_now...
`rails generate mailjet:initializer` // ajouter credentials dans initializers/mailjet.rb
les credentials smtp peuvent être indiqués dans config/dévelopment/environement...
######### Deploy
http://www.bogotobogo.com/RubyOnRails/RubyOnRails_Devise_Authentication_Sending_Confirmation_Email_Heroku_Deploy.php
https://www.youtube.com/watch?v=YnGuALpJN1M 15:09
penser à faire les bonnes config pour config/environments/production.rb
1. (action mailer : dev vs prod)
Job's default behavior is to execute jobs via the :async adapter. So, you can use deliver_later now to send
emails asynchronously. Active Job's default adapter runs jobs with an in-process thread pool. It's well-suited for
the development/test environments, since it doesn't require any external infrastructure, but it's a poor fit for
production since it drops pending jobs on restart.
>>>> If you need a persistent backend, you will need to use an Active Job
adapter that has a persistent backend (Sidekiq, Resque, etc).
##### Sending Email To Multiple Recipients
It is possible to send email to one or more recipients in one email (e.g., informing all admins of a new signup) by setting the list of emails to the `:to` key. The list of emails can be `an array of email addresses` or a single string with the addresses separated by commas.
class AdminMailer < ApplicationMailer
default to: Proc.new { Admin.pluck(:email) },
from: 'notification@example.com'
def new_registration(user)
@user = user
mail(subject: "New User Signup: #{@user.email}")
end
end
###### divers
`rails generate simple_form:install --bootstrap`
Be sure to have a copy of the Bootstrap stylesheet available on your
application, you can get it on http://getbootstrap.com/.
Inside your views, use the 'simple_form_for' with one of the Bootstrap form
classes, '.form-horizontal' or '.form-inline', as the following:
= simple_form_for(@user, html: { class: 'form-horizontal' }) do |form|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment