Skip to content

Instantly share code, notes, and snippets.

@rafaelcpalmeida
Created April 2, 2018 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelcpalmeida/3f6ed1c98376635241c5ec387dfb1105 to your computer and use it in GitHub Desktop.
Save rafaelcpalmeida/3f6ed1c98376635241c5ec387dfb1105 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'csv'
require 'mail'
# PATH's configuration
base_path = '/Users/rafaelalmeida/Downloads'
pdf_base_path = '/Users/rafaelalmeida/Downloads/certificados-pdf'
# SMTP configuration
options = { :address => 'smtp.server.com',
:port => 587,
:user_name => 'someone@server.com',
:password => 'someone@server.com',
:authentication => 'plain',
:enable_starttls_auto => true }
Mail.defaults do
delivery_method :smtp, options
end
# Start reading CSV
csv_text = File.read("#{base_path}/alunos_final.csv")
csv = CSV.parse(csv_text, :headers => true)
csv.each_with_index do |row, index|
# Ship it!
Mail.deliver do
# My CSV file has name and email as headers
to row.to_hash['email']
from 'John Appleseed <someone@server.com>'
subject 'Here\'s the super promised email script'
# Optional line. Remove if you don't want to send an attachment
add_file "path_to_that_file_you_want_to_send.pdf"
text_part do
content_type 'text/plain; charset=UTF-8'
body 'Hello there\r\nHere\'s the promised email script'
end
html_part do
content_type 'text/html; charset=UTF-8'
body 'Hello there<br>Here\'s the promised email script'
end
end
# My CSV file has name and email as headers
puts "Sent email nº #{index + 1} to #{row.to_hash['name']} <#{row.to_hash['email']}>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment