ホームページ >ウェブフロントエンド >jsチュートリアル >ResEndを使用してReactを使用して電子メールを送信する方法
この記事では、React電子メールと再送信を使用して、電子メール機能をReactアプリケーションにシームレスに統合する方法を示しています。 実際の例として、next.jsアプリケーション内に連絡先フォームを構築します。 以前は、Reactの電子メール統合は悪名高い挑戦でしたが、これらのツールはプロセスを大幅に単純化しました。
重要な利点:
関数は、電子メールの送信ロジックを追加する場所です。
(注:スタイリングとフォームビルディングは簡潔に省略されています。テールウィンドCSSまたは標準CSSはスタイリングに使用できます。onSubmit
resendの構成:
<code class="language-javascript">function onSubmit(values: z.infer<typeof formschema="">) { // Email sending logic will be implemented here console.log(values); }</typeof></code>
APIキーを取得する:
ログインまたは再送信アカウントを作成します。 ダッシュボードから、新しいAPIキーを生成します。
.env.local
<code>RESEND_API_KEY=your_api_key_here</code>
から必要なコンポーネントをインポートします
Email.tsx
電子メールコンポーネントを作成します:
@react-email/components
resEnd(api/send/route.ts
)でメールを送信:
<code class="language-javascript">function onSubmit(values: z.infer<typeof formschema="">) { // Email sending logic will be implemented here console.log(values); }</typeof></code>
<code>RESEND_API_KEY=your_api_key_here</code>
<code class="language-javascript">import { Body, Container, Head, Heading, Hr, Html, Preview, Tailwind, Text } from "@react-email/components"; import * as React from "react";</code>
<code class="language-typescript">interface ContactMeEmailProps { name: string; emailAddress: string; phoneNumber: string; content: string; }</code>
onSubmit
関数を更新して、/api/send
:<code class="language-javascript">const VercelInviteUserEmail = ({ name, content, emailAddress, phoneNumber }: ContactMeEmailProps) => ( <preview>A message from {name}</preview> <tailwind> <container classname="border border-solid border-[#eaeaea] rounded my-[40px] mx-auto p-[20px] w-[465px]"> <heading classname="text-black text-[24px] font-normal text-center p-0 my-[30px] mx-0"> <strong>{name}</strong> would like to contact you! </heading> <text classname="text-black text-[14px] leading-[24px]"> Here is the message: {content} </text> <hr classname="border border-solid border-[#eaeaea] my-[26px] mx-0 w-full"> <text classname="text-[#666666] text-[12px] leading-[24px]"> This message was sent by {name}. Contact them at {emailAddress} or {phoneNumber} </text> </container> </tailwind> );</code>
これにより、電子メールの送信機能が記入されます。プレースホルダーのメールアドレスを実際のメールアドレスに置き換え、潜在的なエラーを適切に処理することを忘れないでください。 完全なソースコードはGitHub(元のテキストで提供されているリンク)にあります。
以上がResEndを使用してReactを使用して電子メールを送信する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。