get request for send mail analysis email data invoke send-internet-mail to send the mail
Signed-off-by: Luan Shengde luanshengde2@huawei.com --- lib/mail-post.rb | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 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..889fb23 --- /dev/null +++ b/lib/mail-post.rb @@ -0,0 +1,66 @@ +#!/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['SMTP_FILE']}" + +set :bind, '0.0.0.0' +set :port, ENV['SEND_MAIL_PORT'] + +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'], + 'to' => data['to'], + 'body' => data['body'] + } + 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, + 'to' => data.to, + 'body' => data.body.decoded + } + check_send_mail(mail_info) +end + +def check_send_mail(mail_info) + if ENV['SMTP_REGION'] == 'internet-smtp' + mail_info['from'] = ENV['ROBOT_EMAIL_ADDRESS'] + else + mail_info['from'] = ENV['ROBOT_EMAIL_ADDRESS'] if mail_info['from'].nil? + end + + 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 + +def send_mail(mail_info) + setup_smtp + 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
On Wed, Oct 21, 2020 at 09:27:55AM +0800, Luan Shengde wrote:
get request for send mail analysis email data invoke send-internet-mail to send the mail
Signed-off-by: Luan Shengde luanshengde2@huawei.com
lib/mail-post.rb | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 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..889fb23 --- /dev/null +++ b/lib/mail-post.rb @@ -0,0 +1,66 @@ +#!/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['SMTP_FILE']}"
+set :bind, '0.0.0.0' +set :port, ENV['SEND_MAIL_PORT']
+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'],
- 'to' => data['to'],
- 'body' => data['body']
- }
- 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,
- 'to' => data.to,
- 'body' => data.body.decoded
- }
- check_send_mail(mail_info)
+end
+def check_send_mail(mail_info)
- if ENV['SMTP_REGION'] == 'internet-smtp'
Perhaps no need to test that. Just use
mail_info['from'] ||= ENV['FROM_ADDRESS']
And let the 2 containers pass different FROM_ADDRESS env into docker.
- mail_info['from'] = ENV['ROBOT_EMAIL_ADDRESS']
- else
- mail_info['from'] = ENV['ROBOT_EMAIL_ADDRESS'] if mail_info['from'].nil?
- end
- 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
+def send_mail(mail_info)
- setup_smtp
Can define the local version setup_smtp() here. And let the internet version override it.
Thanks, Fengguang
- 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
+def check_send_mail(mail_info)
- if ENV['SMTP_REGION'] == 'internet-smtp'
Perhaps no need to test that. Just use
mail_info['from'] ||= ENV['FROM_ADDRESS']
And let the 2 containers pass different FROM_ADDRESS env into docker.
for send internet mail, the FROM_ADDRESS must be same to the user_name setted in smtp for send intranet mail, the FROM_ADDRESS can be either user defined or default setted.
so if send internet mail the FROM_ADDRESS must be resetted, and if send intranet mail, the FROM_ADDRESS use user defined or a default value, and user defined value has high priority.
- mail_info['from'] = ENV['ROBOT_EMAIL_ADDRESS']
- else
- mail_info['from'] = ENV['ROBOT_EMAIL_ADDRESS'] if mail_info['from'].nil?
- end
- 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
+def send_mail(mail_info)
- setup_smtp
Can define the local version setup_smtp() here. And let the internet version override it.
do you mean:
define a function setup_smtp for send intranet mail? define a function setup_smtp_internet for send internet mail in lib/internet-smtp.rb?
default run setup_smtp? run setup_smtp_internet ENV['SMTP_REGION'] == 'internet-smtp'?
Thanks Luan Shengde
Thanks, Fengguang
- 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
On Wed, Oct 21, 2020 at 02:51:00PM +0800, Luan Shengde wrote:
+def check_send_mail(mail_info)
- if ENV['SMTP_REGION'] == 'internet-smtp'
Perhaps no need to test that. Just use
mail_info['from'] ||= ENV['FROM_ADDRESS']
And let the 2 containers pass different FROM_ADDRESS env into docker.
for send internet mail, the FROM_ADDRESS must be same to the user_name setted in smtp
Then let it fail. Why would the client set "from:" for Internet email at all?
Thanks, Fengguang
On Wed, Oct 21, 2020 at 03:01:28PM +0800, Wu Fengguang wrote:
On Wed, Oct 21, 2020 at 02:51:00PM +0800, Luan Shengde wrote:
+def check_send_mail(mail_info)
- if ENV['SMTP_REGION'] == 'internet-smtp'
Perhaps no need to test that. Just use
mail_info['from'] ||= ENV['FROM_ADDRESS']
And let the 2 containers pass different FROM_ADDRESS env into docker.
for send internet mail, the FROM_ADDRESS must be same to the user_name setted in smtp
Then let it fail. Why would the client set "from:" for Internet email at all?
because the smtp setting need authentication with a email_addresss if you want to send a internet via the smtp setting, the from must be same to the user_name in smtp setting. if not the authentication will fail.
unless build authentication for user defined 'form:' in smtp setting for sending internet mail, or else send internet mail will end with failure.
Thanks Luan Shengde
Thanks, Fengguang
On Wed, Oct 21, 2020 at 04:03:32PM +0800, Luan Shengde wrote:
On Wed, Oct 21, 2020 at 03:01:28PM +0800, Wu Fengguang wrote:
On Wed, Oct 21, 2020 at 02:51:00PM +0800, Luan Shengde wrote:
+def check_send_mail(mail_info)
- if ENV['SMTP_REGION'] == 'internet-smtp'
Perhaps no need to test that. Just use
mail_info['from'] ||= ENV['FROM_ADDRESS']
And let the 2 containers pass different FROM_ADDRESS env into docker.
for send internet mail, the FROM_ADDRESS must be same to the user_name setted in smtp
Then let it fail. Why would the client set "from:" for Internet email at all?
我的意思, 我们自己的REST client, 就不填"from:"字段了。这样可行?
because the smtp setting need authentication with a email_addresss if you want to send a internet via the smtp setting, the from must be same to the user_name in smtp setting. if not the authentication will fail.
unless build authentication for user defined 'form:' in smtp setting for sending internet mail, or else send internet mail will end with failure.
Thanks Luan Shengde
Thanks, Fengguang
On Wed, Oct 21, 2020 at 04:14:46PM +0800, Wu Fengguang wrote:
On Wed, Oct 21, 2020 at 04:03:32PM +0800, Luan Shengde wrote:
On Wed, Oct 21, 2020 at 03:01:28PM +0800, Wu Fengguang wrote:
On Wed, Oct 21, 2020 at 02:51:00PM +0800, Luan Shengde wrote:
+def check_send_mail(mail_info)
- if ENV['SMTP_REGION'] == 'internet-smtp'
Perhaps no need to test that. Just use
mail_info['from'] ||= ENV['FROM_ADDRESS']
And let the 2 containers pass different FROM_ADDRESS env into docker.
for send internet mail, the FROM_ADDRESS must be same to the user_name setted in smtp
Then let it fail. Why would the client set "from:" for Internet email at all?
我的意思, 我们自己的REST client, 就不填"from:"字段了。这样可行?
yes, it can be this
Thanks Luan Shengde
because the smtp setting need authentication with a email_addresss if you want to send a internet via the smtp setting, the from must be same to the user_name in smtp setting. if not the authentication will fail.
unless build authentication for user defined 'form:' in smtp setting for sending internet mail, or else send internet mail will end with failure.
Thanks Luan Shengde
Thanks, Fengguang
+def send_mail(mail_info)
- setup_smtp
Can define the local version setup_smtp() here. And let the internet version override it.
do you mean:
define a function setup_smtp for send intranet mail? define a function setup_smtp_internet for send internet mail in lib/internet-smtp.rb?
我说的够清楚了吧! 太费劲了,不解释了。回头我做给你看吧。 你把别的改好先。
Thanks, Fengguang