Skip to content

Commit

Permalink
send email
Browse files Browse the repository at this point in the history
  • Loading branch information
chientrm committed Jul 18, 2023
1 parent 908ac15 commit 35bcef1
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 7 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ Enable Cloudflare Workers runtime for local development.
| `raw()` ||
| `bind()` ||

### Email Routing

```ts
sendEmail = async (data : {
seb?: SendEmail; // Platform binding object
sebName: string; // send_email binding name
name: string; // sender name
addr: string; // sender address
recipent: string; // recipent address
subject: string; // raw text
contentType: string; // usually 'text/plain'
data: string;
options?: { hostname?: string };
})
```

## Contributing

Just pull request 😐
1 change: 1 addition & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('esbuild').build({
outdir: 'dist',
bundle: true,
format: 'esm',
external: ['cloudflare:email'],
plugins: [
{
name: 'on-end',
Expand Down
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createD1, createWorker } from './src/worker';
import { createD1, createWorker, sendEmail } from './src/worker';

export { createD1, createWorker };
export { createD1, createWorker, sendEmail };
118 changes: 116 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"esbuild": "^0.18.10",
"itty-router": "^4.0.13",
"itty-router-extras": "^0.4.5",
"mimetext": "^3.0.16",
"typescript": "^5.1.3",
"wrangler": "^3.0.0"
},
Expand All @@ -26,4 +27,4 @@
"local",
"development"
]
}
}
3 changes: 3 additions & 0 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { D1DatabaseExecProxy } from './proxies/d1_database/exec/proxy';
import { D1DatabasePreparedStatementAllProxy } from './proxies/d1_database/prepared_statement/all/proxy';
import { D1DatabasePreparedStatementFirstProxy } from './proxies/d1_database/prepared_statement/first/proxy';
import { D1DatabasePreparedStatementRunProxy } from './proxies/d1_database/prepared_statement/run/proxy';
import { SendEmailProxy } from './proxies/send_email/proxy';

class ProxyFactory {
public static getProxy(postData: PostData) {
Expand All @@ -16,6 +17,8 @@ class ProxyFactory {
return new D1DatabasePreparedStatementRunProxy({ name, payload });
case D1DatabaseExecProxy.proxyType:
return new D1DatabaseExecProxy({ name, payload });
case SendEmailProxy.proxyType:
return new SendEmailProxy({ name, payload });
default:
throw new Error('Unknown proxy type.');
}
Expand Down
44 changes: 44 additions & 0 deletions src/proxies/send_email/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createMimeMessage } from 'mimetext';
import { Proxy } from '../proxy';

interface Payload {
sebName: string;
name: string;
addr: string;
recipent: string;
subject: string;
contentType: string;
data: string;
}

class SendEmailProxy extends Proxy<Payload> {
static readonly proxyType = 'SendEmailProxy';
constructor({
host,
name,
payload,
}: {
host?: string;
name: string;
payload: Payload;
}) {
const proxyType = SendEmailProxy.proxyType;
super({ proxyType, host, name, payload });
}
async execute(env: any) {
const { sebName, name, addr, recipent, subject, contentType, data } =
this.payload,
seb = env[sebName] as SendEmail,
msg = createMimeMessage();
msg.setSender({ name, addr });
msg.setRecipient(recipent);
msg.setSubject(subject);
msg.addMessage({ contentType, data });
const { EmailMessage } = await import('cloudflare:email');
const message = new EmailMessage(addr, recipent, msg.asRaw());
await seb.send(message);
return {};
}
}

export { SendEmailProxy };
49 changes: 47 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createMimeMessage } from 'mimetext';
import type { ErrorResult, PostData, SuccessResult } from './data';
import { ProxyFactory } from './factory';
import { D1DatabaseProxyHolder } from './proxies/d1_database/proxy_holder';
import { SendEmailProxy } from './proxies/send_email/proxy';

const json = <T>(data: T) => {
const result: SuccessResult<T> = { status: 200, data },
Expand Down Expand Up @@ -47,6 +49,49 @@ const json = <T>(data: T) => {
host: options?.hostname ?? 'http://localhost:8787',
name,
payload: {},
});
}),
sendEmail = async ({
seb,
sebName,
name,
addr,
recipent,
subject,
contentType,
data,
options,
}: {
seb?: SendEmail;
sebName: string;
name: string;
addr: string;
recipent: string;
subject: string;
contentType: string;
data: string;
options?: { hostname?: string };
}) => {
if (seb) {
const msg = createMimeMessage();
msg.setSender({ name, addr });
msg.setRecipient(recipent);
msg.setSubject(subject);
msg.addMessage({ contentType, data });
const { EmailMessage } = await import('cloudflare:email'),
message = new EmailMessage(
'[email protected]',
'[email protected]',
msg.asRaw()
);
await seb.send(message);
} else {
const proxy = new SendEmailProxy({
host: options?.hostname ?? 'http://localhost:8787',
name,
payload: { sebName, name, addr, recipent, subject, contentType, data },
});
await proxy.post();
}
};

export { createD1, createWorker };
export { createD1, createWorker, sendEmail };
1 change: 1 addition & 0 deletions watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require('esbuild')
},
},
],
external: ['cloudflare:email']
})
.then((context) => context.watch())
.then(() => {
Expand Down

0 comments on commit 35bcef1

Please sign in to comment.