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

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.lryc.cn/news/2404154.html

相关文章:

  • clickhouse 和 influxdb 选型
  • GOOUUU ESP32-S3-CAM 果云科技开发板开发指南(一)(超详细!)Vscode+espidf 通过摄像头拍摄照片并存取到SD卡中,文末附源码
  • C++学习思路
  • 全流程开源!高德3D贴图生成系统,白模一键生成真实感纹理贴图
  • 使用Conda管理服务器多版本Python环境的完整指南
  • html 滚动条滚动过快会留下边框线
  • 数据通信与计算机网络——数据与信号
  • 【LLM大模型技术专题】「入门到精通系列教程」LangChain4j与Spring Boot集成开发实战指南
  • Flask 基础与实战概述
  • 东芝Toshiba e-STUDIO2110AC打印机信息
  • Vue3 GSAP动画库绑定滚动条视差效果 绑定滚动条 滚动条动画 时间轴
  • grafana-mcp-analyzer:基于 MCP 的轻量 AI 分析监控图表的运维神器!
  • git commit 执行报错 sh: -/: invalid option
  • uniapp 设置手机不息屏
  • 【题解-洛谷】B3622 枚举子集(递归实现指数型枚举)
  • (LeetCode 每日一题)3170. 删除星号以后字典序最小的字符串(贪心+栈)
  • Protobuf 中的类型查找规则
  • Python项目中添加环境配置文件
  • 【区块链基础】区块链的 Fork(分叉)深度解析:原理、类型、历史案例及共识机制的影响
  • IOS 打包账号发布上传和IOS Xcode证书配置
  • 使用 HTML + JavaScript 实现文章逐句高亮朗读功能
  • 【CSS-4】掌握CSS文字样式:从基础到高级技巧
  • 双碳时代,能源调度的难题正从“发电侧”转向“企业侧”
  • 3. 简述node.js特性与底层原理
  • OpenCV CUDA模块图像处理------创建一个模板匹配(Template Matching)对象函数createTemplateMatching()
  • 【Kubernetes】K8s 之 ETCD - 恢复备份
  • CMS、G1、ZGC、Shenandoah 的全面对比
  • RabbitMQ 学习
  • 如何轻松、安全地管理密码(新手指南)
  • AWS App Mesh实战:构建可观测、安全的微服务通信解决方案