Send emails with std/email
. You can only send emails to yourself if you’re on Val Town Free. If you’re on Val Town Pro, you can email anyone.
Basic usage
Section titled “Basic usage”import { email } from "https://esm.town/v/std/email";
await email({ subject: "Hello from Val Town", text: "This is a test email sent from a Val Town val.",});
subject
Section titled “subject”The email subject line. It defaults to Message from @your_username on Val Town
.
to
, cc
, and bcc
Section titled “to, cc, and bcc”By default, the to
field is set to the owner of the Val Town account that calls it.
If you have Val Town Pro, you can send emails to anyone via the to
, cc
, and bcc
fields.
If you don’t have Val Town Pro, you can only send emails to yourself, so leave those fields blank.
The from
is limited to a few options:
-
It defaults to
notifications@val.town
if you don’t specify it. -
If you do specify it, it must be of the form:
{ email: "your_username.valname@valtown.email }"
or{ email: "your_username.valname@valtown.email", name: "Sender Name" }
.
- We will soon allow you to email from any val email address you own, but in the meantime you can get around these limitations by using the
replyTo
field.
replyTo
Section titled “replyTo”replyTo
accepts a string email or an object with strings for email
and name
(optional).
This can be useful if you are sending emails to others with Val Town Pro.
import { email } from "https://esm.town/v/std/email";
await email({ to: "someone_else@example.com", replyTo: "your_custom_email@valtown.email", text: "these pretzels are making me thirsty",});
Attachments
Section titled “Attachments”You can attach files to your emails by using the attachments
field.
Attachments need to be Base64 encoded,
which is what the btoa
method is doing in this example:
import { email } from "https://esm.town/v/std/email";
export const stdEmailAttachmentExample = email({ attachments: [ { content: btoa("hello attachments!"), filename: "test.txt", type: "text", disposition: "attachment", }, ],});
Attachment examples
Section titled “Attachment examples”- Sending a PDF created in code
- Receiving an attachment and then sending it along.
- Receiving an attachment, sending it to OpenAI, and then emailing it.
Headers
Section titled “Headers”You can set custom headers in emails that you send:
import { email } from "https://esm.town/v/std/email";
console.log( await email({ text: "Hi", headers: { "X-Custom-Header": "xxx", }, }));
This is also documented in our REST API, and supported in the SDK.
Limitations
Section titled “Limitations”std/email
is powered internally by Sendgrid.