Avatar

drew_kuhn

Joined November 18, 2024
Public vals
1
drew_kuhn avatar
drew_kuhn
inventiveVioletDinosaur
Script
With our data designed, let’s build the email! Val Town makes this super easy since it provides a robust email package. The package exports a function by the same name (email) which consumes an object. When called with properly formatted parameters, an email will be sent to the address associated with your Val Town account. “Good” emails need some kind of hook to draw the reader’s attention. What better hook than our joke’s setup? Add an email() function call to your Val which sends an email with the setup as its subject and the punchline as its body text. import { email } from "https://esm.town/v/std/email?v=9"; // Fetches a random joke. function fetchRandomJoke() { const SAMPLE_JOKE = { "setup": "What do you call a group of disorganized cats?", "punchline": "A cat-tastrophe.", }; return SAMPLE_JOKE; } const randomJoke = fetchRandomJoke(); const setup = randomJoke.setup; const punchline = randomJoke.punchline; // Sends an email with the joke. export const emailRandomJoke = email({ text: punchline, subject: setup, }); Notice that this data is being passed in as key-value pairs since it is inside of an object. There is only one parameter (that object) passed into the function. Save and evaluate your val by clicking the “Run” button at the top right.
0
Next