编辑代码

const axios = require("axios")

const headers = {
  'Content-Type': 'application/x-www-form-urlencoded',
}


const EmailType = "Domain"

// 获取邮件
const getEmail = async () => {
  try {
    const response = await axios.post('https://22.do/zh/mailbox/generate', `type=${EmailType}`, { headers })

    if (response.status === 200 && response.data.data) {
      const email = response.data.data.address.email
      console.log("获取邮箱成功:", email)
      return email
    } else {
      console.log("获取邮箱失败,请检查Cookie!")
      return false
    }
  } catch (e) {
    console.log("获取邮箱失败,程序性出错!", String(e).slice(0, 100))
    return false
  }
}

// 刷新邮箱
const refreshEmail = async (email) => {
  try {
    const response = await axios.post('https://22.do/zh/mailbox/check', null,
      {
        headers: {
          "cookie": `mail=${email}`
        }
      }
    )

    if (response.status === 200 && response.data) {
      if (response.data.action && response.data.action == 'OK') {
        return response.data.Msg[0]
      } else {
        return false
      }
    } else {
      console.log("刷新邮件失败,请检查Cookie!")
      return false
    }
  } catch (e) {
    console.log("刷新邮件失败,程序性出错!")
    return false
  }
}

// 循环查询邮箱得到最新的邮件并返回邮件ID
const mailAnalyze = async (email) => {
  try {
    let mailRefreshStatus = false
    let refreshNum = 0
    while (!mailRefreshStatus) {

      if (refreshNum > 24) {
        console.log("获取验证码失败,请检查Cookie!")
        return false
      }

      refreshNum++
      console.log(`第${refreshNum}次刷新邮件`)

      const mailRefreshStatusTemp = await refreshEmail(email)
      if (mailRefreshStatusTemp) {
        mailRefreshStatus = true
        return await getMail(mailRefreshStatusTemp.mailId)
      }
      sleep(5000)
    }

    return false
  } catch (e) {
    console.log("获取验证码失败,程序性出错!")
    return false
  }
}

// 获取邮件内容
const getMail = async (mailID) => {
  try {
    const response = await axios.get(`https://22.do/zh/content/${mailID}/html`)
    const content = response.data
    if (response.status === 200 && content) {
      return content
    } else {
      console.log("请检查Cookie!")
      return false
    }
  } catch (e) {
    console.log("程序性出错!")
    return false
  }
}

const Analyze = (mail, regex = /.*/, type = false) => {
  const mailStr = String(mail)
  const match = mailStr.match(regex)
  if (match) {
    if (type) {
      return match
    } else {
      return match[0]
    }
  }
  return false
}

// 根据正则表达式获取邮件内容
const getContent = async (email, regex = /.*/, type = false) => {
  console.log(`444444444444444444`);
  const mail = await mailAnalyze(email)
  if (mail) {
    return await Analyze(mail, regex, type)
  } else {
    return false
  }
}

// 暂停函数
const sleep = (ms) => {
  const start = Date.now()
  while (Date.now() - start < ms) {
    // 无实际作用,让代码暂停执行一段时间
  }
}

module.exports = {
  getEmail,
  getContent
}


const main = async () => {
  const email = "rossanad.aoudky.l.42@gmail.com";  // 固定邮箱地址
  console.log(`使用邮箱: ${email}`);

  const regex = "/.*/";
  const type = false;

  // 获取邮件内容,这里用一个正则示例来提取验证码
  const content = await getContent(email, regex, type);

  console.log(`123123123123123123123123123123`);

  if (content) {
    console.log(`提取的邮件内容: ${content}`);
  } else {
    console.log('未能提取到符合条件的内容');
  }
};

// 启动主程序
main();