Skip to content

Commit

Permalink
examples: init transport samples, send testing mail sample
Browse files Browse the repository at this point in the history
  • Loading branch information
narekhovhannisyan committed Sep 9, 2024
1 parent 49e099d commit eed53c6
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
60 changes: 60 additions & 0 deletions examples/bulk/transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { readFileSync } from "fs";
import Nodemailer from "nodemailer";
import { MailtrapTransport } from "mailtrap"

/**
* For this example, you need to have ready-to-use sending domain or,
* a Demo domain that allows sending emails to your own account email.
* @see https://help.mailtrap.io/article/69-sending-domain-setup
*/

const TOKEN = "<YOUR-TOKEN-HERE>"
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";

const transport = Nodemailer.createTransport(MailtrapTransport({
token: TOKEN,
bulk: true
}))

transport.sendMail({
text: "Welcome to Mailtrap Sending!",
to: {
address: RECIPIENT_EMAIL,
name: "John Doe"
},
from: {
address: SENDER_EMAIL,
name: "Mailtrap Test"
},
subject: "Hello from Mailtrap!",
html: `
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body style="font-family: sans-serif;">
<div style="display: block; margin: auto; max-width: 600px;" class="main">
<h1 style="font-size: 18px; font-weight: bold; margin-top: 20px">Congrats for sending test email with Mailtrap!</h1>
<p>Inspect it using the tabs you see above and learn how this email can be improved.</p>
<img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;">
<p>Now send your email using our fake SMTP server and integration of your choice!</p>
<p>Good luck! Hope it works.</p>
</div>
<!-- Example of invalid for email html/css, will be detected by Mailtrap: -->
<style>
.main { background-color: white; }
a:hover { border-left-width: 1em; min-height: 2em; }
</style>
</body>
</html>
`,
attachments: [
{
filename: "welcome.png",
content: readFileSync("./welcome.png"),
},
],
}).then(console.log)
.catch(console.error)
25 changes: 25 additions & 0 deletions examples/testing/send-mail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MailtrapClient } from "mailtrap"

/**
* For this example, you need to have ready-to-use sending domain or,
* a Demo domain that allows sending emails to your own account email.
* @see https://help.mailtrap.io/article/69-sending-domain-setup
*/

", @see https://help.mailtrap.io/article/69-sending-domain-setup#Demo-Domain--oYOU5"

const TOKEN = "<YOUR-TOKEN-HERE>";
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>"
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";

const client = new MailtrapClient({ token: TOKEN, sandbox: true, testInboxId: TEST_INBOX_ID });

client.send({
from: { name: "Mailtrap Test", email: SENDER_EMAIL },
to: [{ email: RECIPIENT_EMAIL }],
subject: "Hello from Mailtrap!",
text: "Welcome to Mailtrap Sending!",
})
.then(console.log)
.catch(console.error);
62 changes: 62 additions & 0 deletions examples/testing/transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { readFileSync } from "fs";
import Nodemailer from "nodemailer";
import { MailtrapTransport } from "mailtrap"

/**
* For this example, you need to have ready-to-use sending domain or,
* a Demo domain that allows sending emails to your own account email.
* @see https://help.mailtrap.io/article/69-sending-domain-setup
*/

const TOKEN = "<YOUR-TOKEN-HERE>"
const TEST_INBOX_ID = "<YOUR-TEST-INBOX-ID-HERE>"
const SENDER_EMAIL = "<SENDER@YOURDOMAIN.COM>";
const RECIPIENT_EMAIL = "<RECIPIENT@EMAIL.COM>";

const transport = Nodemailer.createTransport(MailtrapTransport({
token: TOKEN,
testInboxId: TEST_INBOX_ID
sandbox: true
}))

transport.sendMail({
text: "Welcome to Mailtrap Sending!",
to: {
address: RECIPIENT_EMAIL,
name: "John Doe"
},
from: {
address: SENDER_EMAIL,
name: "Mailtrap Test"
},
subject: "Hello from Mailtrap!",
html: `
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body style="font-family: sans-serif;">
<div style="display: block; margin: auto; max-width: 600px;" class="main">
<h1 style="font-size: 18px; font-weight: bold; margin-top: 20px">Congrats for sending test email with Mailtrap!</h1>
<p>Inspect it using the tabs you see above and learn how this email can be improved.</p>
<img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;">
<p>Now send your email using our fake SMTP server and integration of your choice!</p>
<p>Good luck! Hope it works.</p>
</div>
<!-- Example of invalid for email html/css, will be detected by Mailtrap: -->
<style>
.main { background-color: white; }
a:hover { border-left-width: 1em; min-height: 2em; }
</style>
</body>
</html>
`,
attachments: [
{
filename: "welcome.png",
content: readFileSync("./welcome.png"),
},
],
}).then(console.log)
.catch(console.error)

0 comments on commit eed53c6

Please sign in to comment.