[nodemailer] Render component when sending email
Unanswered
Savannah posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
Hi guys, i would like to know if it's possible to render a component when an email is send ?
import nodemailer from 'nodemailer';
import Mail from 'nodemailer/lib/mailer';
import SMTPTransport from "nodemailer/lib/smtp-transport";
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.SERVER_MAIL_USER,
pass: process.env.SERVER_MAIL_PASS
}
} as SMTPTransport.Options);
type SendEmailOptions = {
sender: Mail.Address,
recipients: Mail.Address[],
subject: string,
message: string
}
export const sendMail = async ({sender, recipients, subject, message}: SendEmailOptions) => {
console.log('sender', sender, 'recipients', recipients)
try {
await transporter.sendMail({
from: '"Maddison Foo Koch :ghost:" <maddison53@ethereal.email>',
to: "test.fafa@gmail.com",
subject: "Hello ✔",
text: "Hello world?",
html: "<b>Hello world?</b>"
});
console.log('Message sent successfully')
} catch (error) {
console.error('error sending message', error)
}
}
38 Replies
![Avatar](https://cdn.discordapp.com/avatars/301376057326567425/788b75eb8862277af3d31d317ada783f.webp?size=256)
B33fb0n3
yes you can. You can return a specific response and if the response is successful, you can do stuff with the client. Either redirect him or conditionally render the component or ...
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
bro i will suggest you to use React Email if you want to render components in email
![Avatar](https://cdn.discordapp.com/avatars/301376057326567425/788b75eb8862277af3d31d317ada783f.webp?size=256)
B33fb0n3
it seems like he want to render a component after the mail is sent.
... to render a component when an email ...Not the component inside the email.
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
in this case just use the response if mail sent successfully then show the success and not then error simple
![Avatar](https://cdn.discordapp.com/avatars/301376057326567425/788b75eb8862277af3d31d317ada783f.webp?size=256)
B33fb0n3
that's what I said: https://nextjs-forum.com/post/1244633427265978471#message-1244649092114808912
Do you read his or my messages in detail? For me it seem's like you don't 🤔
Do you read his or my messages in detail? For me it seem's like you don't 🤔
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
sorry i forgot to read this message
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
Hey, thanks for the answer. But i'd like to render the component instead of
<b>Hello world?</b>
in the html variableI think that's what i'm looking for, thanks i'll check that asap 😉
![Avatar](https://cdn.discordapp.com/avatars/301376057326567425/788b75eb8862277af3d31d317ada783f.webp?size=256)
B33fb0n3
ah ok, yea. Then I read your message wrong
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
Don't worry, I may have misspoken as well. x)
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
It's working, thanks @B33fb0n3 and @Gharial ! Have a good day 🙂
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
May i ask you a question @Gharial please ?
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
Sure ask
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
Did you know why my className are not in the dom when i send an email ?
import {Button, Html} from "@react-email/components";
import "@/styles/emails/VerificationCodeTemplate.scss";
export function VerificationCodeTemplate() {
return (
<Html>
<div className="wrapper-template">
<div className="wrapper-header">
<Button href="https://www.google.com">Visiter le site</Button>
</div>
<div className="wrapper-texts">
<h1>Terminer l'inscription</h1>
<h2>Entrez le code de vérification suivant pour terminer votre inscription</h2>
</div>
<div className="wrapper-code">
<span>450994</span>
</div>
<p>Si vous n'êtes pas à l'origine de cette demande, veuillez ignorer ce message</p>
</div>
</Html>
)
}
![Image](https://cdn.discordapp.com/attachments/1244633427265978471/1244685238810972261/image.png?ex=665602f6&is=6654b176&hm=91864e68c6ecd6fde51f87e4d726115e8bd63ddc194d3826ea43ff3ea6bc8b61&)
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
Can I know what is your file name
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
Which one ? For the style or the component ?
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
With extension use .jsx and tsx if typescript
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
![Image](https://cdn.discordapp.com/attachments/1244633427265978471/1244685549760151674/image.png?ex=66560340&is=6654b1c0&hm=0cd620edc27804163afd4a88ae04c319a8b301098ff015b6dd1c129fc3547c09&)
my extensions is tsx
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
How you are rendering on nodemailer
Can I see?
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
Like this
import nodemailer from 'nodemailer';
import Mail from 'nodemailer/lib/mailer';
import SMTPTransport from "nodemailer/lib/smtp-transport";
import {render} from "@react-email/render";
import {VerificationCodeTemplate} from "@/app/components/emails/VerificationCodeTemplate";
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.SERVER_MAIL_USER,
pass: process.env.SERVER_MAIL_PASS
}
} as SMTPTransport.Options);
type SendEmailOptions = {
sender: Mail.Address,
recipients: Mail.Address[],
subject: string,
message: string
}
export const sendMail = async ({sender, recipients, subject, message}: SendEmailOptions) => {
try {
await transporter.sendMail({
from: `${sender.name} <${sender.address}>`,
to: recipients.map(recipient => `${recipient.name} <${recipient.address}>`).join(', '),
subject: subject,
html: render(VerificationCodeTemplate())
});
console.log('Message sent successfully')
} catch (error) {
console.error('error sending message', error)
}
}
I got the email
![Image](https://cdn.discordapp.com/attachments/1244633427265978471/1244685828844687442/image.png?ex=66560382&is=6654b202&hm=0de9145d96bb7e2285f02c65962ab0bd4271e6efa58651d3518c8d80a60ce35a&)
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
Sorry this will not support classnam
Use on documentations for this
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
Ohh, okay. And how can i custom it then ?
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
Use style tag or jsx style
You can use tailwind also but you need to check the docs
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
yeah, i see
thanks brother
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
I am not sure but className work I need to check
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
if you can find it, i'll take it 😉
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
bro you use tailwind like this - https://react.email/docs/components/tailwind
![Avatar](https://cdn.discordapp.com/embed/avatars/4.png)
SavannahOP
Yeah, i see it on the doc. But i'm not using Tailwind 😉
But i use the style tag
and it's working great
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Gharial
okay