Created
December 6, 2021 12:12
-
-
Save meerzulee/9fe282d2f05bcc9b4f00970d0729ad14 to your computer and use it in GitHub Desktop.
Directus v9 - Send email hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hooks/create-report/templates/base.liquid | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<meta name="viewport" content="width=device-width"> | |
<meta name="format-detection" content="telephone=no"> | |
<title>{{ projectName }} Email Service</title> | |
<style type="text/css"> | |
a { | |
display: inline-block; | |
height: 52px; | |
width: auto; | |
min-width: 154px; | |
padding: 0 20px; | |
font-size: 16px; | |
font-weight: bold; | |
line-height: 52px; | |
cursor: pointer; | |
border-radius: 4px; | |
text-decoration: none !important; | |
color: white !important; | |
background-color: {{ projectColor }}; | |
} | |
a:hover { | |
filter: brightness(105%); | |
} | |
p { | |
margin: 20px 0 20px 0; | |
} | |
</style> | |
</head> | |
<body | |
style="margin: 0; padding: 20px; background-color: #eceff1; -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; -ms-text-size-adjust: none; font-weight: 400; font-size: 14px; color: #546e7a; font-family: 'Roboto', Helvetica, Helvetica, Arial, sans-serif; letter-spacing: 0;"> | |
<table border="0" cellpadding="0" cellspacing="0" width="100%"> | |
<tr> | |
<td style="padding: 0;"> | |
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" | |
style="border: 0 solid #263238; border-collapse: collapse;"> | |
<tr> | |
<td align="center" bgcolor="{{ projectColor }}" | |
style="padding: 30px 0 30px 0; border-radius: 4px 4px 0 0;"> | |
<img src="{{ projectLogo }}" alt="{{ projectName }}" width="130" | |
style="display: block;" /> | |
</td> | |
</tr> | |
<tr> | |
<td bgcolor="#ffffff" style="padding: 30px; border-radius: 0 0 4px 4px;"> | |
<table border="0" cellpadding="0" cellspacing="0" width="100%"> | |
<tr> | |
<td | |
style="color: #546e7a; font-family: Helvetica, Arial, sans-serif; padding: 0; line-height: 1.5; font-size: 16px;"> | |
{% block content %}{{ html }}{% endblock %} | |
</td> | |
</tr> | |
</table> | |
</td> | |
</tr> | |
<tr> | |
<td bgcolor="#eceff1" style="padding: 20px 30px 0 32px;"> | |
<table border="0" cellpadding="0" cellspacing="0" width="100%"> | |
<tr> | |
<td style="color:#b0bec5; font-family:Helvetica, Arial, sans-serif; font-size:12px;" | |
width="75%"> | |
{% block footer %}{% endblock %} | |
</td> | |
</tr> | |
</table> | |
</td> | |
</tr> | |
</table> | |
</td> | |
</tr> | |
</table> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hooks/create-report/templates/create-report.liquid | |
{% layout "base" %} | |
{% block content %} | |
<p> | |
We have received a request to reset the password for your <i>{{ projectName }}</i> account. If you did not make this change, please contact one of your administrators. Otherwise, to complete the process, click the following link to confirm your email address and enter your new password. | |
</p> | |
<p style="text-align: center; padding: 20px 0;"> | |
<a href="{{ url }}"> | |
Click to reset your password | |
</a> | |
</p> | |
<p> | |
<b>Important: This link will expire in 24 hours.</b> | |
</p> | |
<p> | |
Thank you,<br> | |
{{ projectName }} | |
</p> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hooks/create-report/index.js | |
module.exports = ({ env, database }) => { | |
return { | |
"items.create": async () => { | |
const { Liquid } = require("liquidjs"); | |
const nodemailer = require("nodemailer"); | |
const path = require("path"); | |
const liquidEngine = new Liquid({ | |
root: path.resolve(__dirname, "templates"), | |
extname: ".liquid", | |
}); | |
const transporter = nodemailer.createTransport({ | |
pool: env.EMAIL_SMTP_POOL, | |
host: env.EMAIL_SMTP_HOST, | |
port: env.EMAIL_SMTP_PORT, | |
secure: env.EMAIL_SMTP_SECURE, | |
auth: { | |
user: env.EMAIL_SMTP_USER, | |
pass: env.EMAIL_SMTP_PASSWORD, | |
}, | |
}); | |
const projectInfo = await database | |
.select(["project_name", "project_logo", "project_color"]) | |
.from("directus_settings") | |
.first(); | |
const projectLogo = `${env.PUBLIC_URL}/assets/${projectInfo?.project_logo}`; | |
const html = await liquidEngine.renderFile("create-report", { | |
projectName: projectInfo?.project_name, | |
projectColor: projectInfo?.project_color, | |
email: "[email protected]", | |
url: projectLogo, | |
}); | |
transporter.sendMail({ | |
from: env.EMAIL_FROM, | |
to: "[email protected]", | |
html: html, | |
subject: `Test - Email`, | |
}); | |
}, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment