add setup smpt for intranet/internet smtp enable send mail to both curystal.ci and WAN email
Signed-off-by: Luan Shengde luanshengde2@huawei.com --- container/send-mail/mail-post.rb | 47 ++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 17 deletions(-)
diff --git a/container/send-mail/mail-post.rb b/container/send-mail/mail-post.rb index 4eecf6e..ffbf31d 100755 --- a/container/send-mail/mail-post.rb +++ b/container/send-mail/mail-post.rb @@ -7,23 +7,25 @@ require 'sinatra' require 'json' require 'yaml' require 'open3' -require_relative 'send-mail.rb' +require_relative 'send-mail'
set :bind, '0.0.0.0' -set :port, 11311 +set :port, ENV['SEND_MAIL_PORT'] + +mail_server = `/sbin/ip route |awk '/default/ {print $3}'`.chomp
post '/send_mail_yaml' do data = YAML.safe_load request.body.read - raise TypeError, data, 'request data type error' unless data.class.eql? Hash + raise TypeError, data, 'request data type error' unless data.instance_of?(Hash)
mail_info = { 'references' => data['references'] || '', - 'from' => data['from'] || 'team@crystal.ci', - 'subject' => data['subject'], - 'to' => data['to'], - 'body' => data['body'] + 'from' => data['from'] || (ENV['DEFAULT_EMAIL']).to_s, + 'subject' => data['subject'] || '', + 'to' => data['to'] || '', + 'body' => data['body'] || '' } - check_send_mail(mail_info) + check_send_mail(mail_info, mail_server) end
post '/send_mail_text' do @@ -31,18 +33,29 @@ post '/send_mail_text' do
mail_info = { 'references' => data.references || '', - 'from' => data.from || 'team@crystal.ci', - 'subject' => data.subject, - 'to' => data.to, - 'body' => data.body.decoded + 'from' => data.from || (ENV['DEFAULT_EMAIL']).to_s, + 'subject' => data.subject || '', + 'to' => data.to[0] || '', + 'body' => data.body.decoded || '' } - check_send_mail(mail_info) + check_send_mail(mail_info, mail_server) end
-def check_send_mail(mail_info) - raise TypeError, data, 'empty subject.' if mail_info['subject'].empty? - raise TypeError, data, 'empty email address.' if mail_info['to'].empty? - raise TypeError, data, 'empty email content.' if mail_info['body'].empty? +def check_send_mail(mail_info, mail_server) + raise 'empty subject.' if mail_info['subject'].empty? + raise 'empty email address.' if mail_info['to'].empty? + raise 'empty email content.' if mail_info['body'].empty? + + check_smtp(mail_info, mail_server)
send_mail(mail_info) end + +def check_smtp(mail_info, mail_server) + if [nil, 'crystal.ci'].include? mail_info['to'].split('@')[1] + intranet_smtp(mail_server) + else + internet_smtp + mail_info['from'] = (ENV['EMAIL_ADDR']).to_s + end +end
On Fri, Oct 16, 2020 at 02:06:55PM +0800, Luan Shengde wrote:
add setup smpt for intranet/internet smtp enable send mail to both curystal.ci and WAN email
Signed-off-by: Luan Shengde luanshengde2@huawei.com
container/send-mail/mail-post.rb | 47 ++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 17 deletions(-)
diff --git a/container/send-mail/mail-post.rb b/container/send-mail/mail-post.rb index 4eecf6e..ffbf31d 100755 --- a/container/send-mail/mail-post.rb +++ b/container/send-mail/mail-post.rb @@ -7,23 +7,25 @@ require 'sinatra' require 'json' require 'yaml' require 'open3' -require_relative 'send-mail.rb'
You can remove the file suffix. If others change the file to shell scripts or others, They only change the content.
Thanks, Baijing
+require_relative 'send-mail'
set :bind, '0.0.0.0' -set :port, 11311 +set :port, ENV['SEND_MAIL_PORT']
+mail_server = `/sbin/ip route |awk '/default/ {print $3}'`.chomp
post '/send_mail_yaml' do data = YAML.safe_load request.body.read
- raise TypeError, data, 'request data type error' unless data.class.eql? Hash
raise TypeError, data, 'request data type error' unless data.instance_of?(Hash)
mail_info = { 'references' => data['references'] || '',
- 'from' => data['from'] || 'team@crystal.ci',
- 'subject' => data['subject'],
- 'to' => data['to'],
- 'body' => data['body']
- 'from' => data['from'] || (ENV['DEFAULT_EMAIL']).to_s,
- 'subject' => data['subject'] || '',
- 'to' => data['to'] || '',
- 'body' => data['body'] || '' }
- check_send_mail(mail_info)
- check_send_mail(mail_info, mail_server)
end
post '/send_mail_text' do @@ -31,18 +33,29 @@ post '/send_mail_text' do
mail_info = { 'references' => data.references || '',
- 'from' => data.from || 'team@crystal.ci',
- 'subject' => data.subject,
- 'to' => data.to,
- 'body' => data.body.decoded
- 'from' => data.from || (ENV['DEFAULT_EMAIL']).to_s,
- 'subject' => data.subject || '',
- 'to' => data.to[0] || '',
- 'body' => data.body.decoded || '' }
- check_send_mail(mail_info)
- check_send_mail(mail_info, mail_server)
end
-def check_send_mail(mail_info)
- raise TypeError, data, 'empty subject.' if mail_info['subject'].empty?
- raise TypeError, data, 'empty email address.' if mail_info['to'].empty?
- raise TypeError, data, 'empty email content.' if mail_info['body'].empty?
+def check_send_mail(mail_info, mail_server)
raise 'empty subject.' if mail_info['subject'].empty?
raise 'empty email address.' if mail_info['to'].empty?
raise 'empty email content.' if mail_info['body'].empty?
check_smtp(mail_info, mail_server)
send_mail(mail_info)
end
+def check_smtp(mail_info, mail_server)
- if [nil, 'crystal.ci'].include? mail_info['to'].split('@')[1]
- intranet_smtp(mail_server)
- else
- internet_smtp
- mail_info['from'] = (ENV['EMAIL_ADDR']).to_s
- end
+end
2.23.0
On Fri, Oct 16, 2020 at 02:06:55PM +0800, Luan Shengde wrote:
add setup smpt for intranet/internet smtp
smpt looks like spell error
enable send mail to both curystal.ci and WAN email
curystal? WAN?
- 'from' => data['from'] || (ENV['DEFAULT_EMAIL']).to_s,
DEFAULT_EMAIL is what?
+def check_smtp(mail_info, mail_server)
- if [nil, 'crystal.ci'].include? mail_info['to'].split('@')[1]
- intranet_smtp(mail_server)
- else
- internet_smtp
- mail_info['from'] = (ENV['EMAIL_ADDR']).to_s
EMAIL_ADDR is what?
Looks DEFAULT_EMAIL/EMAIL_ADDR naming should be improved.
Thanks, Fengguang
On Mon, Oct 19, 2020 at 02:46:06PM +0800, Wu Fengguang wrote:
On Fri, Oct 16, 2020 at 02:06:55PM +0800, Luan Shengde wrote:
add setup smpt for intranet/internet smtp
smpt looks like spell error
I got it
enable send mail to both curystal.ci and WAN email
curystal?
spell error
WAN?
world area networ email I will update it
- 'from' => data['from'] || (ENV['DEFAULT_EMAIL']).to_s,
DEFAULT_EMAIL is what?
when sending email to crystal.ci, the DEFAULT_EMAIL is team@crystal.ci
+def check_smtp(mail_info, mail_server)
- if [nil, 'crystal.ci'].include? mail_info['to'].split('@')[1]
- intranet_smtp(mail_server)
- else
- internet_smtp
- mail_info['from'] = (ENV['EMAIL_ADDR']).to_s
EMAIL_ADDR is what?
user's email address
Looks DEFAULT_EMAIL/EMAIL_ADDR naming should be improved.
Thanks, Fengguang
WAN?
world area networ email I will update it
Well you used WAN in once place and internat in another place. WAN is against LAN, but you used intranet. Please be consistent.
- 'from' => data['from'] || (ENV['DEFAULT_EMAIL']).to_s,
DEFAULT_EMAIL is what?
when sending email to crystal.ci, the DEFAULT_EMAIL is team@crystal.ci
crystal.ci is not portable.
+def check_smtp(mail_info, mail_server)
- if [nil, 'crystal.ci'].include? mail_info['to'].split('@')[1]
- intranet_smtp(mail_server)
- else
- internet_smtp
- mail_info['from'] = (ENV['EMAIL_ADDR']).to_s
EMAIL_ADDR is what?
user's email address
That's my_email?
Looks DEFAULT_EMAIL/EMAIL_ADDR naming should be improved.
Please suggest new names here.
Thanks, Fengguang
+def check_smtp(mail_info, mail_server)
- if [nil, 'crystal.ci'].include? mail_info['to'].split('@')[1]
That "crystal.ci" won't work in 3rd party deployment.
Thanks, Fengguang
- intranet_smtp(mail_server)
- else
- internet_smtp
- mail_info['from'] = (ENV['EMAIL_ADDR']).to_s
- end
+end
2.23.0
On Mon, Oct 19, 2020 at 02:51:10PM +0800, Wu Fengguang wrote:
+def check_smtp(mail_info, mail_server)
- if [nil, 'crystal.ci'].include? mail_info['to'].split('@')[1]
That "crystal.ci" won't work in 3rd party deployment.
so how to deal with the service send mail for crystal.ci and internet email? separate them in different containers?
Thanks Luan Shengde
Thanks, Fengguang
- intranet_smtp(mail_server)
- else
- internet_smtp
- mail_info['from'] = (ENV['EMAIL_ADDR']).to_s
- end
+end
2.23.0
On Mon, Oct 19, 2020 at 03:11:25PM +0800, Luan Shengde wrote:
On Mon, Oct 19, 2020 at 02:51:10PM +0800, Wu Fengguang wrote:
+def check_smtp(mail_info, mail_server)
- if [nil, 'crystal.ci'].include? mail_info['to'].split('@')[1]
That "crystal.ci" won't work in 3rd party deployment.
so how to deal with the service send mail for crystal.ci and internet email? separate them in different containers?
'crystal.ci' can be passed from local server domain name, instead of hard coded. Better split the containers. The code can reuse if possible.
Thanks, Fengguang
- intranet_smtp(mail_server)
- else
- internet_smtp
- mail_info['from'] = (ENV['EMAIL_ADDR']).to_s
- end
+end
2.23.0