email

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Email

邮件

Val Town supports both directions: vals can be triggered by incoming mail (email-type vals) and can send mail via
std/email
.
Val Town 支持两个方向:val 可以由传入邮件触发(邮件类型的 val),也可以通过
std/email
发送邮件。

Receiving email — email-type vals

接收邮件——邮件类型的 val

Email vals (
fileType: "email"
) run when a message is delivered to the val's assigned address.
ts
// Learn more: https://docs.val.town/vals/email/
// Email type: {
//   from: string,
//   to: string[],
//   subject?: string,
//   text?: string,
//   html?: string,
//   attachments: File[],
//   headers: Record<string, string>
// }
export default async function (e: Email) {
  console.log(e.from, e.subject, e.text);
}
The file must have an
export
—
export default
for the handler.
Maximum 30MB per message, including attachments. Larger messages will be rejected.
邮件 val(
fileType: "email"
)会在消息投递到为该 val 分配的地址时运行。
ts
// Learn more: https://docs.val.town/vals/email/
// Email type: {
//   from: string,
//   to: string[],
//   subject?: string,
//   text?: string,
//   html?: string,
//   attachments: File[],
//   headers: Record<string, string>
// }
export default async function (e: Email) {
  console.log(e.from, e.subject, e.text);
}
文件必须包含
export
——处理程序需要使用
export default
。
每条消息最大 30MB,其中包括附件。更大的消息将被拒绝。

Reading the assigned address

读取分配的地址

When you list files or create an email-type file, the response includes
links.email
— the address that triggers this val. Always read this from the API response. Never construct an email address yourself — the format is owned by the platform and may change.
列出文件或创建邮件类型的文件时,响应中会包含
links.email
——这是触发该 val 的地址。始终从 API 响应中读取此地址。绝不自行构造电子邮件地址——其格式归平台所有,并且可能发生变化。

Sending email —
std/email

发送邮件——
std/email

For outgoing mail, import from
std/email
:
ts
import { email } from "https://esm.town/v/std/email";

await email({
  to: "user@example.com",
  subject: "Hello",
  text: "Message body",
});
std/email
exports
email
as the send function itself — call it directly (
email({ ... })
); there is no
email.send
method. It accepts the shape you'd expect:
to
,
subject
,
text
,
html
, plus
from
,
cc
,
bcc
,
replyTo
,
attachments
, and
headers
. If no
to
field is specified, it defaults to sending mail to the val owner's address.
要发送邮件,请从
std/email
导入:
ts
import { email } from "https://esm.town/v/std/email";

await email({
  to: "user@example.com",
  subject: "Hello",
  text: "Message body",
});
std/email
直接导出作为发送函数的
email
——直接调用它(
email({ ... })
);它没有
email.send
方法。它接受符合预期的结构:
to
、
subject
、
text
、
html
,以及
from
、
cc
、
bcc
、
replyTo
、
attachments
和
headers
。如果未指定
to
字段,则默认向 val 所有者的地址发送邮件。

Replying to an incoming message

回复传入消息

Combine the two — read the inbound
from
in an email-type handler, then call
email
to reply:
ts
import { email } from "https://esm.town/v/std/email";

export default async function (e: Email) {
  await email({
    to: e.from,
    subject: `Re: ${e.subject ?? ""}`,
    text: "Got it, thanks!",
  });
}
将两者结合使用——在邮件类型的处理程序中读取传入消息的
from
,然后调用
email
进行回复:
ts
import { email } from "https://esm.town/v/std/email";

export default async function (e: Email) {
  await email({
    to: e.from,
    subject: `Re: ${e.subject ?? ""}`,
    text: "Got it, thanks!",
  });
}

Verifying changes

验证变更

After editing an email-type val, use
run_file
with a sample
Email
payload to invoke the handler manually instead of waiting for a real incoming message. For send-only vals, run the script the same way and check
get_logs
for delivery errors.
编辑邮件类型的 val 后,使用
run_file
传入一个示例
Email
负载来手动调用处理程序,而不是等待真实的传入消息。对于仅用于发送的 val,以相同方式运行脚本,并通过
get_logs
检查投递错误。