send-mail/main.rb

43 lines
849 B
Ruby
Raw Normal View History

2019-11-18 19:23:22 +07:00
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'net/smtp'
# Inputs
2019-12-06 16:18:17 +07:00
address = ENV['INPUT_SERVER_ADDRESS']
port = ENV['INPUT_SERVER_PORT']
username = ENV['INPUT_USERNAME']
password = ENV['INPUT_PASSWORD']
subject = ENV['INPUT_SUBJECT']
body = ENV['INPUT_BODY']
to = ENV['INPUT_TO']
from = ENV['INPUT_FROM']
2019-11-18 19:23:22 +07:00
2019-12-06 16:26:00 +07:00
# Body
prefix = 'file://'
body = if body.start_with?(prefix)
path = body.delete_prefix(prefix)
File.read(path)
else
body
end
2019-12-06 16:26:00 +07:00
2019-11-18 19:23:22 +07:00
# Message
2019-12-06 16:18:17 +07:00
message = <<~END_OF_MESSAGE
Subject: #{subject}
From: #{from} <#{username}>
2019-11-18 19:23:22 +07:00
2019-12-06 16:18:17 +07:00
#{body}
END_OF_MESSAGE
2019-11-18 19:23:22 +07:00
# Send
begin
smtp = Net::SMTP.new(address, port.to_i)
smtp.enable_tls
smtp.start(address, username, password, :login)
2019-12-06 16:18:17 +07:00
smtp.send_message(message, username, to.split(','))
2019-11-18 19:23:22 +07:00
rescue StandardError => e
puts "Error: #{e.message}"
exit 1
end