Created
March 9, 2017 04:12
-
-
Save bf4/5a973e060ad5e98cfbd9c9c947816d41 to your computer and use it in GitHub Desktop.
my little mailer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| message.to = 'Person Name <[email protected]>' | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| smtp_settings.update( | |
| # domain: 'myappdomain.com', | |
| address: 'smtp.gmail.com', | |
| port: 587, | |
| user_name: '[email protected]', | |
| password: '', | |
| authentication: 'plain', | |
| enable_starttls_auto: true | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 <[email protected]>' | |
| 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