1ba991379Sopenharmony_ci/* 2ba991379Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3ba991379Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4ba991379Sopenharmony_ci * you may not use this file except in compliance with the License. 5ba991379Sopenharmony_ci * You may obtain a copy of the License at 6ba991379Sopenharmony_ci * 7ba991379Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8ba991379Sopenharmony_ci * 9ba991379Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10ba991379Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11ba991379Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12ba991379Sopenharmony_ci * See the License for the specific language governing permissions and 13ba991379Sopenharmony_ci * limitations under the License. 14ba991379Sopenharmony_ci */ 15ba991379Sopenharmony_ci 16ba991379Sopenharmony_cipackage utils 17ba991379Sopenharmony_ci 18ba991379Sopenharmony_ciimport ( 19ba991379Sopenharmony_ci "crypto/tls" 20ba991379Sopenharmony_ci "fmt" 21ba991379Sopenharmony_ci "github.com/sirupsen/logrus" 22ba991379Sopenharmony_ci "gopkg.in/gomail.v2" 23ba991379Sopenharmony_ci "strconv" 24ba991379Sopenharmony_ci "strings" 25ba991379Sopenharmony_ci) 26ba991379Sopenharmony_ci 27ba991379Sopenharmony_citype MailConfig struct { 28ba991379Sopenharmony_ci Host string `key:"host" default:""` 29ba991379Sopenharmony_ci Port string `key:"port" default:""` 30ba991379Sopenharmony_ci port int 31ba991379Sopenharmony_ci User string `key:"user" default:""` 32ba991379Sopenharmony_ci Password string `key:"password" default:""` 33ba991379Sopenharmony_ci From string `key:"from" default:""` 34ba991379Sopenharmony_ci To string `key:"to" default:""` 35ba991379Sopenharmony_ci toList []string 36ba991379Sopenharmony_ci} 37ba991379Sopenharmony_ci 38ba991379Sopenharmony_civar mailConfig MailConfig 39ba991379Sopenharmony_ci 40ba991379Sopenharmony_cifunc init() { 41ba991379Sopenharmony_ci ParseFromConfigFile("mail", &mailConfig) 42ba991379Sopenharmony_ci if mailConfig.Host != "" { 43ba991379Sopenharmony_ci var err error 44ba991379Sopenharmony_ci if mailConfig.port, err = strconv.Atoi(mailConfig.Port); err != nil { 45ba991379Sopenharmony_ci panic(fmt.Errorf("parse mail port err: %v", err)) 46ba991379Sopenharmony_ci } 47ba991379Sopenharmony_ci mailConfig.toList = strings.Split(mailConfig.To, ",") 48ba991379Sopenharmony_ci } 49ba991379Sopenharmony_ci} 50ba991379Sopenharmony_ci 51ba991379Sopenharmony_cifunc SendMail(subject string, body string, attachments ...string) error { 52ba991379Sopenharmony_ci if mailConfig.Host == "" { 53ba991379Sopenharmony_ci logrus.Info("mail not configured, do nothing") 54ba991379Sopenharmony_ci return nil 55ba991379Sopenharmony_ci } 56ba991379Sopenharmony_ci dail := gomail.NewDialer(mailConfig.Host, mailConfig.port, mailConfig.User, mailConfig.Password) 57ba991379Sopenharmony_ci dail.TLSConfig = &tls.Config{InsecureSkipVerify: true, ServerName: mailConfig.Host} 58ba991379Sopenharmony_ci msg := gomail.NewMessage() 59ba991379Sopenharmony_ci msg.SetBody("text/html", body) 60ba991379Sopenharmony_ci msg.SetHeader("From", mailConfig.From) 61ba991379Sopenharmony_ci msg.SetHeader("To", mailConfig.toList...) 62ba991379Sopenharmony_ci msg.SetHeader("Subject", subject) 63ba991379Sopenharmony_ci for _, a := range attachments { 64ba991379Sopenharmony_ci msg.Attach(a) 65ba991379Sopenharmony_ci } 66ba991379Sopenharmony_ci return dail.DialAndSend(msg) 67ba991379Sopenharmony_ci} 68