1658973600
Comment envoyer un e-mail avec AWS SES ? Ici, nous envoyons un e-mail de confirmation d'inscription à un utilisateur.
import { getHTMLTemplate, fillHtmlTemplate, sendEmail } from "../email"
export const sendEmailConfirmationLink = async (
email: string,
authLink: string
) => {
const html = await getHTMLTemplate("auth")
await sendEmail({
email,
body: fillHtmlTemplate(html, {
"auth-link": authLink,
email,
}),
subject: "Log in to Increaser",
source: `Login <noreply@increaser.org>`,
})
}
Tout d'abord, nous voulons prendre un modèle HTML. La fonction prend le nom du modèle et le lit à partir du fichier. Je rends les noms de modèles identiques au HtmlTemplate
type.
export const getHTMLTemplate = memoize((template: HtmlTemplate) => {
return readFile(
path.resolve(__dirname, `../../templates/${template}.html`),
"utf8"
)
})
Je n'envoie généralement pas d'e-mails avec un style fantaisiste pour ne pas apparaître dans les spams ou les onglets de promotion des destinataires.
Dans le auth-email
modèle, nous attendons deux variables - auth-link et email.
<p>Click the link below to sign in to <b>Increaser</b>.</p>
<p>This link will expire in 20 minutes.</p>
<a href={{auth-link}}>Sign in to Increaser</a>
<p>Confirming this request will securely sign you in using {{email}}.</p>
- Increaser Team
Une fois que nous aurons lu le fichier dans une chaîne, nous utiliserons la fillHtmlTemplate
fonction pour insérer des variables dans un e-mail html.
export const fillHtmlTemplate = (
html: string,
variables: Record<string, string>
) => {
let result = html
Object.entries(variables).forEach(([key, value]) => {
result = result.split(`{{${key}}}`).join(value)
})
return result
}
Nous transmettons l'e-mail du destinataire, le corps HTML, l'objet et la source à la sendEmail
fonction.
J'ai exécuté mon Lambda dans la région européenne, mais AWS SES ne fonctionne pas là-bas. Je passe donc explicitement la région de Virginie au constructeur SES.
interface SendEmailParameters {
email: string
body: string
subject: string
source: string
}
const ses = new aws.SES({ region: "us-east-1" })
export const sendEmail = ({
email,
body,
subject,
source,
}: SendEmailParameters) =>
ses
.sendEmail({
Destination: {
ToAddresses: [email],
},
Message: {
Body: {
Html: {
Data: body,
},
},
Subject: {
Data: subject,
},
},
Source: source,
})
.promise()
Nous passons Destination
, Message
et Source
à la méthode SES et en faisons une promesse.
Source : https://radzion.com/blog/ses-send
1658973600
Comment envoyer un e-mail avec AWS SES ? Ici, nous envoyons un e-mail de confirmation d'inscription à un utilisateur.
import { getHTMLTemplate, fillHtmlTemplate, sendEmail } from "../email"
export const sendEmailConfirmationLink = async (
email: string,
authLink: string
) => {
const html = await getHTMLTemplate("auth")
await sendEmail({
email,
body: fillHtmlTemplate(html, {
"auth-link": authLink,
email,
}),
subject: "Log in to Increaser",
source: `Login <noreply@increaser.org>`,
})
}
Tout d'abord, nous voulons prendre un modèle HTML. La fonction prend le nom du modèle et le lit à partir du fichier. Je rends les noms de modèles identiques au HtmlTemplate
type.
export const getHTMLTemplate = memoize((template: HtmlTemplate) => {
return readFile(
path.resolve(__dirname, `../../templates/${template}.html`),
"utf8"
)
})
Je n'envoie généralement pas d'e-mails avec un style fantaisiste pour ne pas apparaître dans les spams ou les onglets de promotion des destinataires.
Dans le auth-email
modèle, nous attendons deux variables - auth-link et email.
<p>Click the link below to sign in to <b>Increaser</b>.</p>
<p>This link will expire in 20 minutes.</p>
<a href={{auth-link}}>Sign in to Increaser</a>
<p>Confirming this request will securely sign you in using {{email}}.</p>
- Increaser Team
Une fois que nous aurons lu le fichier dans une chaîne, nous utiliserons la fillHtmlTemplate
fonction pour insérer des variables dans un e-mail html.
export const fillHtmlTemplate = (
html: string,
variables: Record<string, string>
) => {
let result = html
Object.entries(variables).forEach(([key, value]) => {
result = result.split(`{{${key}}}`).join(value)
})
return result
}
Nous transmettons l'e-mail du destinataire, le corps HTML, l'objet et la source à la sendEmail
fonction.
J'ai exécuté mon Lambda dans la région européenne, mais AWS SES ne fonctionne pas là-bas. Je passe donc explicitement la région de Virginie au constructeur SES.
interface SendEmailParameters {
email: string
body: string
subject: string
source: string
}
const ses = new aws.SES({ region: "us-east-1" })
export const sendEmail = ({
email,
body,
subject,
source,
}: SendEmailParameters) =>
ses
.sendEmail({
Destination: {
ToAddresses: [email],
},
Message: {
Body: {
Html: {
Data: body,
},
},
Subject: {
Data: subject,
},
},
Source: source,
})
.promise()
Nous passons Destination
, Message
et Source
à la méthode SES et en faisons une promesse.
Source : https://radzion.com/blog/ses-send
1658973600
Como enviar um e-mail com o AWS SES? Aqui, enviamos um e-mail de confirmação de inscrição para um usuário.
import { getHTMLTemplate, fillHtmlTemplate, sendEmail } from "../email"
export const sendEmailConfirmationLink = async (
email: string,
authLink: string
) => {
const html = await getHTMLTemplate("auth")
await sendEmail({
email,
body: fillHtmlTemplate(html, {
"auth-link": authLink,
email,
}),
subject: "Log in to Increaser",
source: `Login <noreply@increaser.org>`,
})
}
Primeiro, queremos pegar um modelo HTML. A função pega o nome do modelo e o lê do arquivo. Eu faço nomes de modelo iguais ao HtmlTemplate
tipo.
export const getHTMLTemplate = memoize((template: HtmlTemplate) => {
return readFile(
path.resolve(__dirname, `../../templates/${template}.html`),
"utf8"
)
})
Não costumo enviar e-mails com um estilo sofisticado para não aparecer nas guias de spam ou promoção do destinatário.
No auth-email
modelo, esperamos duas variáveis - auth-link e email.
<p>Click the link below to sign in to <b>Increaser</b>.</p>
<p>This link will expire in 20 minutes.</p>
<a href={{auth-link}}>Sign in to Increaser</a>
<p>Confirming this request will securely sign you in using {{email}}.</p>
- Increaser Team
Depois de lermos o arquivo em uma string, usaremos a fillHtmlTemplate
função para inserir variáveis em um html de e-mail.
export const fillHtmlTemplate = (
html: string,
variables: Record<string, string>
) => {
let result = html
Object.entries(variables).forEach(([key, value]) => {
result = result.split(`{{${key}}}`).join(value)
})
return result
}
Passamos o email do destinatário, corpo HTML, assunto e fonte para a sendEmail
função.
Executei meu Lambda na região europeia, mas o AWS SES não funciona lá. Então, passo explicitamente a região da Virgínia para o construtor SES.
interface SendEmailParameters {
email: string
body: string
subject: string
source: string
}
const ses = new aws.SES({ region: "us-east-1" })
export const sendEmail = ({
email,
body,
subject,
source,
}: SendEmailParameters) =>
ses
.sendEmail({
Destination: {
ToAddresses: [email],
},
Message: {
Body: {
Html: {
Data: body,
},
},
Subject: {
Data: subject,
},
},
Source: source,
})
.promise()
Passamos Destination
, Message
e Source
para o método SES e o transformamos em uma promessa.
1658934720
How to send an email with AWS SES? Here we send a signup confirmation email to a user.
See more at: https://radzion.com/blog/ses-send
1658977380
Làm cách nào để gửi email với AWS SES? Tại đây, chúng tôi gửi một email xác nhận đăng ký đến người dùng.
import { getHTMLTemplate, fillHtmlTemplate, sendEmail } from "../email"
export const sendEmailConfirmationLink = async (
email: string,
authLink: string
) => {
const html = await getHTMLTemplate("auth")
await sendEmail({
email,
body: fillHtmlTemplate(html, {
"auth-link": authLink,
email,
}),
subject: "Log in to Increaser",
source: `Login <noreply@increaser.org>`,
})
}
Đầu tiên, chúng tôi muốn lấy một mẫu HTML. Hàm lấy tên mẫu và đọc nó từ tệp. Tôi đặt tên mẫu giống như HtmlTemplate
kiểu.
export const getHTMLTemplate = memoize((template: HtmlTemplate) => {
return readFile(
path.resolve(__dirname, `../../templates/${template}.html`),
"utf8"
)
})
Tôi không thường gửi email có kiểu dáng lạ mắt để không xuất hiện trong các tab thư rác hoặc quảng cáo của người nhận.
Trong auth-email
mẫu, chúng tôi mong đợi hai biến - liên kết auth và email.
<p>Click the link below to sign in to <b>Increaser</b>.</p>
<p>This link will expire in 20 minutes.</p>
<a href={{auth-link}}>Sign in to Increaser</a>
<p>Confirming this request will securely sign you in using {{email}}.</p>
- Increaser Team
Khi chúng tôi đã đọc tệp thành một chuỗi, chúng tôi sẽ sử dụng fillHtmlTemplate
hàm để chèn các biến vào một email html.
export const fillHtmlTemplate = (
html: string,
variables: Record<string, string>
) => {
let result = html
Object.entries(variables).forEach(([key, value]) => {
result = result.split(`{{${key}}}`).join(value)
})
return result
}
Chúng tôi chuyển email người nhận, nội dung HTML, chủ đề và nguồn cho sendEmail
hàm.
Tôi đã chạy Lambda của mình ở khu vực Châu Âu, nhưng AWS SES không hoạt động ở đó. Vì vậy, tôi chuyển vùng Virginia một cách rõ ràng cho hàm tạo SES.
interface SendEmailParameters {
email: string
body: string
subject: string
source: string
}
const ses = new aws.SES({ region: "us-east-1" })
export const sendEmail = ({
email,
body,
subject,
source,
}: SendEmailParameters) =>
ses
.sendEmail({
Destination: {
ToAddresses: [email],
},
Message: {
Body: {
Html: {
Data: body,
},
},
Subject: {
Data: subject,
},
},
Source: source,
})
.promise()
Chúng tôi vượt qua Destination
, Message
và Source
đến phương pháp SES và biến nó thành một lời hứa.
1654588030
TypeScript Deep Dive
I've been looking at the issues that turn up commonly when people start using TypeScript. This is based on the lessons from Stack Overflow / DefinitelyTyped and general engagement with the TypeScript community. You can follow for updates and don't forget to ★ on GitHub 🌹
If you are here to read the book online get started.
Book is completely free so you can copy paste whatever you want without requiring permission. If you have a translation you want me to link here. Send a PR.
You can also download one of the Epub, Mobi, or PDF formats from the actions tab by clicking on the latest build run. You will find the files in the artifacts section.
All the amazing contributors 🌹
Share URL: https://basarat.gitbook.io/typescript/
Author: Basarat
Source Code: https://github.com/basarat/typescript-book/
License: View license