Move away from sendgrid

This commit is contained in:
Luke Vella 2022-04-13 14:55:29 +01:00
parent e669c0dc11
commit f48658a5d8
12 changed files with 87 additions and 947 deletions

View file

@ -19,6 +19,20 @@ git clone https://github.com/lukevella/Rallly.git
cd Rallly
```
_optional_: Configure your SMTP server. Without this, Rallly won't be able to send out emails. You can set the following environment variables in a `.env` in the root of the project.
```
# /.env
# This will appear as the FROM email address
SUPPORT_EMAIL=""
# SMTP Server Details
SMTP_HOST=""
SMTP_PORT=""
SMTP_SECURE="" # Enable TLS - "true" or "false" (default: "false")
SMTP_USER=""
SMTP_PWD=""
```
Build and run with `docker-compose`
```bash
@ -39,14 +53,23 @@ cd Rallly
Copy the sample `.env` file then open it and set the variables.
```bash
cp sample.env .env
cp rallly-conf.env .env
```
You will need to supply a url to an empty postgres database.
```
# /.env
DATABASE_URL='postgresql://user:password@hostname/dbname'
# Postgres connection URL
DATABASE_URL=""
# This will appear as the FROM email address
SUPPORT_EMAIL=""
# SMTP Server Details
SMTP_HOST=""
SMTP_PORT=""
SMTP_SECURE="" # Enable TLS - "true" or "false" (default: "false")
SMTP_USER=""
SMTP_PWD=""
```
Install dependencies

View file

@ -14,11 +14,15 @@ services:
args:
- DATABASE_URL=postgres://postgres:postgres@rallly_db:5432/db?pgbouncer=true
restart: always
command: sh -c "DATABASE_URL=postgres://postgres:postgres@rallly_db:5432/db yarn prisma migrate deploy && yarn start"
command: sh -c "yarn prisma migrate deploy && yarn start"
depends_on:
- rallly_db
ports:
- 3000:3000
environment:
- DATABASE_URL=postgres://postgres:postgres@rallly_db:5432/db
env_file:
- .env
volumes:
db-data:

7
environment.d.ts vendored
View file

@ -4,11 +4,16 @@ declare global {
DATABASE_URL: string;
NODE_ENV: "development" | "production";
JWT_SECRET: string;
SENDGRID_API_KEY?: string;
MAINTENANCE_MODE?: "true";
PLAUSIBLE_DOMAIN?: string;
NEXT_PUBLIC_CRISP_WEBSITE_ID?: string;
LEGACY_MONGODB_URI?: string;
SUPPORT_EMAIL: string;
SMTP_HOST: string;
SMTP_USER: string;
SMTP_PWD: string;
SMTP_SECURE: string;
SMTP_PORT: string;
}
}
}

View file

@ -1,4 +0,0 @@
declare module "nodemailer-sendgrid-transport" {
function sgTransport(options: { auth: { api_key: string } }): SMTPTransport;
export default sgTransport;
}

View file

@ -35,7 +35,6 @@
"next-i18next": "^10.5.0",
"next-plausible": "^3.1.9",
"nodemailer": "^6.7.2",
"nodemailer-sendgrid-transport": "^0.2.0",
"prisma": "^3.11.0",
"react": "17.0.2",
"react-big-calendar": "^0.38.9",

View file

@ -61,7 +61,7 @@ export default withLink(async (req, res, link) => {
author: newComment.authorName,
pollUrl,
homePageUrl: absoluteUrl(req).origin,
supportEmail: process.env.NEXT_PUBLIC_SUPPORT_EMAIL,
supportEmail: process.env.SUPPORT_EMAIL,
unsubscribeUrl,
},
});

View file

@ -60,7 +60,7 @@ export default withLink(async (req, res, link) => {
participantName: participant.name,
pollUrl,
homePageUrl: absoluteUrl(req).origin,
supportEmail: process.env.NEXT_PUBLIC_SUPPORT_EMAIL,
supportEmail: process.env.SUPPORT_EMAIL,
unsubscribeUrl,
},
});

View file

@ -38,7 +38,7 @@ export default withLink(async (req, res, link) => {
pollUrl,
verifyEmailUrl,
homePageUrl,
supportEmail: process.env.NEXT_PUBLIC_SUPPORT_EMAIL,
supportEmail: process.env.SUPPORT_EMAIL,
},
});

View file

@ -74,7 +74,7 @@ export default async function handler(
pollUrl,
verifyEmailUrl,
homePageUrl,
supportEmail: process.env.NEXT_PUBLIC_SUPPORT_EMAIL,
supportEmail: process.env.SUPPORT_EMAIL,
},
});
} catch (e) {

View file

@ -1,4 +1,8 @@
# required
DATABASE_URL=""
# optional
SENDGRID_API_KEY=""
# postgres database - not needed if running with docker-compose
DATABASE_URL=replace-me
# smtp server - required if you want to receive emails
SMTP_HOST=replace-me
SMTP_PORT=replace-me
SMTP_SECURE=replace-me # TLS Enabled - "true" or "false" (default: "false")
SMTP_USER=replace-me
SMTP_PWD=replace-me

View file

@ -1,5 +1,4 @@
import nodemailer from "nodemailer";
import sgTransport from "nodemailer-sendgrid-transport";
interface SendEmailParameters {
to: string;
@ -7,35 +6,34 @@ interface SendEmailParameters {
html: string;
}
let transport: nodemailer.Transporter;
const getTransport = async () => {
if (process.env.SENDGRID_API_KEY) {
const sgTransportParams = sgTransport({
auth: { api_key: process.env.SENDGRID_API_KEY },
});
return nodemailer.createTransport(sgTransportParams);
} else {
const auth = await nodemailer.createTestAccount();
console.log(
`Email sent using ethereal.email account:\n${auth.user}\n${auth.pass}`,
);
return nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false,
// In development we use https://ethereal.email
auth,
if (!transport) {
transport = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT),
secure: process.env.SMTP_SECURE === "true",
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PWD,
},
});
}
return transport;
};
export const sendEmail = async (params: SendEmailParameters) => {
const transport = await getTransport();
return await transport.sendMail({
to: params.to,
from: `Rallly ${process.env.NEXT_PUBLIC_SUPPORT_EMAIL}`,
subject: params.subject,
html: params.html,
});
try {
await transport.verify();
return await transport.sendMail({
to: params.to,
from: `Rallly ${process.env.SUPPORT_EMAIL}`,
subject: params.subject,
html: params.html,
});
} catch {
console.log("SMTP server details are not configured or are incorrect");
}
};

921
yarn.lock

File diff suppressed because it is too large Load diff