send-mail/main.rb

45 lines
988 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
server_address = ENV['INPUT_SERVER_ADDRESS']
server_port = ENV['INPUT_SERVER_PORT']
2019-12-06 16:18:17 +07:00
username = ENV['INPUT_USERNAME']
password = ENV['INPUT_PASSWORD']
subject = ENV['INPUT_SUBJECT']
body = ENV['INPUT_BODY']
to = ENV['INPUT_TO']
from = ENV['INPUT_FROM']
content_type = ENV['INPUT_CONTENT_TYPE'] || 'text/plain'
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
Content-Type: #{content_type}; charset=utf-8
2019-12-06 16:18:17 +07:00
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(server_address, server_port.to_i)
2019-11-18 19:23:22 +07:00
smtp.enable_tls
smtp.start(server_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