Skip to content

Commit

Permalink
feat: email winglib
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr committed Sep 17, 2024
1 parent cfbf8d2 commit c2ecb5b
Show file tree
Hide file tree
Showing 18 changed files with 2,377 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/canary.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions .github/workflows/email-pull.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions .github/workflows/email-release.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .github/workflows/pull-request-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
cognito
containers
dynamodb
email
eventbridge
fifoqueue
github
Expand Down
6 changes: 6 additions & 0 deletions .mergify.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ publishing them for you.
| [cognito](./cognito) | [@winglibs/cognito](https://www.npmjs.com/package/@winglibs/cognito) | sim, tf-aws |
| [containers](./containers) | [@winglibs/containers](https://www.npmjs.com/package/@winglibs/containers) | sim, tf-aws |
| [dynamodb](./dynamodb) | [@winglibs/dynamodb](https://www.npmjs.com/package/@winglibs/dynamodb) | sim, tf-aws |
| [email](./email) | [@winglibs/email](https://www.npmjs.com/package/@winglibs/email) | sim, tf-aws |
| [eventbridge](./eventbridge) | [@winglibs/eventbridge](https://www.npmjs.com/package/@winglibs/eventbridge) | awscdk, sim, tf-aws |
| [fifoqueue](./fifoqueue) | [@winglibs/fifoqueue](https://www.npmjs.com/package/@winglibs/fifoqueue) | sim, tf-aws |
| [github](./github) | [@winglibs/github](https://www.npmjs.com/package/@winglibs/github) | * |
Expand Down
2 changes: 2 additions & 0 deletions email/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
21 changes: 21 additions & 0 deletions email/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Wing

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions email/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# email

## Prerequisites

* [winglang](https://winglang.io).

For AWS, you need to have an AWS account and the AWS CLI installed. You'll also need to have [Terraform](https://developer.hashicorp.com/terraform/install) or [OpenTofu](https://opentofu.org/docs/intro/install/) installed to deploy the application.

## Installation

```sh
npm i @winglibs/email
```

## Usage

```js
bring email;

let email = new email.Email(sender: "[email protected]");

new cloud.Function(inflight () => {
email.send(
to: ["[email protected]"],
subject: "My subject",
text: "My content",
html: "<h1>My content</h1>", // optional
);
});
```

### Simulator

When using `email.Email` in the local simulator, emails are mocked and are emitted to the logs.
A table showing all emails that have been sent can be viewed in the email resource's interaction panel.

### AWS

When compiled to AWS platforms, the email resource uses [Amazon SES](https://aws.amazon.com/ses/).
For testing, we recommend using your own email address for `sender` since sender email addresses must be verified.
When the application is deployed, an email will be sent to verify the configured `sender` address.

By default, new AWS accounts are in the sandbox mode. This means emails can only be sent to verified addresses. It also limits the number of emails that can be sent. To send emails to other addresses, you need to request production access [here](https://docs.aws.amazon.com/ses/latest/dg/request-production-access.html).

## License

This library is licensed under the [MIT License](./LICENSE).
14 changes: 14 additions & 0 deletions email/aws.extern.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions email/aws.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import types from "./aws.extern";
import { SESv2Client, SendEmailCommand } from "@aws-sdk/client-sesv2";

export const _sendEmail: types["_sendEmail"] = async (sender, options) => {
const ses = new SESv2Client();
let res = await ses.send(new SendEmailCommand({
Content: {
Simple: {
Body: {
Text: {
Data: options.text,
Charset: "UTF-8",
},
Html: {
Data: options.html,
Charset: "UTF-8",
}
},
Subject: {
Data: options.subject,
Charset: "UTF-8",
}
}
},
Destination: {
ToAddresses: options.to as string[], // cast from readonly string[] to string[]
},
FromEmailAddress: sender,
}));
return res.MessageId;
};
22 changes: 22 additions & 0 deletions email/lib.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
bring cloud;
bring expect;
bring "./lib.w" as l;

let email = new l.Email(sender: "[email protected]");

test "send email" {
// sending an email with html
email.send(
to: ["[email protected]"],
subject: "My subject",
text: "My body",
html: "<h1>My body</h1>",
);

// sending an email without html
email.send(
to: ["[email protected]"],
subject: "My subject",
text: "My body",
);
}
27 changes: 27 additions & 0 deletions email/lib.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
bring util;
bring "./sim.w" as email_sim;
bring "./tfaws.w" as email_tfaws;
bring "./types.w" as types;

pub class Email {
inner: types.IEmail;
new(props: types.EmailProps) {
let target = util.env("WING_TARGET");

if target == "sim" {
let email = new email_sim.Email_sim(props);
email.addUI(this);
this.inner = email;
} else {
this.inner = new email_tfaws.Email_tfaws(props);
}

nodeof(this.inner).hidden = true;
nodeof(this).icon = "inbox";
nodeof(this).color = "pink";
}

pub inflight send(options: types.SendEmailOptions): void {
this.inner.send(options);
}
}
Loading

0 comments on commit c2ecb5b

Please sign in to comment.