
On Fri, Oct 23, 2020 at 11:16:24AM +0800, Xiao Shenwei wrote:
On Fri, Oct 23, 2020 at 10:56:27AM +0800, Luan Shengde wrote:
get request for send mail analysis email data send mail
default use local defined smtp setup when required internet-smtp, will override the local setup and use internet-smtp instead
Signed-off-by: Luan Shengde <luanshengde2@huawei.com> --- lib/mail-post.rb | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 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..e62640a --- /dev/null +++ b/lib/mail-post.rb @@ -0,0 +1,68 @@ +#!/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 'mail' + +set :bind, '0.0.0.0' +set :port, ENV['SEND_MAIL_PORT'] + +mail_server = `/sbin/ip route |awk '/default/ {print $3}'`.chomp
^ how adout add a space
I see it
+ +smtp = { + address: mail_server, + enable_starttls_auto: false +} + +Mail.defaults { delivery_method :smtp, smtp } + +post '/send_mail_yaml' do + data = YAML.safe_load request.body.read + raise TypeError, data, 'request data type error' unless data.class.eql? Hash
date.is_a?(Hash)
data, parsed from json/yaml string, it's type is hash.
+ + mail_info = { + 'references' => data['references'], + 'subject' => data['subject'], + 'to' => data['to'], + 'body' => data['body'] + } + check_send_mail(mail_info) +end + +post '/send_mail_text' do
i feel post '/send_mail_yaml' and post '/send_mail_text' high similarity, can't merge it?
They are different data format, send_mail_yaml: hash send_mail_text: mail_file_content I merged yaml/json if merge send_mail_yaml/send_mail_text it will need much judgement and make it complex.
+ data = Mail.read_from_string(request.body.read) + + mail_info = { + 'references' => data.references, + 'subject' => data.subject, + 'to' => data.to, + 'body' => data.body.decoded + } + check_send_mail(mail_info) +end + +def check_send_mail(mail_info) + mail_info['from'] = ENV['ROBOT_EMAIL_ADDRESS'] + + raise 'no subject.' if mail_info['subject'].nil? + raise 'no email address.' if mail_info['to'].nil? + raise 'no email content.' if mail_info['body'].nil? + + send_mail(mail_info) +end
hwo about check_send_mail return true or false
how
if check_send_mail; then send_mail fi
when there is nil value for subject/to/body, I need to raise error, If use return true/false, I will get nothing form the log based on the inspiration you suggested, maybe def send_mail(mail_info) check_send_mail(mail_info) mail = ... is a good choice. Thanks Luan Shengde
Thanks, Shenwei
+ +def send_mail(mail_info) + mail = Mail.new do + references mail_info['references'] + from mail_info['from'] + subject mail_info['subject'] + to mail_info['to'] + body mail_info['body'] + end + mail.deliver! +end -- 2.23.0