Skip to content

Commit e24b3d3

Browse files
committed
add sending emails
1 parent 4c6d208 commit e24b3d3

File tree

8 files changed

+683
-0
lines changed

8 files changed

+683
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Using functions for Sending Email
2+
3+
This lesson will walk through creating a function to process payments via [Stripe](https://www.stripe.com/).
4+
5+
<!-- AUTO-GENERATED-CONTENT:START (TOC) -->
6+
- [Lesson Steps](#lesson-steps)
7+
- [Complete code](#complete-code)
8+
<!-- AUTO-GENERATED-CONTENT:END -->
9+
10+
## Lesson Steps
11+
12+
<!-- AUTO-GENERATED-CONTENT:START (GENERATE_LESSONS_STEPS)-->
13+
1. Create a `netlify.toml` file in your project.
14+
15+
The `netlify.toml` file is where we configure how the site builds and where your serverless functions live.
16+
17+
2. In `netlify.toml`, add a `[build]` section and add `publish` and `functions` values
18+
19+
3. We need to create this site in Netlify
20+
21+
Open your terminal and run the following command:
22+
23+
```bash
24+
netlify init
25+
```
26+
27+
Choose manual deployments
28+
29+
5. After configuring your redirect, its time to deploy
30+
31+
Open your terminal and run the following command:
32+
33+
```bash
34+
netlify deploy -p
35+
```
36+
37+
6. Invoke the function to ensure it's working properly
38+
39+
Visit your site and the URL https://your-site-url.netlify.com/.netlify/functions/redirect
40+
41+
It should redirect you to a new location.
42+
<!-- AUTO-GENERATED-CONTENT:END -->
43+
44+
<!-- SCROLL UP FOR STEPS -->
45+
46+
<!-- Step 1. Create a `netlify.toml` file in your project.
47+
48+
The `netlify.toml` file is where we configure how the site builds and where your serverless functions live.
49+
50+
-->
51+
52+
<!-- Step 3. We need to create this site in Netlify
53+
54+
Open your terminal and run the following command:
55+
56+
```bash
57+
netlify init
58+
```
59+
60+
Choose manual deployments
61+
-->
62+
63+
<!-- Step 5. After configuring your redirect, its time to deploy
64+
65+
Open your terminal and run the following command:
66+
67+
```bash
68+
netlify deploy -p
69+
```
70+
-->
71+
72+
<!-- Step 6. Invoke the function to ensure it's working properly
73+
74+
Visit your site and the URL https://your-site-url.netlify.com/.netlify/functions/redirect
75+
76+
It should redirect you to a new location.
77+
78+
-->
79+
80+
81+
<!-- AUTO-GENERATED-CONTENT:START (README_BOTTOM) -->
82+
## Complete code
83+
84+
If you need help or get stuck refer to the completed code of this lesson
85+
86+
[View Complete Code](https://github.com/DavidWells/netlify-functions-workshop/tree/master/lessons-code-complete/use-cases/6-scraping)
87+
<!-- AUTO-GENERATED-CONTENT:END -->
88+
89+
90+
## Additional Resources
91+
92+
- [Serverless User Engagement with AWS Lambda, SendGrid, BadgeUp and Node.js](https://codeburst.io/serverless-user-engagement-with-aws-lambda-sendgrid-badgeup-and-node-js-53cdc4fa1ddd)
93+
- [Sendgrid Send HTML Email](https://github.com/BadgeUp/badgeup-serverless-email-demo/blob/a686df908d77713271142dbae285f322db670969/index.js)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const mailgunSdk = require('mailgun-js')
2+
const apiKey = process.env.MAILGUN_API_KEY
3+
const domain = `mail.${process.env.DOMAIN}`
4+
const mailgun = mailgunSdk({ apiKey, domain })
5+
6+
exports.handler = async (event, context, callback) => {
7+
const data = JSON.parse(event.body)
8+
9+
let response
10+
try {
11+
/* Send email to recicipent */
12+
response = await mailgun.messages().send({
13+
to: data.to,
14+
from: data.from,
15+
subject: data.subject,
16+
text: data.text,
17+
})
18+
} catch (e) {
19+
console.log('Err', e)
20+
return {
21+
statusCode: e.statusCode || 500,
22+
body: JSON.stringify({
23+
error: e.message
24+
})
25+
}
26+
}
27+
28+
return {
29+
statusCode: 200,
30+
body: JSON.stringify({
31+
result: response.message
32+
})
33+
}
34+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "send-email-via-mailgun",
3+
"version": "1.0.0",
4+
"description": "",
5+
"author": "David Wells",
6+
"license": "MIT",
7+
"dependencies": {
8+
"mailgun": "^0.5.0"
9+
}
10+
}

0 commit comments

Comments
 (0)