|
| 1 | +/* |
| 2 | +* Copyright 2021 Layotto Authors |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package aliyun |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "strings" |
| 22 | + |
| 23 | + openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" |
| 24 | + dm20151123 "github.com/alibabacloud-go/dm-20151123/v2/client" |
| 25 | + "github.com/alibabacloud-go/tea/tea" |
| 26 | + |
| 27 | + "mosn.io/layotto/components/email" |
| 28 | +) |
| 29 | + |
| 30 | +type AliyunEmail struct { |
| 31 | + client *dm20151123.Client |
| 32 | +} |
| 33 | + |
| 34 | +func NewAliyunEmail() email.EmailService { |
| 35 | + return &AliyunEmail{} |
| 36 | +} |
| 37 | + |
| 38 | +var _ email.EmailService = (*AliyunEmail)(nil) |
| 39 | + |
| 40 | +func (a *AliyunEmail) Init(ctx context.Context, conf *email.Config) error { |
| 41 | + accessKeyID := conf.Metadata[email.ClientKey] |
| 42 | + accessKeySecret := conf.Metadata[email.ClientSecret] |
| 43 | + endpoint := conf.Metadata[email.Endpoint] |
| 44 | + config := &openapi.Config{ |
| 45 | + // accessKey ID |
| 46 | + AccessKeyId: tea.String(accessKeyID), |
| 47 | + // accessKey Secret |
| 48 | + AccessKeySecret: tea.String(accessKeySecret), |
| 49 | + // endpoint, ref https://api.aliyun.com/product/Dm |
| 50 | + Endpoint: tea.String(endpoint), |
| 51 | + } |
| 52 | + |
| 53 | + client, err := dm20151123.NewClient(config) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + a.client = client |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +// SendEmail . |
| 62 | +func (a *AliyunEmail) SendEmail(ctx context.Context, req *email.SendEmailRequest) (*email.SendEmailResponse, error) { |
| 63 | + if !a.checkSendRequest(req) { |
| 64 | + return nil, email.ErrInvalid |
| 65 | + } |
| 66 | + |
| 67 | + // AccountName is the email send from |
| 68 | + accountName := req.Address.From |
| 69 | + // ToAddress is target addresses the email send to |
| 70 | + toAddress := strings.Join(req.Address.To, ",") |
| 71 | + |
| 72 | + sendMailRequest := &dm20151123.SingleSendMailRequest{} |
| 73 | + sendMailRequest. |
| 74 | + SetAccountName(accountName). |
| 75 | + // AddressType = 1: use the email send from |
| 76 | + // ref https://help.aliyun.com/document_detail/29444.html |
| 77 | + SetAddressType(1). |
| 78 | + // ReplyToAddress = false: the email no need to reply |
| 79 | + // ref https://help.aliyun.com/document_detail/29444.html |
| 80 | + SetReplyToAddress(false). |
| 81 | + SetSubject(req.Subject). |
| 82 | + SetToAddress(toAddress). |
| 83 | + SetTextBody(req.Content.Text) |
| 84 | + |
| 85 | + resp, err := a.client.SingleSendMail(sendMailRequest) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + return &email.SendEmailResponse{ |
| 90 | + RequestId: *resp.Body.RequestId, |
| 91 | + }, nil |
| 92 | +} |
| 93 | + |
| 94 | +func (a *AliyunEmail) checkSendRequest(r *email.SendEmailRequest) bool { |
| 95 | + // make sure content not empty |
| 96 | + if r.Content == nil || r.Content.Text == "" { |
| 97 | + return false |
| 98 | + } |
| 99 | + if info := r.Address; info == nil || info.From == "" || len(info.To) == 0 { |
| 100 | + return false |
| 101 | + } |
| 102 | + return true |
| 103 | +} |
| 104 | + |
| 105 | +// SendEmailWithTemplate . |
| 106 | +// template must have been applied in aliyun console, and there need the template name |
| 107 | +// receivers must have been filled in aliyun console, and there need the receivers list name |
| 108 | +func (a *AliyunEmail) SendEmailWithTemplate(ctx context.Context, req *email.SendEmailWithTemplateRequest) (*email.SendEmailWithTemplateResponse, error) { |
| 109 | + if !a.checkSendWithTemplateRequest(req) { |
| 110 | + return nil, email.ErrInvalid |
| 111 | + } |
| 112 | + |
| 113 | + // AccountName is the email send from |
| 114 | + accountName := req.Address.From |
| 115 | + // ReceiversName is the name of the recipient list that is created in advance and uploaded with recipients. |
| 116 | + // Only take the element with index zero |
| 117 | + receiversName := req.Address.To[0] |
| 118 | + |
| 119 | + sendMailWithTemplateRequest := &dm20151123.BatchSendMailRequest{} |
| 120 | + sendMailWithTemplateRequest. |
| 121 | + SetAccountName(accountName). |
| 122 | + // AddressType = 1: use the email send from |
| 123 | + // ref https://help.aliyun.com/document_detail/29444.html |
| 124 | + SetAddressType(1). |
| 125 | + SetReceiversName(receiversName). |
| 126 | + SetTemplateName(req.Template.TemplateId) |
| 127 | + |
| 128 | + resp, err := a.client.BatchSendMail(sendMailWithTemplateRequest) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + return &email.SendEmailWithTemplateResponse{ |
| 133 | + RequestId: *resp.Body.RequestId, |
| 134 | + }, nil |
| 135 | +} |
| 136 | + |
| 137 | +func (a *AliyunEmail) checkSendWithTemplateRequest(r *email.SendEmailWithTemplateRequest) bool { |
| 138 | + // make sure template exist |
| 139 | + if r.Template == nil || r.Template.TemplateId == "" { |
| 140 | + return false |
| 141 | + } |
| 142 | + if info := r.Address; info == nil || info.From == "" || len(info.To) == 0 { |
| 143 | + return false |
| 144 | + } |
| 145 | + return true |
| 146 | +} |
0 commit comments