-
Create a new rails projects
rails new mailer --skip-active-record
-
In development.rb, change the settings from
FROM: config.action_mailer.raise_delivery_errors = false TO: config.action_mailer.raise_delivery_errors = true
-
Create a config file under config/smtp.yml.
development: gmail_username: <gmail username> gmail_password: <gmail password>
-
Load the file in environment.rb, before the Rails.application.initialize line:
SMTP_CONFIG = YAML.load_file("#{Rails.root}/config/smtp.yml")[::Rails.env] # Initialize the Rails application. Rails.application.initialize!
-
In development.rb, add the following SMTP settings:
# SMTP settings for gmail config.action_mailer.smtp_settings = { :address => "smtp.gmail.com", :port => 587, :user_name => SMTP_CONFIG['gmail_username'],, :password => SMTP_CONFIG['gmail_password'], :authentication => "plain", :enable_starttls_auto => true, :openssl_verify_mode => 'none' } config.action_mailer.default_url_options = {host: "localhost:3000"}
-
Generate a mailer
rails g mailer SampleMailer create_message
-
Under the create_message message of the SampleMailer, write the following:
@greeting = "Hi" mail to: "[email protected]", subject: "New comment has been generated"
-
You can check the preview by access the following link:
http://localhost:3000/rails/mailers/sample_mailer
-
Now you can test the mailer by using rails console.
rails console
-
Create a new message
COMMAND: new_mail = SampleMailer.create_message OUTPUT: Rendered sample_mailer/create_message.html.erb within layouts/mailer (0.1ms) Rendered sample_mailer/create_message.text.erb within layouts/mailer (0.1ms) SampleMailer#create_message: processed outbound mail in 36.8ms => #<Mail::Message:70195998331660, Multipart: true, Headers: <From: [email protected]>, <To: [email protected]>, <Subject: New comment has been generated>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_578b56286558c_43e33fd7c485e1f899d3"; charset=UTF-8>>
-
Send the created message.
COMMAND: new_mail.deliver_now OUTPUT: Sent mail to [email protected] (81.4ms) Date: Sun, 17 Jul 2016 19:59:29 +1000 From: [email protected] To: [email protected] Message-ID: <[email protected]> Subject: New comment has been generated Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="--==_mimepart_578b56286558c_43e33fd7c485e1f899d3"; charset=UTF-8 Content-Transfer-Encoding: 7bit ----==_mimepart_578b56286558c_43e33fd7c485e1f899d3 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit SampleMailer#send_message Hi, find me in app/views/sample_mailer/send_message.text.erb ----==_mimepart_578b56286558c_43e33fd7c485e1f899d3 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit <html> <body> <h1>Sample mail</h1> <p> Hi, find me in app/views/sample_mailer/send_message.html.erb </p> </body> </html> ----==_mimepart_578b56286558c_43e33fd7c485e1f899d3-- => #<Mail::Message:70195998331660, Multipart: true, Headers: <Date: Sun, 17 Jul 2016 19:59:29 +1000>, <From: [email protected]>, <To: [email protected]>, <Message-ID: <[email protected]>>, <Subject: New comment has been generated>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_578b56286558c_43e33fd7c485e1f899d3"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>
-
Notifications
You must be signed in to change notification settings - Fork 0
josedelapazjr/mailer-rails
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published