Create a gist now

Instantly share code, notes, and snippets.

Embed
my little mailer
message.to = 'Person Name <person@example.com>'
message.subject = 'Sample Subject'
# body is probably good enough, but you can set text_part/html_part below if you like
message.body = <<HERE.strip
Hey-
This is a sample
Thanks
HERE
# message.text_part = <<HERE.strip
# HERE
# message.html_part = <<HERE.strip
# HERE
smtp_settings.update(
# domain: 'myappdomain.com',
address: 'smtp.gmail.com',
port: 587,
user_name: 'handle@gmail.com',
password: '',
authentication: 'plain',
enable_starttls_auto: true
)
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'mail'
end
require 'mail'
# Usage:
# ruby deliver.rb 2014-09-30-sample.rb # preview
# ruby deliver.rb 2014-09-30-sample.rb deliver
## Configure
smtp_settings = {}
eval File.read('./config.rb')
Mail.defaults do
delivery_method :smtp, smtp_settings
end
message_file = ARGV.shift
deliver = ARGV.shift
## Build Message
require 'ostruct'
message = OpenStruct.new
eval File.read(message_file)
mail = Mail.new do
from 'First Last <handle@gmail.com>'
to message.to
subject message.subject
body message.body
delivery_method :smtp, smtp_settings
end
mail.html_part = Mail::Part.new do
content_type 'text/html; charset=UTF-8'
body message.html_part
end if message.html_part
mail.text_part = Mail::Part.new do
body message.text_part
end if message.text_part
## Preview or Deliver
puts mail.to_s
puts
if deliver == 'deliver'
puts 'delivering'
puts
puts output = mail.deliver!
puts
File.open(message_file, 'a') do |f|
f.write(
"\n" <<
output.to_s.
split(/\r?\n/).
map {|l| "# #{l}" }.
join("\n")
)
end
else
puts mail.message_id
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment