使用@nestjs/mailer 发送带有 Excel 附件的电子邮件
Send email with Excel attachment using @nestjs/mailer
我正在尝试通过@nestjs/mailer发送带有excel附件的电子邮件:
await this.mailService.send({ //service with @nestjs/mailer
to: messageTo,
subject: messageTitle,
template: 'application-approved',
context: {
content: messageBody,
},
attachments: attachments.map(({ name, arrayBuffer }) => ({ // array with filenames and arrayBuffers
filename: name, // filename like `name.xlsx`
contents: arrayBuffer, // arrayBuffer contains filled excel file
contentType: // tried to remove this property - no effect
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
})),
});
当邮件到达时,所有附件都被标记为已损坏。我尝试同时发送 ArrayBuffer 和 Buffer - 它没有效果。
另一方面,创建文件并将其附加到响应的 HTTP 路由成功。
有解决这个问题的想法吗?
我从未使用过 @nestjs/mailer
,但由于它在底层使用了 nodemailer
,我会尝试这样做:
await this.mailService.send({
to: messageTo,
subject: messageTitle,
template: 'application-approved',
context: {
content: messageBody,
},
attachments: attachments.map(({ name, arrayBuffer }) => ({
filename: name,
contents: Buffer.from(arrayBuffer).toString('base64'),
contentTransferEncoding: 'base64',
contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
})),
});
如果这不起作用,也许您应该首先检查文件内容是否正确。可以把文件存盘打开,如果不行那就是数据问题,不是传输问题。
此外,检查 nodemailer
附件的类型定义,看看您还有哪些其他选项:
interface AttachmentLike {
/** String, Buffer or a Stream contents for the attachmentent */
content?: string | Buffer | Readable | undefined;
/** path to a file or an URL (data uris are allowed as well) if you want to stream the file instead of including it (better for larger attachments) */
path?: string | Url | undefined;
}
interface Attachment extends AttachmentLike {
/** filename to be reported as the name of the attached file, use of unicode is allowed. If you do not want to use a filename, set this value as false, otherwise a filename is generated automatically */
filename?: string | false | undefined;
/** optional content id for using inline images in HTML message source. Using cid sets the default contentDisposition to 'inline' and moves the attachment into a multipart/related mime node, so use it only if you actually want to use this attachment as an embedded image */
cid?: string | undefined;
/** If set and content is string, then encodes the content to a Buffer using the specified encoding. Example values: base64, hex, binary etc. Useful if you want to use binary attachments in a JSON formatted e-mail object */
encoding?: string | undefined;
/** optional content type for the attachment, if not set will be derived from the filename property */
contentType?: string | undefined;
/** optional transfer encoding for the attachment, if not set it will be derived from the contentType property. Example values: quoted-printable, base64. If it is unset then base64 encoding is used for the attachment. If it is set to false then previous default applies (base64 for most, 7bit for text). */
contentTransferEncoding?: '7bit' | 'base64' | 'quoted-printable' | false | undefined;
/** optional content disposition type for the attachment, defaults to ‘attachment’ */
contentDisposition?: 'attachment' | 'inline' | undefined;
/** is an object of additional headers */
headers?: Headers | undefined;
/** an optional value that overrides entire node content in the mime message. If used then all other options set for this node are ignored. */
raw?: string | Buffer | Readable | AttachmentLike | undefined;
}
我正在尝试通过@nestjs/mailer发送带有excel附件的电子邮件:
await this.mailService.send({ //service with @nestjs/mailer
to: messageTo,
subject: messageTitle,
template: 'application-approved',
context: {
content: messageBody,
},
attachments: attachments.map(({ name, arrayBuffer }) => ({ // array with filenames and arrayBuffers
filename: name, // filename like `name.xlsx`
contents: arrayBuffer, // arrayBuffer contains filled excel file
contentType: // tried to remove this property - no effect
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
})),
});
当邮件到达时,所有附件都被标记为已损坏。我尝试同时发送 ArrayBuffer 和 Buffer - 它没有效果。
另一方面,创建文件并将其附加到响应的 HTTP 路由成功。
有解决这个问题的想法吗?
我从未使用过 @nestjs/mailer
,但由于它在底层使用了 nodemailer
,我会尝试这样做:
await this.mailService.send({
to: messageTo,
subject: messageTitle,
template: 'application-approved',
context: {
content: messageBody,
},
attachments: attachments.map(({ name, arrayBuffer }) => ({
filename: name,
contents: Buffer.from(arrayBuffer).toString('base64'),
contentTransferEncoding: 'base64',
contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
})),
});
如果这不起作用,也许您应该首先检查文件内容是否正确。可以把文件存盘打开,如果不行那就是数据问题,不是传输问题。
此外,检查 nodemailer
附件的类型定义,看看您还有哪些其他选项:
interface AttachmentLike {
/** String, Buffer or a Stream contents for the attachmentent */
content?: string | Buffer | Readable | undefined;
/** path to a file or an URL (data uris are allowed as well) if you want to stream the file instead of including it (better for larger attachments) */
path?: string | Url | undefined;
}
interface Attachment extends AttachmentLike {
/** filename to be reported as the name of the attached file, use of unicode is allowed. If you do not want to use a filename, set this value as false, otherwise a filename is generated automatically */
filename?: string | false | undefined;
/** optional content id for using inline images in HTML message source. Using cid sets the default contentDisposition to 'inline' and moves the attachment into a multipart/related mime node, so use it only if you actually want to use this attachment as an embedded image */
cid?: string | undefined;
/** If set and content is string, then encodes the content to a Buffer using the specified encoding. Example values: base64, hex, binary etc. Useful if you want to use binary attachments in a JSON formatted e-mail object */
encoding?: string | undefined;
/** optional content type for the attachment, if not set will be derived from the filename property */
contentType?: string | undefined;
/** optional transfer encoding for the attachment, if not set it will be derived from the contentType property. Example values: quoted-printable, base64. If it is unset then base64 encoding is used for the attachment. If it is set to false then previous default applies (base64 for most, 7bit for text). */
contentTransferEncoding?: '7bit' | 'base64' | 'quoted-printable' | false | undefined;
/** optional content disposition type for the attachment, defaults to ‘attachment’ */
contentDisposition?: 'attachment' | 'inline' | undefined;
/** is an object of additional headers */
headers?: Headers | undefined;
/** an optional value that overrides entire node content in the mime message. If used then all other options set for this node are ignored. */
raw?: string | Buffer | Readable | AttachmentLike | undefined;
}