Next @Felipe I have a linux server with 4 rails applications running on it.
And also a ruby application that fires a few emails a day, this ruby application is small and uses ActionMailer for the sending emails and has a crontab job running a few times a day.
I'll put an example of this application here very simplified.
It stays in the /var/www/mensageria
directory inside it I have the following files:
- mailer_config.rb (smtp mailer settings, user password etc)
- notification_mailer.rb (Class mailer)
- send.rb (File that triggers email)
And it has a folder called notification_mailer
and inside it a alert.html.erb
file.
In the file mailer_config.rb
it 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 file notification_mailer.rb
it 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 file send.rb
it has the following code:
require './notification_mailer.rb'
NotificationMailer.alert('[email protected]','Teste de Email').deliver
The alert.html.erb
file places the content you want to upload.
Now 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 this it will run send.rb
every day at 06:00.