簡體   English   中英

Nodemailer 在本地工作,但不在 lambda 上工作

[英]Nodemailer working in local, but not on lambda

當我在本地運行時,Nodemailer 正在發送電子郵件,但不在 lambda 上運行。 令人驚訝的是,SO 上與此類似的問題的解決方案都不適用於我。 問題之一還建議使 nodemailer 中的 sendEmail 函數異步並等待它,因為它不像在本地那樣等待它在 AWS 上完成執行。

下面是我的具有郵寄邏輯的代碼(請參閱我用標簽 [1] 和 [2] 注釋的行):

 import * as nodemailer from 'nodemailer'; import SMTPTransport = require('nodemailer/lib/smtp-transport'); import * as Tracer from 'tracer'; import {Transport} from 'common-models/transport'; import {MailObject} from 'common-models/mailObject'; import * as Mail from 'nodemailer'; import EmailTemplates = require('swig-email-templates'); export class MailHelper { private logLevel = process.env.TRACER_LEVEL || 'debug'; private smtpOptions: SMTPTransport.Options; private transporter: nodemailer.Transporter; private templates: EmailTemplates; private logger: Tracer.Tracer.Logger = Tracer.colorConsole({ level: this.logLevel }); constructor(transport: Transport) { this.smtpOptions = { host: transport.host, port: transport.port, secure: transport.secure, tls: transport.tls, auth: { user: transport.auth.user, pass: transport.auth.password } } } connect(): Promise < boolean > { console.log(`Inside connect`); return new Promise < boolean > (async (resolve, reject) => { try { this.transporter = await nodemailer.createTransport(this.smtpOptions) resolve(true); } catch (error) { this.logger.fatal(error); reject(error); } }) } async sendMail(body: MailObject): Promise < string > { //<-----[1] made this asynchronous console.log(`[bodymail][value]: ${JSON.stringify(body)}`) const senderAddress: Mail.Address = { address: body.from.emailAddress, name: body.from.name ? body.from.name : '' } const recipientAddress: Mail.Address = { address: body.to.emailAddress, name: body.to.name ? body.to.name : '' } const mailOptions: Mail.Options = { from: senderAddress, to: recipientAddress, subject: body.subject, text: body.text ? body.text : undefined, html: body.html ? body.html : undefined, amp: body.html ? body.html : undefined } console.log(`[mail options][value]: ${JSON.stringify(mailOptions)}`) return new Promise<any> (async(resolve, reject) => { if (!this.transporter) { this.logger.warn(`Transporter not intitialized. Use connect method`); reject(`Transporter not initialized. Use connect method`); } console.log(`Inside the promise of sendMail`); this.transporter.sendMail(mailOptions).then(async value => { //<--- [2] also using then to await for the result console.log(`[sentMail][value]: ${JSON.stringify(value)}`); resolve(value); }).catch(error => { this.logger.fatal(error); reject(error); }) }) } public sendMailFromHTML(body: MailObject): Promise < string > { return new Promise < string > (async (resolve, reject) => { console.log(`Inside the promise of sendMailFromHTML`); this.sendMail(body).then((value: string) => { resolve(value); }).catch(error => { reject(error); }) }) } public sendMailFromTemplate(body: MailObject, values: {}): Promise < string > { this.templates = new EmailTemplates(); return new Promise < string > ((resolve, reject) => { console.log(`Inside promise of sendMailFromTemplate`); this.templates.render(body.template ? body.template : '', values, (err, html, text, subject) => { if (err) { this.logger.error(err); reject(err); } else { body.subject = subject ? subject : body.subject; body.html = html; body.text = text; this.sendMail(body).then((value: string) => { resolve(value); }).catch(error => { reject(error); }); } }); }); } }

嘗試改變:

this.transporter = await nodemailer.createTransport(this.smtpOptions)

到:

this.transporter = nodemailer.createTransport(this.smtpOptions)

這修復了我在 Vercel 上托管的項目的 SMTP 問題。 行為類似,我無法從生產發送電子郵件,但我的本地環境沒有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM