Barebones prototype for sending MJML/ERB emails using Sidekiq via a simple Sinatra API.
- Redis must be installed and running.
- Node.js and MJML must be installed.
- Obviously, Ruby must be installed. ;)
- An SMTP server or service available for sending email messages.
There are a few parameters that need to be configured for the prototype to work.
Add and edit a sidekiq.env file with the Redis host, port and db.
export REDIS_HOST=localhost
export REDIS_PORT=6379
export REDIS_DB=YOUR_REDIS_DB
Additional Sidekiq configuration changes can be made to the config/sidekiq.rb file:
# config/sidekiq.rb
Sidekiq.configure_server do |config|
config.redis = { :host => ENV['REDIS_HOST'], :port => ENV['REDIS_PORT'], :db => ENV['REDIS_DB'] }
end
Sidekiq.configure_client do |config|
config.redis = { :host => ENV['REDIS_HOST'], :port => ENV['REDIS_PORT'], :db => ENV['REDIS_DB'] }
endOnce Node.js is installed, you can install MJML:
> npm install -g mjmlEdit the config/mjml.rb file with the MJML binary path, the validation level and any other available parameter.
# config/mjml.rb
MJML.configure do |config|
# Make sure that MJML is installed and that the proper binary path is configured.
# Could not make the following config work:
# config.bin_path = '/usr/bin/env mjml'
config.bin_path = '/usr/local/bin/mjml'
# config.logger = MJML::Logger.setup!(STDOUT)
config.minify_output = true
# Set the validation level to :strict if you want to have MJML v3.0 validation.
config.validation_level = :strict # :skip/:soft/:strict
endThe prototype has been tested using Amazon Simple Email Service (SES). The mail.env and config/mail.rb can be modified/tweaked for other email services.
Add and edit a mail.env file with the SMTP server, port, username, password and sender email:
export MAIL_SMTP_SERVER=your.smtp.server.com
export MAIL_SMTP_PORT=587
export MAIL_SMTP_USERNAME=YOUR_IAM_SMTP_USER_FOR_SES
export MAIL_SMTP_PASSWORD=YOUR_VERY_LONG_PASSWORD
export [email protected]
Additional Mail configuration changes can be made to the config/mail.rb file:
# config/mail.rb
Mail.defaults do
# These settings have been tested with AWS SES.
# Settings may need to be tweaked for Gmail, AWS WorkMail, and other services.
smtp_settings = { :address => ENV['MAIL_SMTP_SERVER'],
:port => ENV['MAIL_SMTP_PORT'],
:user_name => ENV['MAIL_SMTP_USERNAME'],
:password => ENV['MAIL_SMTP_PASSWORD'],
:authentication => 'plain',
:enable_starttls_auto => true }
delivery_method :smtp, smtp_settings
end-
Run
bundle installto get all the gems needed by the prototype. -
Make sure that Redis is running. Redis can be started by running:
> redis-server- Start Sidekiq by running:
> sidekiq -r ./sidekiq_server.rb- Start the Sinatra application by running:
> rackup config.ru-
Assuming that Sinatra is listening on
http://localhost:9292/, make aPOSTrequest to the/sendresource with the following parameters:- rcpt - The email message recipient.
- subj - The email message subject.
- msg - The email message content.
Postman is your friend! You can make a
POSTrequest and send the data via parameters or form-data.