Skip to content

Commit

Permalink
fix: add test credentials to newsletter
Browse files Browse the repository at this point in the history
Signed-off-by: Mirko Mollik <[email protected]>
  • Loading branch information
cre8 committed Oct 7, 2024
1 parent 7ab7623 commit ad26202
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/newsletter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
EMAIL_ADDRESS: ${{ secrets.EMAIL_ADDRESS }}
EMAIL_PASSWORD: ${{ secrets.EMAIL_PASSWORD }}
EMAIL_RECIPIENT: ${{ github.event.inputs.email }}
EMAIL_RECIPIENT: ${{ github.event.inputs.email }}
NODE_ENV: ${{ vars.NODE_ENV }}
EMAIL_STORE: true

- name: Upload newsletter artifact
uses: actions/upload-artifact@v4
with:
name: newsletter
path: viewer/newsletter.html
2 changes: 2 additions & 0 deletions viewer/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
EMAIL_ADDRESS=
EMAIL_PASSWORD=
LOCATION='http://localhost:4200/#'
EMAIL_STORE=true
NODE_ENV=development
1 change: 1 addition & 0 deletions viewer/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

.env
newsletter.html

# Compiled output
/dist
Expand Down
42 changes: 32 additions & 10 deletions viewer/scripts/newsletter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {config} from 'dotenv';
import fs from 'fs';
import handlebars from 'handlebars';
import { execSync } from 'child_process';
import {createTransport} from 'nodemailer'
import {createTransport, createTestAccount, getTestMessageUrl} from 'nodemailer'

config();

Expand Down Expand Up @@ -61,14 +61,33 @@ const preHeader = `We got ${caseStudies.length} new case ${caseStudies.length ==
const template = handlebars.compile(templateSource);
const html = template({ caseStudies, lastMonth: currentMonthName, subject, preHeader, walletCounter });

// Create a transporter
let transporter = createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_ADDRESS,
pass: process.env.EMAIL_PASSWORD
if(process.env.EMAIL_STORE){
fs.writeFileSync(`./newsletter.html`, html);
}


function getMailConfig() {
if(process.env.NODE_ENV === 'production'){
return {
service: 'gmail',
auth: {
user: process.env.EMAIL_ADDRESS,
pass: process.env.EMAIL_PASSWORD
}
}
});
}
else {
return createTestAccount().then((account) => ({
host: account.smtp.host,
port: account.smtp.port,
secure: account.smtp.secure,
auth: {
user: account.user,
pass: account.pass,
}
}))
}
}

// Email options
let mailOptions = {
Expand All @@ -79,9 +98,12 @@ let mailOptions = {
};

// Send the email
transporter.sendMail(mailOptions, (error, info) => {
getMailConfig().then(config => createTransport(config).sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Email sent: ' + info.response);
});
if(process.env.NODE_ENV !== 'production') {
console.log('Preview URL: %s', getTestMessageUrl(info));
}
}));

0 comments on commit ad26202

Please sign in to comment.