Generate a Scheduled Newsletter

suggest change

Create the Newsletter model:

model Newsletter name:string email:stringnewsletter.rb

Create the Newsletter controller:

controller Newsletters create

After that, change the create.html.erb view to the nex name. We will convert this file to and partial view which will be stored inside the Footer. The name will be _form.html.erb.

Change name file from: | To: |

|:———————————————––|:—————————————————| | app/views/newsletters/create.html.erb | app/views/newsletters/_form.html.erb |

After that set the routes:

routes.rb

Later on, we need to set the form we will use to save each mail:

_form.html.erb

And after that, insert on the footer:

_footer.html.erb

Now, install the -letter_opener- to can preview email in the default browser instead of sending it. This means you do not need to set up email delivery in your development environment, and you no longer need to worry about accidentally sending a test email to someone else’s address.

First add the gem to your development environment and run the bundle command to install it.

Gemfile

Then set the delivery method in the Development Environment:

development.rb

Now, create an Mailer Structure to manage the whole mailers which we will work. In terminal

UserMailer newsletter_mailer

And inside the UserMailer, we have to create a method called Newsletter Mailer which will be created to contain inside on the lastest blog post and will be fired with a rake action. We will assume that you had a blog structure created before.

user_mailer.rb'your_gmail_account@gmail.com'def newsletter_mailer
```
@newsletter = Newsletter.all
@post = Post.last(3)
emails = @newsletter.collect(&:email).join(", ")
mail(to: emails, subject: "Hi, this is a test mail.")
```
  end

After that, create the Mailer Template:

newsletter_mailer.html.erb

Since we want to send the email as a separate process, let’s create a Rake task to fire off the email. Add a new file called email_tasks.rake to lib/tasks directory of your Rails application:

email_tasks.rake

The send_digest_email: :environment means to load the Rails environment before running the task, so you can access the application classes (like UserMailer) within the task.

Now, running the command rake -T will list the newly created Rake task. Test everything works by running the task and checking whether the email is sent or not.

To test if the mailer method works, run the rake command:

weekly_newsletter_email

At this point, we have a working rake task which can be scheduled using crontab. So we will install the Whenever Gem which is used to provide a clear syntax for writing and deploying cron jobs.

gem 'whenever', require: false

After that, run the next command to create an initial config/schedule.rb file for you (as long as the config folder is already present in your project).

wheneverize .

  [add] writing `./config/schedule.rb'
  [done] wheneverized!

Now, inside the schedule file, we have to create our CRON JOB and call the mailer method inside determining the CRON JOB to operate some tasks without assistance and in a selected range of time. You can use different types of syntax as is explained on this link.

schedule.rb1.day, :at => '4:30 am'rake 'weekly_newsletter_email'

Cron Job Basic Syntax

Cron Jobs with Whenever

Now to test the Cron Job was succesfully created we can use the next command to read since terminal, our scheduled job in CRON SYNTAX:

whenever30 4 * * *rake weekly_newsletter_email --silent

Now, to run the test in Development Environment, is wise to set the next line on the application.rb principal file to let the application knows where are the models it will use.

application.rb

Now to let Capistrano V3 save the new Cron Job inside the server and the trigger which will fired up the execution of this task, we have to add the next requirement:

'whenever/capistrano'

And insert into the deploy file the identifier which CRON JOB will use about the environment and the name of the application.

deploy.rb

And ready, after save changes on each file, run the capistrano deploy command:

cap production deploy

And now your JOB was created and calendarize to run the Mailer Method which is what i want and in the range of time we set on this files.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


ActionMailer:
*Generate a Scheduled Newsletter

Table Of Contents
13ActionMailer
55CSV