Skip to content

Instantly share code, notes, and snippets.

@yumminhuang
Last active April 23, 2019 21:29
Show Gist options
  • Save yumminhuang/4ebc9760ecc57aa29af1 to your computer and use it in GitHub Desktop.
Save yumminhuang/4ebc9760ecc57aa29af1 to your computer and use it in GitHub Desktop.
Ruby script to send emails using Pony
# Email account configuration
via: smtp
fromname: Alex
authenticate: true
account:
hostname: smtp.example.com
port: 587
username: alex@example.com
password: password
tls: true
#!/usr/bin/env ruby
# encoding: utf-8
require 'pony'
require 'yaml'
class MailSender
def initialize(config, contacts, subject, message, attachment=nil)
@config = YAML.load_file(config)
@recipients = contacts
@subject = subject
# Path to text file
@message = message
# Path to attachment file
@attachment = attachment unless attachment.nil?
end
def handle
Pony.options = pony_options
send_emails unless emails.empty?
end
def pony_options
mail_opts = mail_options
mail_opts.merge!(authed_options) if @config['authenticate']
mail_opts[:subject] = @subject
mail_opts[:body] = File.read(@message)
unless @attachment.nil?
mail_opts[:attachments] =
{File.basename(@attachment) => File.read(@attachment)}
end
return mail_opts
end
def emails
@mails ||= @recipients.each_with_object([]) do |to, res|
tmp = Hash.new
tmp[:to] = to
res << tmp
end
return @mails
end
def send_emails
emails.each do |email|
begin
Timeout.timeout 10 do
Pony.mail(email)
puts "Sent an email to #{email[:to]}"
end
rescue Timeout::Error
puts "Failed to send an emial to #{email[:to]}"
end
end
end
def mail_options
{
:from => "#{@config['fromname']} <#{@config['account']['username']}>",
:via => @config['via'],
:charset => 'UTF-8',
:text_part_charset => 'UTF-8',
:sender => @config['account']['username'],
}
end
def authed_options
{
via_options: {
:address => @config['account']['hostname'],
:port => @config['account']['port'],
:enable_starttls_auto => @config['account']['tls'],
:user_name => @config['account']['username'],
:password => @config['account']['password'],
:authentication => :plain
}
}
end
end
if __FILE__ == $0
# Contacts
contacts = ['john@example.com', 'paul@example.com']
# Send emails
MailSender.new('config.yml',
contacts,
'Test email',
'message.txt',
'attachment.pdf').handle
end
@yumminhuang
Copy link
Author

Configuration you need to modify in config.yml:

  • fromname: The name that is shown as the sender;
  • hostname and port: Check your email site;
  • username and password: your email address and password.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment