Skip to content

Instantly share code, notes, and snippets.

@wndxlori
Last active April 15, 2024 20:00
Show Gist options
  • Save wndxlori/3348dd63d1d79e90b93facc5c6294e7d to your computer and use it in GitHub Desktop.
Save wndxlori/3348dd63d1d79e90b93facc5c6294e7d to your computer and use it in GitHub Desktop.
When you have a whole LOT of ebooks to send to your Kindle
#!/Usr/bin/env ruby
require 'mail'
require 'fileutils'
# Check if a command line argument is provided
if ARGV.length < 1
puts "Usage: ruby send_to-kindle.rb <directory containing epub files>"
exit 1
end
# Access the command line parameter, directory containing the files
# to be emailed to your Kindle
directory = File.expand_path(ARGV[0])
puts "The directory with epub's is: #{directory}"
# Email addresses to send the files to and from, specific to your
# desination Kindle. On Amazon, these account settings are found:
# Manage your Content & Devices | Preferences | Personal Document Settings
kindle_email = "your_kindle@kindle.com"
auth_email = 'your_authorized_email@example.com'
# Configure mail settings. See mail gem docs for alternativees to smtp
Mail.defaults do
delivery_method :smtp, address: "mail.example.com", port: 25
end
# Iterate over each file in the directory
Dir.foreach(directory) do |file|
next if file == '.' || file == '..'
file_path = File.join(directory, file)
if File.file?(file_path) && File.extname(file_path) == '.epub'
# Get the file name
filename = File.basename(file_path)
# Send email with the file attached
puts "Sending email for file: #{filename}"
subject = "File: #{filename}"
body = "Please add the attached file."
# Sending email with attachment
mail = Mail.new do
to kindle_email
from auth_email
subject subject
body body
add_file file_path
end
mail.deliver
puts "Email sent for file: #{filename}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment