1
1
import { render } from "@react-email/render" ;
2
+ import nodemailer from "nodemailer" ;
2
3
import { EmailError , MailMessage , MailTransport , PlainTextMailMessage } from "./index" ;
3
- import nodemailer from "nodemailer"
4
4
5
5
export type SmtpMailTransportOptions = {
6
- type : ' smtp' ,
6
+ type : " smtp" ;
7
7
config : {
8
- host ?: string ,
9
- port ?: number ,
10
- secure ?: boolean ,
8
+ host ?: string ;
9
+ port ?: number ;
10
+ secure ?: boolean ;
11
11
auth ?: {
12
- user ?: string ,
13
- pass ?: string
14
- }
15
- }
16
- }
12
+ user ?: string ;
13
+ pass ?: string ;
14
+ } ;
15
+ } ;
16
+ } ;
17
17
18
18
export class SmtpMailTransport implements MailTransport {
19
19
#client: nodemailer . Transporter ;
20
20
21
21
constructor ( options : SmtpMailTransportOptions ) {
22
- this . #client = nodemailer . createTransport ( options . config )
22
+ this . #client = nodemailer . createTransport ( options . config ) ;
23
23
}
24
24
25
- async send ( { to, from, replyTo, subject, react} : MailMessage ) : Promise < void > {
25
+ async send ( { to, from, replyTo, subject, react } : MailMessage ) : Promise < void > {
26
26
try {
27
27
await this . #client. sendMail ( {
28
28
from : from ,
@@ -31,16 +31,19 @@ export class SmtpMailTransport implements MailTransport {
31
31
subject,
32
32
html : render ( react ) ,
33
33
} ) ;
34
- }
35
- catch ( error : Error ) {
36
- console . error (
37
- `Failed to send email to ${ to } , ${ subject } . Error ${ error . name } : ${ error . message } `
38
- ) ;
39
- throw new EmailError ( error ) ;
34
+ } catch ( error ) {
35
+ if ( error instanceof Error ) {
36
+ console . error (
37
+ `Failed to send email to ${ to } , ${ subject } . Error ${ error . name } : ${ error . message } `
38
+ ) ;
39
+ throw new EmailError ( error ) ;
40
+ } else {
41
+ throw error ;
42
+ }
40
43
}
41
44
}
42
45
43
- async sendPlainText ( { to, from, replyTo, subject, text} : PlainTextMailMessage ) : Promise < void > {
46
+ async sendPlainText ( { to, from, replyTo, subject, text } : PlainTextMailMessage ) : Promise < void > {
44
47
try {
45
48
await this . #client. sendMail ( {
46
49
from : from ,
@@ -49,12 +52,15 @@ export class SmtpMailTransport implements MailTransport {
49
52
subject,
50
53
text : text ,
51
54
} ) ;
52
- }
53
- catch ( error : Error ) {
54
- console . error (
55
- `Failed to send email to ${ to } , ${ subject } . Error ${ error . name } : ${ error . message } `
56
- ) ;
57
- throw new EmailError ( error ) ;
55
+ } catch ( error ) {
56
+ if ( error instanceof Error ) {
57
+ console . error (
58
+ `Failed to send email to ${ to } , ${ subject } . Error ${ error . name } : ${ error . message } `
59
+ ) ;
60
+ throw new EmailError ( error ) ;
61
+ } else {
62
+ throw error ;
63
+ }
58
64
}
59
65
}
60
66
}
0 commit comments