Skip to content

Instantly share code, notes, and snippets.

@WallasFaria
Last active October 12, 2022 13:29
Show Gist options
  • Save WallasFaria/65c46a0694caf0ab7584117edfcf357c to your computer and use it in GitHub Desktop.
Save WallasFaria/65c46a0694caf0ab7584117edfcf357c to your computer and use it in GitHub Desktop.
Send email by action mailer
send_email.rb
mailer/
|- daily_email.html.erb
|- daily_email.text.erb
<%# path: ./mailer/daily_email.html.erb %>
<h1>Hello <%= @name %>!</h1>
<p>
Welcome to our system.
</p>
<%# path: ./mailer/daily_email.text.erb %>
Hello <%= @name %>!
Welcome to our system.
require 'bundler/inline'
# dependencies
gemfile do
source 'https://rubygems.org'
gem 'actionmailer', '4.2.9'
# it is needed in ruby 3.x
gem 'net-smtp', require: false
gem 'net-imap', require: false
gem 'net-pop', require: false
end
require 'action_mailer'
# config
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.view_paths = File.dirname(__FILE__)
ActionMailer::Base.smtp_settings = {
address: ENV.fetch('SMTP_HOST'),
port: ENV.fetch('SMTP_PORT'),
user_name: ENV.fetch('SMTP_EMAIL'),
password: ENV.fetch('SMTP_PASSWORD'),
domain: 'yoursite.com',
authentication: 'login'
}
# code
class Mailer < ActionMailer::Base
def daily_email
@name = 'Wallas Faria'
mail(from: '[email protected]', to: '[email protected]', subject: 'testing mail') do |format|
format.text
format.html
end
end
end
# main
email = Mailer.daily_email
puts email
email.deliver_now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment