|
| 1 | +package gomail |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/go-playground/validator/v10" |
| 11 | + tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1" |
| 12 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 13 | +) |
| 14 | + |
| 15 | +// Body to send to the function (via curl for example or Messaging & Queuing). |
| 16 | + |
| 17 | +// { |
| 18 | + |
| 19 | +// "subject": "from console test", |
| 20 | +// "message": "very very long msg" |
| 21 | +// } |
| 22 | + |
| 23 | +// Region used to call the API. |
| 24 | +const region = scw.RegionFrPar |
| 25 | + |
| 26 | +// Data holds the body of the HTTP call. Fields in Data must be completed |
| 27 | +// to send an email |
| 28 | +type Data struct { |
| 29 | + Subject string `json:"subject" validate:"required"` |
| 30 | + Message string `json:"message" validate:"required"` |
| 31 | + To string `json:"to" validate:"required,email"` |
| 32 | +} |
| 33 | + |
| 34 | +// Handler is the entrypoint of the function. |
| 35 | +func Handler(respWriter http.ResponseWriter, req *http.Request) { |
| 36 | + // Only allow POST verbs |
| 37 | + if req.Method != http.MethodPost { |
| 38 | + respWriter.WriteHeader(http.StatusMethodNotAllowed) |
| 39 | + |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + var body Data |
| 44 | + |
| 45 | + if err := json.NewDecoder(req.Body).Decode(&body); err != nil { |
| 46 | + respWriter.WriteHeader(http.StatusBadRequest) |
| 47 | + |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + // Validate the email data, return a 400 error if not valid |
| 52 | + |
| 53 | + if err := validator.New().Struct(body); err != nil { |
| 54 | + respWriter.WriteHeader(http.StatusBadRequest) |
| 55 | + _, _ = respWriter.Write([]byte(err.Error())) |
| 56 | + |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + if err := sendMail(body.Subject, body.Message, body.To, "CHANGE_ME", false); err != nil { |
| 61 | + respWriter.WriteHeader(http.StatusInternalServerError) |
| 62 | + _, _ = respWriter.Write([]byte(err.Error())) |
| 63 | + |
| 64 | + return |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// sendMail sends a mail to "mailTo" using "from" email. |
| 69 | +// If checkMailStatus is at true, the function will take more time to run some calls |
| 70 | +// to the API in order to get mail status over time. |
| 71 | +func sendMail(subject, content, mailTo, from string, checkMailStatus bool) error { |
| 72 | + // Create a Scaleway client |
| 73 | + client, err := scw.NewClient( |
| 74 | + // Get your organization ID at https://console.scaleway.com/organization/settings |
| 75 | + scw.WithDefaultOrganizationID(os.Getenv("SCW_DEFAULT_ORGANIZATION_ID")), |
| 76 | + // Get your credentials at https://console.scaleway.com/iam/api-keys |
| 77 | + scw.WithAuth(os.Getenv("SCW_ACCESS_KEY"), os.Getenv("SCW_SECRET_KEY")), |
| 78 | + // Get more about our availability zones at |
| 79 | + // https://www.scaleway.com/en/docs/console/my-account/reference-content/products-availability/ |
| 80 | + scw.WithDefaultRegion(region), |
| 81 | + ) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("error creating scaleway client with sdk %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + // Create SDK object to manipulate transactional email. |
| 87 | + temAPI := tem.NewAPI(client) |
| 88 | + |
| 89 | + // Create email is used to send the email to the destination. |
| 90 | + mailResp, err := temAPI.CreateEmail(&tem.CreateEmailRequest{ |
| 91 | + Subject: subject, |
| 92 | + Text: content, |
| 93 | + ProjectID: os.Getenv("SCW_DEFAULT_ORGANIZATION_ID"), |
| 94 | + From: &tem.CreateEmailRequestAddress{Email: from}, |
| 95 | + To: []*tem.CreateEmailRequestAddress{{Email: mailTo}}, |
| 96 | + Region: region, |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("error trying to create and send mail %w", err) |
| 100 | + } |
| 101 | + |
| 102 | + if checkMailStatus { |
| 103 | + // Now we get the email ID in order to get it's status |
| 104 | + emailID := mailResp.Emails[0].ID |
| 105 | + |
| 106 | + mail, err := temAPI.GetEmail(&tem.GetEmailRequest{EmailID: emailID}) |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("error trying to get mail on first time %w", err) |
| 109 | + } |
| 110 | + |
| 111 | + fmt.Println("mail status :", mail.Status.String()) |
| 112 | + |
| 113 | + // This sleep is only for educational purposes, it should be removed for industrial applications. |
| 114 | + time.Sleep(5 * time.Second) |
| 115 | + |
| 116 | + mail, err = temAPI.GetEmail(&tem.GetEmailRequest{EmailID: emailID}) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("error trying to get mail on second time %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + fmt.Println("mail status after 5 seconds :", mail.Status.String()) |
| 122 | + // expected output: |
| 123 | + // mail status : new |
| 124 | + // mail status after 5 seconds : sent |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
0 commit comments