技能 编程开发 配置SMTP邮件发送设置

配置SMTP邮件发送设置

v20260825
webiny-mailer-smtp
本技能指导如何在Webiny项目中配置SMTP邮件发送设置。通过在`webiny.config.tsx`中使用`<Api.Mailer.Smtp />`组件,可以程序化设置邮件服务的SMTP主机、端口和认证凭证。强调必须使用环境变量安全管理密码,并在配置后执行API构建命令。
获取技能
439 次下载
概览

Mailer SMTP Configuration

TL;DR

Add <Api.Mailer.Smtp ... /> to your webiny.config.tsx to configure SMTP email delivery at build time. The component serializes the settings into the build artifact — always pass the password via an environment variable, never hard-code it. Deploy the API after making changes.

Core Pattern

import { Api } from "webiny/extensions";

<Api.Mailer.Smtp
    host={"smtp.example.com"}
    port={587}
    user={"smtp-user"}
    password={process.env.SMTP_PASSWORD!}
    from={"noreply@example.com"}
    replyTo={"support@example.com"}   {/* optional */}
/>

Place this inside the JSX returned by your config component in webiny.config.tsx, alongside other <Api.*> extensions.

Props Reference

PropTypeRequiredDescription
hoststringYesSMTP server hostname
portnumberYesSMTP server port (positive integer)
userstringYesSMTP authentication username
passwordstringYesSMTP authentication password — use env var
fromstringYesDefault sender address
replyTostringNoDefault reply-to address

from and replyTo are validated by the email-addresses package against RFC 5322.

Full Example (webiny.config.tsx)

import React from "react";
import { Api, Admin, Core } from "webiny/extensions";

const MyConfig = () => {
  return (
    <>
      {/* ... other extensions ... */}

      {/* Mailer: configure SMTP transport via code */}
      <Api.Mailer.Smtp
        host={"smtp.sendgrid.net"}
        port={587}
        user={"apikey"}
        password={process.env.SENDGRID_API_KEY!}
        from={"noreply@acme.com"}
        replyTo={"support@acme.com"}
      />
    </>
  );
};

Add SENDGRID_API_KEY=your-secret to your .env file (never commit it).

Common Mistakes

MistakeFix
Hard-coding the passwordUse process.env.SMTP_PASSWORD!
process.env.SMTP_PASSWORD undefined at build time → "expected string, received object" errorEnsure the env var is set before deploying
from/replyTo value fails validationMust be a valid RFC 5322 mailbox address
Settings not taking effectRun yarn webiny build api after editing webiny.config.tsx

Quick Reference

# After editing webiny.config.tsx, build the API
yarn webiny build api
Note: This command only works in a dev environment. For other environments, run yarn webiny build api --env=YOUR_ENV instead.
// Environment variable in .env
SMTP_PASSWORD = your - smtp - password - here;

Related Skills

  • webiny-project-structure — Where webiny.config.tsx lives and how extensions are structured
  • webiny-local-development — Running and deploying locally
信息
分类 编程开发
名称 webiny-mailer-smtp
版本 v20260825
大小 3.88KB
更新时间 2026-09-06
语言