配置AWS SES邮箱适配器后Parse-Server启动失败求助
Parse Server 8.2.1 配置AWS SES邮箱适配器(密码重置邮件)问题解决
问题概述
运行Parse Server 8.2.1,需配置AWS SES发送用户密码重置邮件,使用config.json而非server.js进行配置。当前配置后服务无法启动,报错template content path is invalid,但日志打印的模板路径正确指向实际存在的文件。
当前环境
- AWS侧:已完成验证邮箱、访问密钥及密钥的配置
- Parse Server:版本8.2.1,使用
config.json配置 - 已安装依赖包:
parse-server@8.2.1 /opt/bitnami/parse ├── @aws-sdk/client-ses@3.844.0 └── parse-server-api-mail-adapter@3.1.1
- 邮件模板:在
config.json同路径下创建emails文件夹,包含password_reset.html和password_reset.txt文件
错误详情
启动服务时日志输出正确的模板路径,但仍报错:
Adapter options received: { options: { sender: 'XXXXXXX@gmail.com', templates: { passwordResetEmail: [Object] } } } Resolved template paths: { textPath: '/opt/bitnami/parse/emails/password_reset.txt', htmlPath: '/opt/bitnami/parse/emails/password_reset.html', markdownPath: undefined } Error: ApiMailAdapter: template content path is invalid. at Errors.get (/opt/bitnami/parse/node_modules/parse-server-api-mail-adapter/lib/Errors.js:44:37) at ApiMailAdapter._validateTemplate (/opt/bitnami/parse/node_modules/parse-server-api-mail-adapter/lib/ApiMailAdapter.js:519:22) at new ApiMailAdapter (/opt/bitnami/parse/node_modules/parse-server-api-mail-adapter/lib/ApiMailAdapter.js:62:13) at mailAdapterWrapper (/opt/bitnami/parse/exp06.js:33:10) ...
问题定位与修复步骤
1. 修复依赖导入错误
exp06.js中SES客户端的导入语句为空,需补充正确的依赖包路径:
// 原错误代码 // const { SESClient, SendEmailCommand } = require(''); // 修正后 const { SESClient, SendEmailCommand } = require('@aws-sdk/client-ses');
2. 移除不必要的路径解析
parse-server-api-mail-adapter内部会自动处理模板路径的验证与读取,手动调用path.resolve可能导致路径识别异常,直接保留原始路径即可:
修改exp06.js中的模板处理逻辑,删除path.resolve相关代码:
// 原代码 // const reset = templates.passwordResetEmail; // if (reset) { // if (reset.textPath) reset.textPath = path.resolve(reset.textPath); // if (reset.htmlPath) reset.htmlPath = path.resolve(reset.htmlPath); // ... // } // 修正后无需手动处理路径,直接传递原始配置
3. 验证模板文件权限
确保Parse Server运行用户(bitnami)拥有模板文件的读取权限:
chown bitnami:bitnami /opt/bitnami/parse/emails/* chmod 644 /opt/bitnami/parse/emails/*
4. 优化config.json路径配置
使用相对路径简化配置(若exp06.js与config.json同目录):
"emailAdapter": { "adapter": "./exp06.js", "options": { "sender": "AwsVerfiedEmail@myURL.com", "templates": { "passwordResetEmail": { "subject": "Reset your {{appName}} password", "textPath": "./emails/password_reset.txt", "htmlPath": "./emails/password_reset.html" } } } }
5. 完整修正后的exp06.js
require('regenerator-runtime/runtime'); const { default: ApiMailAdapter } = require('parse-server-api-mail-adapter'); const { SESClient, SendEmailCommand } = require('@aws-sdk/client-ses'); const client = new SESClient({ region: 'us-east-1', credentials: { accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, }, }); module.exports = function mailAdapterWrapper(options = {}) { console.log("Adapter options received:", { options }); return new ApiMailAdapter({ apiCallback: async ({ to, subject, text, html }) => { const emailParams = { Destination: { ToAddresses: [to], }, Message: { Subject: { Data: subject, }, Body: {}, }, Source: options.sender || 'blipdropper@gmail.com', }; if (text) { emailParams.Message.Body.Text = { Data: text }; } if (html) { emailParams.Message.Body.Html = { Data: html }; } await client.send(new SendEmailCommand(emailParams)); }, sender: options.sender, templates: options.templates, }); };
验证
修复完成后重启Parse Server,检查日志确认无启动错误,触发密码重置流程验证邮件是否正常发送。
内容的提问来源于stack exchange,提问作者Daniel Patriarca
相关产品推荐
相关产品推荐

