prepare_and_send.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. 邮件发送入口
  3. 调用同目录下的 dealer-email-outreach 脚本
  4. """
  5. import subprocess
  6. import sys
  7. from pathlib import Path
  8. from typing import Dict, Any, Optional
  9. def _get_script_dir() -> Path:
  10. return Path(__file__).parent
  11. def _get_skill_dir() -> Path:
  12. return _get_script_dir().parent.parent
  13. def _get_asset(name: str) -> Path:
  14. return _get_skill_dir() / "assets" / name
  15. def prepare_emails(
  16. excel_path: str,
  17. sheet_name: str,
  18. filter_str: str,
  19. subject: str,
  20. template_name: str = "email_template.md",
  21. output_path: str = "outreach_preview.json"
  22. ) -> str:
  23. """生成邮件预览 JSON"""
  24. script = _get_script_dir() / "prepare_outreach_emails.py"
  25. template = _get_asset(template_name)
  26. if not script.exists():
  27. raise FileNotFoundError(f"未找到脚本: {script}")
  28. if not template.exists():
  29. raise FileNotFoundError(f"未找到模板: {template}")
  30. cmd = [
  31. sys.executable,
  32. str(script),
  33. "--excel", excel_path,
  34. "--sheet", sheet_name,
  35. "--filter", filter_str,
  36. "--template", str(template),
  37. "--subject", subject,
  38. "--output", output_path,
  39. ]
  40. result = subprocess.run(cmd, capture_output=True, text=True)
  41. if result.returncode != 0:
  42. raise RuntimeError(f"生成邮件预览失败: {result.stderr}")
  43. return output_path
  44. def send_emails(
  45. preview_path: str,
  46. sender: str,
  47. auth_code: str,
  48. smtp_host: str = "smtp.qq.com",
  49. smtp_port: int = 465
  50. ) -> Dict[str, Any]:
  51. """发送邮件"""
  52. script = _get_script_dir() / "send_outreach_emails.py"
  53. if not script.exists():
  54. raise FileNotFoundError(f"未找到脚本: {script}")
  55. cmd = [
  56. sys.executable,
  57. str(script),
  58. "--input", preview_path,
  59. "--smtp-host", smtp_host,
  60. "--smtp-port", str(smtp_port),
  61. "--sender", sender,
  62. "--auth-code", auth_code,
  63. "--confirm-send",
  64. ]
  65. result = subprocess.run(cmd, capture_output=True, text=True)
  66. if result.returncode != 0:
  67. raise RuntimeError(f"发送邮件失败: {result.stderr}")
  68. return {
  69. "stdout": result.stdout,
  70. "stderr": result.stderr,
  71. }
  72. if __name__ == "__main__":
  73. print("邮件模块脚本路径:", _get_script_dir())
  74. print("模板路径:", _get_asset("email_template.md"))