Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 50497d1336
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 57 lines (48 sloc) 1.321 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
require 'mail'

# Configures smtp settings to send email.
def init_mail(params)
  username = params['username']
  password = params['password']
  domain = params['domain']
  host = params['host']
  port = params['port']

  puts "Preparing mail configuration"
  mail_conf = {:address => host,
               :port => port,
               :domain => domain, #custom domain
               :user_name => username,
               :password => password,
               :authentication => 'plain',
               :enable_starttls_auto => true} #gmail require this option
  Mail.defaults do
    delivery_method :smtp, mail_conf
  end
  puts "Mail service configured"
end

def send_mail(to, from, subject, content)
  puts "Preparing email from: #{from}, to: #{to}, subject: #{subject}, body: #{content}"
  msg = Mail.new do
    to to
    from from
    subject subject
    html_part do |m|
      content_type 'text/html'
      body content
    end
  end
  puts "Mail ready, delivering"
  details = msg.deliver
  puts "Mail delivered!"
  details
end

# Sample worker that sends an email.
puts "Worker started"

init_mail(params['email'])

to = params['to']
unless to.is_a?(Array)
  to = [to]
end

to.each do |email|
  message_details = send_mail(email, params['from'], params['subject'], params['body'])
end

puts "Worker finished"
Something went wrong with that request. Please try again.