Skip to content

Commit f27e1e3

Browse files
committed
fix: add config mailgun
1 parent a3ba533 commit f27e1e3

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/config/email.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import nodemailer from 'nodemailer'
2+
import mg from 'nodemailer-mailgun-transport'
23
import { google } from 'googleapis'
4+
import { isEmpty } from 'lodash'
35

46
require('dotenv').config()
57

@@ -11,12 +13,16 @@ const {
1113
MAIL_USERNAME,
1214
MAIL_PASSWORD,
1315
MAIL_AUTH_TYPE,
16+
MAILGUN_API_KEY,
17+
MAILGUN_DOMAIN,
1418
OAUTH_CLIENT_ID,
1519
OAUTH_CLIENT_SECRET,
1620
OAUTH_REFRESH_TOKEN,
1721
OAUTH_REDIRECT_URL,
1822
} = process.env
1923

24+
const isMailgunAPI = !isEmpty(MAILGUN_API_KEY) || !isEmpty(MAILGUN_DOMAIN)
25+
2026
class EmailProvider {
2127
private mailConfig: nodemailer.SentMessageInfo
2228

@@ -36,9 +42,6 @@ class EmailProvider {
3642
private setMailConfig = (): nodemailer.SentMessageInfo => {
3743
const configTransport: nodemailer.SentMessageInfo = {
3844
service: MAIL_DRIVER,
39-
auth: {
40-
user: MAIL_USERNAME,
41-
},
4245
}
4346

4447
// Use Google OAuth
@@ -63,10 +66,15 @@ class EmailProvider {
6366
configTransport.auth.clientSecret = OAUTH_CLIENT_SECRET
6467
configTransport.auth.refreshToken = OAUTH_REFRESH_TOKEN
6568
configTransport.auth.accessToken = accessToken()
69+
} else if (isMailgunAPI) {
70+
// SMPT with Mailgun API
71+
configTransport.auth.api_key = MAILGUN_API_KEY
72+
configTransport.auth.domain = MAILGUN_DOMAIN
6673
} else {
6774
// SMTP Default
6875
configTransport.host = MAIL_HOST
6976
configTransport.port = MAIL_PORT
77+
configTransport.auth.user = MAIL_USERNAME
7078
configTransport.auth.pass = MAIL_PASSWORD
7179
}
7280

@@ -91,7 +99,9 @@ class EmailProvider {
9199
subject: string,
92100
text: string
93101
): void | string[] => {
94-
this.mailConfig = this.setMailConfig()
102+
this.mailConfig = isMailgunAPI
103+
? mg(this.setMailConfig())
104+
: this.setMailConfig()
95105
this.mailOptions = this.setMailOptions(dest, subject, text)
96106
// Nodemailer Transport
97107
const transporter: nodemailer.Transporter = nodemailer.createTransport(

0 commit comments

Comments
 (0)