get request for send internet mail analysis email data invoke send-internet-mail to send the mail
Signed-off-by: Luan Shengde luanshengde2@huawei.com --- lib/mail-post.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 lib/mail-post.rb
diff --git a/lib/mail-post.rb b/lib/mail-post.rb new file mode 100755 index 0000000..ea8bf56 --- /dev/null +++ b/lib/mail-post.rb @@ -0,0 +1,50 @@ +#!/usr/bin/env ruby +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. +# frozen_string_literal: true + +require 'sinatra' +require 'json' +require 'yaml' +require 'open3' +require_relative "#{ENV['CCI_SRC']}/container/send-internet-mail/send-internet-mail.rb" + +set :bind, '0.0.0.0' +set :port, 11312 + +post '/send_mail_yaml' do + data = YAML.safe_load request.body.read + raise TypeError, data, 'request data type error' unless data.class.eql? Hash + + mail_info = { + 'references' => data['references'], + 'from' => data['from'], + 'subject' => data['subject'] || nil, + 'to' => data['to'] || nil, + 'body' => data['body'] || nil + } + check_send_mail(mail_info) +end + +post '/send_mail_text' do + data = Mail.read_from_string(request.body.read) + + mail_info = { + 'references' => data.references, + 'from' => data.from, + 'subject' => data.subject || nil, + 'to' => data.to || nil, + 'body' => data.body.decoded || nil + } + check_send_mail(mail_info) +end + +def check_send_mail(mail_info) + raise 'empty subject.' if mail_info['subject'].nil? + raise 'empty email address.' if mail_info['to'].nil? + raise 'empty email content.' if mail_info['body'].nil? + + puts ENV['EMAIL_ADDRESS'] + puts ENV['EMAIL_PASSWORD'] + send_mail(mail_info) +end