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 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` Build and run with `docker-compose`
```bash ```bash
@ -39,14 +53,23 @@ cd Rallly
Copy the sample `.env` file then open it and set the variables. Copy the sample `.env` file then open it and set the variables.
```bash ```bash
cp sample.env .env cp rallly-conf.env .env
``` ```
You will need to supply a url to an empty postgres database. You will need to supply a url to an empty postgres database.
``` ```
# /.env # /.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 Install dependencies

View file

@ -14,11 +14,15 @@ services:
args: args:
- DATABASE_URL=postgres://postgres:postgres@rallly_db:5432/db?pgbouncer=true - DATABASE_URL=postgres://postgres:postgres@rallly_db:5432/db?pgbouncer=true
restart: always 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: depends_on:
- rallly_db - rallly_db
ports: ports:
- 3000:3000 - 3000:3000
environment:
- DATABASE_URL=postgres://postgres:postgres@rallly_db:5432/db
env_file:
- .env
volumes: volumes:
db-data: db-data:

7
environment.d.ts vendored
View file

@ -4,11 +4,16 @@ declare global {
DATABASE_URL: string; DATABASE_URL: string;
NODE_ENV: "development" | "production"; NODE_ENV: "development" | "production";
JWT_SECRET: string; JWT_SECRET: string;
SENDGRID_API_KEY?: string;
MAINTENANCE_MODE?: "true"; MAINTENANCE_MODE?: "true";
PLAUSIBLE_DOMAIN?: string; PLAUSIBLE_DOMAIN?: string;
NEXT_PUBLIC_CRISP_WEBSITE_ID?: string; NEXT_PUBLIC_CRISP_WEBSITE_ID?: string;
LEGACY_MONGODB_URI?: 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-i18next": "^10.5.0",
"next-plausible": "^3.1.9", "next-plausible": "^3.1.9",
"nodemailer": "^6.7.2", "nodemailer": "^6.7.2",
"nodemailer-sendgrid-transport": "^0.2.0",
"prisma": "^3.11.0", "prisma": "^3.11.0",
"react": "17.0.2", "react": "17.0.2",
"react-big-calendar": "^0.38.9", "react-big-calendar": "^0.38.9",

View file

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

View file

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

View file

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

View file

@ -1,4 +1,8 @@
# required # postgres database - not needed if running with docker-compose
DATABASE_URL="" DATABASE_URL=replace-me
# optional # smtp server - required if you want to receive emails
SENDGRID_API_KEY="" 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 nodemailer from "nodemailer";
import sgTransport from "nodemailer-sendgrid-transport";
interface SendEmailParameters { interface SendEmailParameters {
to: string; to: string;
@ -7,35 +6,34 @@ interface SendEmailParameters {
html: string; html: string;
} }
let transport: nodemailer.Transporter;
const getTransport = async () => { const getTransport = async () => {
if (process.env.SENDGRID_API_KEY) { if (!transport) {
const sgTransportParams = sgTransport({ transport = nodemailer.createTransport({
auth: { api_key: process.env.SENDGRID_API_KEY }, host: process.env.SMTP_HOST,
}); port: parseInt(process.env.SMTP_PORT),
return nodemailer.createTransport(sgTransportParams); secure: process.env.SMTP_SECURE === "true",
} else { auth: {
const auth = await nodemailer.createTestAccount(); user: process.env.SMTP_USER,
pass: process.env.SMTP_PWD,
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,
}); });
} }
return transport;
}; };
export const sendEmail = async (params: SendEmailParameters) => { export const sendEmail = async (params: SendEmailParameters) => {
const transport = await getTransport(); const transport = await getTransport();
return await transport.sendMail({ try {
to: params.to, await transport.verify();
from: `Rallly ${process.env.NEXT_PUBLIC_SUPPORT_EMAIL}`, return await transport.sendMail({
subject: params.subject, to: params.to,
html: params.html, 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