当前位置: 首页 > news >正文

QuickJS 如何发送一封邮件 ?

参阅:bellard.org‌ : QuickJS 如何使用 qjs 执行 js 脚本

在 QuickJS 中发送邮件需要依赖外部库或调用系统命令,因为 QuickJS 本身不包含 SMTP 功能。以下是两种实现方法:

方法 1:调用系统命令(推荐)

使用 QuickJS 的 std 模块调用 curl 命令发送邮件(适用于 Linux/macOS):

// qjs sendmail.js
import * as std from 'std';// 邮件配置
const smtpServer = 'smtp://smtp.example.com:587';
const username = 'your_email@example.com';
const password = 'your_password';
const from = 'sender@example.com';
const to = 'recipient@example.com';
const subject = 'QuickJS 邮件测试';
const body = '这是一封来自 QuickJS 的测试邮件';
const attachmentPath = '/path/to/attachment.txt';// 构建 curl 命令
const command = ['curl','--url', smtpServer,'--user', `${username}:${password}`,'--mail-from', from,'--mail-rcpt', to,'--upload-file', '-','--ssl-reqd'
].join(' ');// 构建 MIME 邮件内容
const boundary = 'quickjs-boundary';
const mimeMessage = [`From: <${from}>`,`To: <${to}>`,`Subject: ${subject}`,`MIME-Version: 1.0`,`Content-Type: multipart/mixed; boundary="${boundary}"`,'',`--${boundary}`,'Content-Type: text/plain; charset=utf-8','',body,'',`--${boundary}`,`Content-Type: application/octet-stream; name="${attachmentPath.split('/').pop()}"`,'Content-Disposition: attachment','Content-Transfer-Encoding: base64','',std.loadFile(attachmentPath, 'binary').toString('base64'),'',`--${boundary}--`
].join('\r\n');// 执行命令
const proc = std.popen(command, 'w');
proc.puts(mimeMessage);
proc.close();console.log('邮件发送完成');

运行命令:

qjs sendmail.js

方法 2:使用原生模块(需要编译)

需要编写 C 扩展模块并编译进 QuickJS:

  1. 创建 mailer.c
#include "quickjs.h"
#include <libesmtp.h>static JSValue sendMail(JSContext *ctx, JSValueConst this_val,int argc, JSValueConst *argv) {// 实现 SMTP 邮件发送逻辑// 使用 libesmtp 库 (https://libesmtp.github.io/)// ...
}static const JSCFunctionListEntry funcs[] = {JS_CFUNC_DEF("send", 1, sendMail),
};static int mailer_init(JSContext *ctx, JSModuleDef *m) {return JS_SetModuleExportList(ctx, m, funcs, countof(funcs));
}JSModuleDef *js_init_module_mailer(JSContext *ctx, const char *module_name) {JSModuleDef *m = JS_NewCModule(ctx, module_name, mailer_init);if (!m) return NULL;JS_AddModuleExportList(ctx, m, funcs, countof(funcs));return m;
}
  1. 编译 QuickJS 并链接 libesmtp:
gcc -o qjs mailer.c qjs.c -Iquickjs -lesmtp -lssl -lcrypto
  1. 在 QuickJS 中使用:
import * as mailer from 'mailer';mailer.send({server: 'smtp.example.com:587',user: 'your_email@example.com',pass: 'your_password',from: 'sender@example.com',to: 'recipient@example.com',subject: '测试邮件',body: '邮件正文',attachments: ['/path/to/file.txt']
});

注意事项:

  1. curl 方法要求

    • 系统需安装 curl (7.20.0+)
    • 附件需可读且大小不超过邮件服务器限制
    • 敏感信息建议从环境变量读取
  2. 安全建议

    // 从环境变量读取凭据
    const username = std.getenv('SMTP_USER');
    const password = std.getenv('SMTP_PASS');
    
  3. Windows 系统

    • 使用 PowerShell 的 Send-MailMessage
    const psCommand = [`Send-MailMessage`,`-From '${from}'`,`-To '${to}'`,`-Subject '${subject}'`,`-Body '${body}'`,`-Attachments '${attachmentPath}'`,`-SmtpServer '${smtpServer.split(':')[0]}'`,`-Port ${smtpServer.split(':')[1] || 587}`,`-Credential (New-Object System.Management.Automation.PSCredential('${username}', (ConvertTo-SecureString '${password}' -AsPlainText -Force)))`
    ].join(' ');std.system(`powershell -Command "${psCommand}"`);
    
  4. 替代方案

    • 使用 HTTP API 发送(如 Mailgun/SendGrid)
    • 调用 Python/Node.js 脚本处理邮件发送

对于简单需求,调用系统命令是最快实现方式。对于复杂应用,建议使用 Node.js 等更成熟的运行时环境。

http://www.lqws.cn/news/197875.html

相关文章:

  • 【LLM-Agent】智能体的记忆缓存设计
  • 浅谈 React Hooks
  • ​React Hooks 的闭包陷阱问题
  • python学习打卡day47
  • 从0开始学习R语言--Day19--连续变量的相关性检验
  • 数 据 结 构 进 阶:哨 兵 位 的 头 结 点 如 何 简 化 链 表 操 作
  • 管道与进程间通信
  • 如何处理双面沉金线路板上的定位孔?
  • 实现简易动效
  • 【CSS-5】掌握CSS文本样式:从基础到高级技巧
  • MS358A 低功耗运算放大器 车规
  • Linux与Windows切换使用Obsidian,出现 unexplained changes 问题的解决
  • 阿里云ACP云计算备考笔记 (4)——企业应用服务
  • NLP学习路线图(三十):微调策略
  • 【小红书拥抱开源】小红书开源大规模混合专家模型——dots.llm1
  • 如何从浏览器中导出网站证书
  • 第5章:Cypher查询语言进阶
  • 浅谈 React Suspense
  • Svelte 核心语法详解:Vue/React 开发者如何快速上手?
  • BERT, GPT, Transformer之间的关系
  • 从温湿度控制切入:楼宇自控系统打造舒适建筑环境的路径
  • AcWing--数据结构1
  • github中main与master,master无法合并到main
  • Go深入学习延迟语句
  • MCP 技术完全指南:微软开源项目助力 AI 开发标准化学习
  • WPF学习PropertyChanged
  • 前沿论文汇总(机器学习/深度学习/大模型/搜广推/自然语言处理)
  • 【单源最短路经】Dijkstra 算法(朴素版和堆优化版)、Bellman-Ford 算法、spfa 算法 及 负环判断
  • OpenLayers 导航之运动轨迹
  • 队列的概念及实现