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