Skip to content

Instantly share code, notes, and snippets.

@richardTowers
Created October 20, 2022 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardTowers/419a1e15f7d839fc49dcdeeba3780718 to your computer and use it in GitHub Desktop.
Save richardTowers/419a1e15f7d839fc49dcdeeba3780718 to your computer and use it in GitHub Desktop.
Send lots of emails with attached files using GOV.UK Notify

Send lots of emails with attached files using GOV.UK Notify

Usage

Create a template with a single ((link_to_file)) placeholder in Notify, and an API key. Then call the script with the filename to attach, the API key, and the template ID as environment variables, and a list of email addresses on standard in.

export API_KEY="redacted"
export TEMPLATE_ID="redacted"
export FILENAME_TO_UPLOAD="whatever.docx" 
cat list-of-email-addresses | ./email-with-notify.rb
#!/usr/bin/env ruby
require 'notifications/client'
client = Notifications::Client.new(ENV.fetch("API_KEY"))
File.open(ENV.fetch("FILENAME_TO_UPLOAD"), "rb") do |f|
upload = Notifications.prepare_upload(f)
template_id = ENV.fetch("TEMPLATE_ID")
ARGF.readlines.each do |line|
line = line.strip
puts "Attempting for #{line}"
emailresponse = client.send_email(
email_address: line,
template_id: template_id,
personalisation: {
link_to_file: upload
}
)
puts "Succeeded for #{line}"
rescue Notifications::Client::RequestError => error
puts "Error for #{line} - #{error.code}: #{error.message}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment