Next @Felipe I have a linux server with 4 Rails applications running on it.
And also a ruby application that triggers a few emails a day, this ruby application is small and uses the Actionmailer for sending emails and has a crontab job running a few times a day.
I will put an example of this application here very simplified.
It stays in the directory /var/www/mensageria
inside it I have the following files:
- mailer_config.Rb (smtp Directory settings, password user etc)
- notification_mailer.Rb (Class Mailer)
- send.Rb (File that fires the email)
And there’s a folder called notification_mailer
and within it a file alert.html.erb
.
In the archive mailer_config.rb
has the following code:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "domain.com.br",
authentication: :plain,
user_name: "user",
password: "password",
enable_starttls_auto: true
}
In the archive notification_mailer.rb
has the following code:
require 'rubygems'
require 'action_mailer'
require 'action_view'
require './mailer_config.rb'
class NotificationMailer < ActionMailer::Base
default from: 'Nome do Remetente <[email protected]>'
def alert(to,subject)
mail(to: to, subject: subject) do |format|
format.html { render './notification_mailer/alert.html.erb' }
end
end
end
In the archive send.rb
has the following code:
require './notification_mailer.rb'
NotificationMailer.alert('[email protected]','Teste de Email').deliver
The archive alert.html.erb
you place the content to be sent.
Ai now only the crontab is missing. In a terminal on the server run:
crontab -e
you can edit as if you were editing a file by vim
0 6 * * * /bin/bash -l -c 'cd /var/www/mensageria && ruby send.rb'
With that he will execute the send.rb
every day at 06:00.
I don’t know Rails and its servers, but on Linux servers for PHP you can use Cron Jobs. I found this for Rails, see if it helps you: Whenever
– ricardobarantini
@user157930 Daria just to explain the use of
Whenever
?– Felipe Avelar