There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.
TypeScript appcrdteditorelectronknowledge-basemarkdownmironotesnotionnotion-alternativerustrust-langrust-languagetabletableviewwhiteboardwikiworkspace
feat(server): allow customize mailer server (#5835)
liuyidf157819dc3a
diff --git a/.github/actions/deploy/deploy.mjs b/.github/actions/deploy/deploy.mjsindex d5297f3781..c3e7c58652 100644--- a/.github/actions/deploy/deploy.mjs+++ b/.github/actions/deploy/deploy.mjs@@ -15,9 +15,9 @@ const {R2_SECRET_ACCESS_KEY,ENABLE_CAPTCHA,CAPTCHA_TURNSTILE_SECRET,- OAUTH_EMAIL_SENDER,- OAUTH_EMAIL_LOGIN,- OAUTH_EMAIL_PASSWORD,+ MAILER_SENDER,+ MAILER_USER,+ MAILER_PASSWORD,AFFINE_GOOGLE_CLIENT_ID,AFFINE_GOOGLE_CLIENT_SECRET,CLOUD_SQL_IAM_ACCOUNT,@@ -103,9 +103,9 @@ const createHelmCommand = ({ isDryRun }) => {`--set-string graphql.app.objectStorage.r2.accountId="${R2_ACCOUNT_ID}"`,`--set-string graphql.app.objectStorage.r2.accessKeyId="${R2_ACCESS_KEY_ID}"`,`--set-string graphql.app.objectStorage.r2.secretAccessKey="${R2_SECRET_ACCESS_KEY}"`,- `--set-string graphql.app.oauth.email.sender="${OAUTH_EMAIL_SENDER}"`,- `--set-string graphql.app.oauth.email.login="${OAUTH_EMAIL_LOGIN}"`,- `--set-string graphql.app.oauth.email.password="${OAUTH_EMAIL_PASSWORD}"`,+ `--set-string graphql.app.mailer.sender="${MAILER_SENDER}"`,+ `--set-string graphql.app.mailer.user="${MAILER_USER}"`,+ `--set-string graphql.app.mailer.password="${MAILER_PASSWORD}"`,`--set-string graphql.app.oauth.google.enabled=true`,`--set-string graphql.app.oauth.google.clientId="${AFFINE_GOOGLE_CLIENT_ID}"`,`--set-string graphql.app.oauth.google.clientSecret="${AFFINE_GOOGLE_CLIENT_SECRET}"`,diff --git a/.github/helm/affine/charts/graphql/templates/deployment.yaml b/.github/helm/affine/charts/graphql/templates/deployment.yamlindex 06d7bf77ea..5cc2d61ba1 100644--- a/.github/helm/affine/charts/graphql/templates/deployment.yaml+++ b/.github/helm/affine/charts/graphql/templates/deployment.yaml@@ -85,31 +85,31 @@ spec:value: "{{ .Values.app.features.earlyAccessPreview }}"- name: FEATURES_SYNC_CLIENT_VERSION_CHECKvalue: "{{ .Values.app.features.syncClientVersionCheck }}"- - name: OAUTH_EMAIL_SENDER+ - name: MAILER_HOSTvalueFrom:secretKeyRef:- name: "{{ .Values.app.oauth.email.secretName }}"- key: sender- - name: OAUTH_EMAIL_LOGIN+ name: "{{ .Values.app.mailer.secretName }}"+ key: host+ - name: MAILER_PORTvalueFrom:secretKeyRef:- name: "{{ .Values.app.oauth.email.secretName }}"- key: login- - name: OAUTH_EMAIL_SERVER+ name: "{{ .Values.app.mailer.secretName }}"+ key: port+ - name: MAILER_USERvalueFrom:secretKeyRef:- name: "{{ .Values.app.oauth.email.secretName }}"- key: server- - name: OAUTH_EMAIL_PORT+ name: "{{ .Values.app.mailer.secretName }}"+ key: user+ - name: MAILER_PASSWORDvalueFrom:secretKeyRef:- name: "{{ .Values.app.oauth.email.secretName }}"- key: port- - name: OAUTH_EMAIL_PASSWORD+ name: "{{ .Values.app.mailer.secretName }}"+ key: password+ - name: MAILER_SENDERvalueFrom:secretKeyRef:- name: "{{ .Values.app.oauth.email.secretName }}"- key: password+ name: "{{ .Values.app.mailer.secretName }}"+ key: sender- name: STRIPE_API_KEYvalueFrom:secretKeyRef:diff --git a/.github/helm/affine/charts/graphql/values.yaml b/.github/helm/affine/charts/graphql/values.yamlindex 78cd80e9c8..a4f01e653a 100644--- a/.github/helm/affine/charts/graphql/values.yaml+++ b/.github/helm/affine/charts/graphql/values.yaml@@ -35,14 +35,7 @@ app:accountId: ''accessKeyId: ''secretAccessKey: ''- oauth:- email:- secretName: 'oauth-email'- sender: '[email protected]'- login: ''- password: ''- server: 'smtp.gmail.com'- port: '465'+ oauth:google:enabled: falsesecretName: oauth-google@@ -53,6 +46,13 @@ app:secretName: oauth-githubclientId: ''clientSecret: ''+ mailer:+ secretName: 'mailer'+ host: 'smtp.gmail.com'+ port: '465'+ user: ''+ password: ''+ sender: '[email protected]'payment:stripe:secretName: 'stripe'diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.ymlindex df596e5828..f0ffe2aaf1 100644--- a/.github/workflows/build-test.yml+++ b/.github/workflows/build-test.yml@@ -448,7 +448,6 @@ jobs:${{ matrix.tests.script }}env:DEV_SERVER_URL: http://localhost:8080- ENABLE_LOCAL_EMAIL: true- name: Upload test resultsif: ${{ failure() }}diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.ymlindex bc598c818b..c9d14b8986 100644--- a/.github/workflows/deploy.yml+++ b/.github/workflows/deploy.yml@@ -275,9 +275,9 @@ jobs:R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}ENABLE_CAPTCHA: trueCAPTCHA_TURNSTILE_SECRET: ${{ secrets.CAPTCHA_TURNSTILE_SECRET }}- OAUTH_EMAIL_SENDER: ${{ secrets.OAUTH_EMAIL_SENDER }}- OAUTH_EMAIL_LOGIN: ${{ secrets.OAUTH_EMAIL_LOGIN }}- OAUTH_EMAIL_PASSWORD: ${{ secrets.OAUTH_EMAIL_PASSWORD }}+ MAILER_SENDER: ${{ secrets.OAUTH_EMAIL_SENDER }}+ MAILER_USER: ${{ secrets.OAUTH_EMAIL_LOGIN }}+ MAILER_PASSWORD: ${{ secrets.OAUTH_EMAIL_PASSWORD }}GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}AFFINE_GOOGLE_CLIENT_ID: ${{ secrets.AFFINE_GOOGLE_CLIENT_ID }}AFFINE_GOOGLE_CLIENT_SECRET: ${{ secrets.AFFINE_GOOGLE_CLIENT_SECRET }}diff --git a/docs/developing-server.md b/docs/developing-server.mdindex 843fc2d4d1..2c044f25b1 100644--- a/docs/developing-server.md+++ b/docs/developing-server.md@@ -59,9 +59,9 @@ You may need additional env for auth login. You may want to put your own one ifFor email login & password, please refer to https://nodemailer.com/usage/using-gmail/```-OAUTH_EMAIL_SENDER=-OAUTH_EMAIL_LOGIN=-OAUTH_EMAIL_PASSWORD=+MAILER_SENDER=+MAILER_USER=+MAILER_PASSWORD=OAUTH_GOOGLE_ENABLED="true"OAUTH_GOOGLE_CLIENT_ID=OAUTH_GOOGLE_CLIENT_SECRET=diff --git a/packages/backend/server/package.json b/packages/backend/server/package.jsonindex 4fefebd0e7..69ec5e2bc5 100644--- a/packages/backend/server/package.json+++ b/packages/backend/server/package.json@@ -139,10 +139,11 @@"environmentVariables": {"TS_NODE_PROJECT": "./tests/tsconfig.json","NODE_ENV": "test",- "ENABLE_LOCAL_EMAIL": "true",- "OAUTH_EMAIL_LOGIN": "[email protected]",- "OAUTH_EMAIL_PASSWORD": "affine",- "OAUTH_EMAIL_SENDER": "[email protected]",+ "MAILER_HOST": "0.0.0.0",+ "MAILER_PORT": "1025",+ "MAILER_USER": "[email protected]",+ "MAILER_PASSWORD": "affine",+ "MAILER_SENDER": "[email protected]","FEATURES_EARLY_ACCESS_PREVIEW": "false"}},diff --git a/packages/backend/server/src/config/affine.env.ts b/packages/backend/server/src/config/affine.env.tsindex 0aae7ff906..8b4aadc04d 100644--- a/packages/backend/server/src/config/affine.env.ts+++ b/packages/backend/server/src/config/affine.env.ts@@ -13,11 +13,12 @@ AFFiNE.ENV_MAP = {OAUTH_GITHUB_ENABLED: ['auth.oauthProviders.github.enabled', 'boolean'],OAUTH_GITHUB_CLIENT_ID: 'auth.oauthProviders.github.clientId',OAUTH_GITHUB_CLIENT_SECRET: 'auth.oauthProviders.github.clientSecret',- OAUTH_EMAIL_LOGIN: 'auth.email.login',- OAUTH_EMAIL_SENDER: 'auth.email.sender',- OAUTH_EMAIL_SERVER: 'auth.email.server',- OAUTH_EMAIL_PORT: ['auth.email.port', 'int'],- OAUTH_EMAIL_PASSWORD: 'auth.email.password',+ MAILER_HOST: 'mailer.host',+ MAILER_PORT: ['mailer.port', 'int'],+ MAILER_USER: 'mailer.auth.user',+ MAILER_PASSWORD: 'mailer.auth.pass',+ MAILER_SENDER: 'mailer.from.address',+ MAILER_SECURE: ['mailer.secure', 'boolean'],THROTTLE_TTL: ['rateLimiter.ttl', 'int'],THROTTLE_LIMIT: ['rateLimiter.limit', 'int'],REDIS_SERVER_HOST: 'plugins.redis.host',@@ -30,7 +31,6 @@ AFFiNE.ENV_MAP = {'doc.manager.experimentalMergeWithYOcto','boolean',],- ENABLE_LOCAL_EMAIL: ['auth.localEmail', 'boolean'],STRIPE_API_KEY: 'plugins.payment.stripe.keys.APIKey',STRIPE_WEBHOOK_KEY: 'plugins.payment.stripe.keys.webhookKey',FEATURES_EARLY_ACCESS_PREVIEW: ['featureFlags.earlyAccessPreview', 'boolean'],diff --git a/packages/backend/server/src/config/affine.self.ts b/packages/backend/server/src/config/affine.self.tsindex 65d1b27569..96cf69c842 100644--- a/packages/backend/server/src/config/affine.self.ts+++ b/packages/backend/server/src/config/affine.self.ts@@ -42,5 +42,13 @@ AFFiNE.plugins.use('redis');AFFiNE.plugins.use('payment');if (AFFiNE.deploy) {+ AFFiNE.mailer = {+ service: 'gmail',+ auth: {+ user: env.MAILER_USER,+ pass: env.MAILER_PASSWORD,+ },+ };+AFFiNE.plugins.use('gcloud');}diff --git a/packages/backend/server/src/core/auth/next-auth-options.ts b/packages/backend/server/src/core/auth/next-auth-options.tsindex e6308f3f6e..3a80c2edd9 100644--- a/packages/backend/server/src/core/auth/next-auth-options.ts+++ b/packages/backend/server/src/core/auth/next-auth-options.ts@@ -97,22 +97,7 @@ export const NextAuthOptionsProvider: FactoryProvider<NextAuthOptions> = {return result;};const nextAuthOptions: NextAuthOptions = {- providers: [- // @ts-expect-error esm interop issue- Email.default({- server: {- host: config.auth.email.server,- port: config.auth.email.port,- auth: {- user: config.auth.email.login,- pass: config.auth.email.password,- },- },- from: config.auth.email.sender,- sendVerificationRequest: (params: SendVerificationRequestParams) =>- sendVerificationRequest(config, logger, mailer, session, params),- }),- ],+ providers: [],adapter: prismaAdapter,debug: !config.node.prod,session: {@@ -138,6 +123,18 @@ export const NextAuthOptionsProvider: FactoryProvider<NextAuthOptions> = {},};+ if (config.mailer && mailer) {+ nextAuthOptions.providers.push(+ // @ts-expect-error esm interop issue+ Email.default({+ server: config.mailer,+ from: config.mailer.from,+ sendVerificationRequest: (params: SendVerificationRequestParams) =>+ sendVerificationRequest(config, logger, mailer, session, params),+ })+ );+ }+nextAuthOptions.providers.push(// @ts-expect-error esm interop issueCredentials.default({diff --git a/packages/backend/server/src/fundamentals/config/def.ts b/packages/backend/server/src/fundamentals/config/def.tsindex 833de588a8..2dc04cad3e 100644--- a/packages/backend/server/src/fundamentals/config/def.ts+++ b/packages/backend/server/src/fundamentals/config/def.ts@@ -1,4 +1,5 @@import type { ApolloDriverConfig } from '@nestjs/apollo';+import SMTPTransport from 'nodemailer/lib/smtp-transport';import type { LeafPaths } from '../utils/types';import { EnvConfigType } from './env';@@ -264,18 +265,6 @@ export interface AFFiNEConfig {}>>;- /**- * whether to use local email service to send email- * local debug only- */- localEmail: boolean;- email: {- server: string;- port: number;- login: string;- sender: string;- password: string;- };captcha: {/*** whether to enable captcha@@ -299,6 +288,13 @@ export interface AFFiNEConfig {};};+ /**+ * Configurations for mail service used to post auth or bussiness mails.+ *+ * @see https://nodemailer.com/smtp/+ */+ mailer?: SMTPTransport.Options;+doc: {manager: {/**diff --git a/packages/backend/server/src/fundamentals/config/default.ts b/packages/backend/server/src/fundamentals/config/default.tsindex 49bc5a6085..9c884a136f 100644--- a/packages/backend/server/src/fundamentals/config/default.ts+++ b/packages/backend/server/src/fundamentals/config/default.ts@@ -167,14 +167,6 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {return this.privateKey;},oauthProviders: {},- localEmail: false,- email: {- server: 'smtp.gmail.com',- port: 465,- login: '',- sender: '',- password: '',- },},storage: getDefaultAFFiNEStorageConfig(),rateLimiter: {diff --git a/packages/backend/server/src/fundamentals/mailer/index.ts b/packages/backend/server/src/fundamentals/mailer/index.tsindex e990f6c489..b91a7a8fb4 100644--- a/packages/backend/server/src/fundamentals/mailer/index.ts+++ b/packages/backend/server/src/fundamentals/mailer/index.ts@@ -1,11 +1,21 @@import { Global, Module } from '@nestjs/common';+import { OptionalModule } from '../nestjs';import { MailService } from './mail.service';import { MAILER } from './mailer';+@Global()+@OptionalModule({+ providers: [MAILER],+ exports: [MAILER],+ requires: ['mailer.auth.user', 'mailer.auth.pass'],+})+class MailerModule {}+@Global()@Module({- providers: [MAILER, MailService],+ imports: [MailerModule],+ providers: [MailService],exports: [MailService],})export class MailModule {}diff --git a/packages/backend/server/src/fundamentals/mailer/mail.service.ts b/packages/backend/server/src/fundamentals/mailer/mail.service.tsindex 66241e90c7..aab0620a4e 100644--- a/packages/backend/server/src/fundamentals/mailer/mail.service.ts+++ b/packages/backend/server/src/fundamentals/mailer/mail.service.ts@@ -1,30 +1,28 @@-import { Inject, Injectable } from '@nestjs/common';+import { Inject, Injectable, Optional } from '@nestjs/common';import { Config } from '../config';-import {- MAILER_SERVICE,- type MailerService,- type Options,- type Response,-} from './mailer';+import { MAILER_SERVICE, type MailerService, type Options } from './mailer';import { emailTemplate } from './template';@Injectable()export class MailService {constructor(- @Inject(MAILER_SERVICE) private readonly mailer: MailerService,- private readonly config: Config+ private readonly config: Config,+ @Optional() @Inject(MAILER_SERVICE) private readonly mailer?: MailerService) {}- async sendMail(options: Options): Promise<Response> {- return this.mailer.sendMail(options);+ async sendMail(options: Options) {+ if (!this.mailer) {+ throw new Error('Mailer service is not configured.');+ }++ return this.mailer.sendMail({+ from: this.config.mailer?.from,+ ...options,+ });}hasConfigured() {- return (- !!this.config.auth.email.login &&- !!this.config.auth.email.password &&- !!this.config.auth.email.sender- );+ return !!this.mailer;}async sendInviteEmail(@@ -80,7 +78,6 @@ export class MailService {});return this.sendMail({- from: this.config.auth.email.sender,to,subject: `${invitationInfo.user.name} invited you to join ${invitationInfo.workspace.name}`,html,@@ -119,7 +116,6 @@ export class MailService {buttonUrl: url,});return this.sendMail({- from: this.config.auth.email.sender,to,subject: `Modify your AFFiNE password`,html,@@ -135,7 +131,6 @@ export class MailService {buttonUrl: url,});return this.sendMail({- from: this.config.auth.email.sender,to,subject: `Set your AFFiNE password`,html,@@ -150,7 +145,6 @@ export class MailService {buttonUrl: url,});return this.sendMail({- from: this.config.auth.email.sender,to,subject: `Verify your current email for AFFiNE`,html,@@ -165,7 +159,6 @@ export class MailService {buttonUrl: url,});return this.sendMail({- from: this.config.auth.email.sender,to,subject: `Verify your new email for AFFiNE`,html,@@ -177,7 +170,6 @@ export class MailService {content: `As per your request, we have changed your email. Please make sure you're using ${to} when you log in the next time. `,});return this.sendMail({- from: this.config.auth.email.sender,to,subject: `Your email has been changed`,html,@@ -200,7 +192,6 @@ export class MailService {content: `${inviteeName} has joined ${workspaceName}`,});return this.sendMail({- from: this.config.auth.email.sender,to,subject: title,html,@@ -223,7 +214,6 @@ export class MailService {content: `${inviteeName} has left your workspace`,});return this.sendMail({- from: this.config.auth.email.sender,to,subject: title,html,diff --git a/packages/backend/server/src/fundamentals/mailer/mailer.ts b/packages/backend/server/src/fundamentals/mailer/mailer.tsindex ba02cc8887..1f8affac2b 100644--- a/packages/backend/server/src/fundamentals/mailer/mailer.ts+++ b/packages/backend/server/src/fundamentals/mailer/mailer.ts@@ -11,28 +11,11 @@ export type Response = SMTPTransport.SentMessageInfo;export type Options = SMTPTransport.Options;export const MAILER: FactoryProvider<- Transporter<SMTPTransport.SentMessageInfo>+ Transporter<SMTPTransport.SentMessageInfo> | undefined> = {provide: MAILER_SERVICE,useFactory: (config: Config) => {- if (config.auth.localEmail) {- return createTransport({- host: '0.0.0.0',- port: 1025,- secure: false,- auth: {- user: config.auth.email.login,- pass: config.auth.email.password,- },- });- }- return createTransport({- service: 'gmail',- auth: {- user: config.auth.email.login,- pass: config.auth.email.password,- },- });+ return config.mailer ? createTransport(config.mailer) : undefined;},inject: [Config],};diff --git a/tests/affine-cloud/playwright.config.ts b/tests/affine-cloud/playwright.config.tsindex 1587aef56a..37666a5ad2 100644--- a/tests/affine-cloud/playwright.config.ts+++ b/tests/affine-cloud/playwright.config.ts@@ -52,11 +52,12 @@ const config: PlaywrightTestConfig = {DEBUG: 'affine:*',FORCE_COLOR: 'true',DEBUG_COLORS: 'true',- ENABLE_LOCAL_EMAIL: process.env.ENABLE_LOCAL_EMAIL ?? 'true',NEXTAUTH_URL: 'http://localhost:8080',- OAUTH_EMAIL_SENDER: '[email protected]',- OAUTH_EMAIL_LOGIN: '[email protected]',- OAUTH_EMAIL_PASSWORD: 'affine',+ MAILER_HOST: '0.0.0.0',+ MAILER_PORT: '1025',+ MAILER_SENDER: '[email protected]',+ MAILER_USER: '[email protected]',+ MAILER_PASSWORD: 'affine',},},],diff --git a/tests/affine-desktop-cloud/playwright.config.ts b/tests/affine-desktop-cloud/playwright.config.tsindex b732df17bb..4016026f52 100644--- a/tests/affine-desktop-cloud/playwright.config.ts+++ b/tests/affine-desktop-cloud/playwright.config.ts@@ -47,9 +47,8 @@ const config: PlaywrightTestConfig = {DEBUG: 'affine:*',FORCE_COLOR: 'true',DEBUG_COLORS: 'true',- ENABLE_LOCAL_EMAIL: process.env.ENABLE_LOCAL_EMAIL ?? 'true',NEXTAUTH_URL: 'http://localhost:8080',- OAUTH_EMAIL_SENDER: '[email protected]',+ MAILER_SENDER: '[email protected]',},},],