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

技术问询:如何实现Gmail邮件自动部分转发仅提取链接

Got it, this is totally doable with Google Apps Script—here's a step-by-step guide to make this work exactly how you want:

Step 1: Open Google Apps Script

You can access it two easy ways:

  • From Gmail: Click the gear icon → Settings → See all settings → Advanced, scroll down to "Google Apps Script" and click the link.
  • Or just search for "Google Apps Script" in your browser, then log in with the same Google account you use for Gmail.
    Once there, click "New Project" to create a blank script file.
Step 2: Paste the Custom Script

Replace the default myFunction() code with this script—make sure to update the placeholder values to match your needs:

function autoForwardOnlyLink() {
  // 1. 先设置搜索条件,确保只处理你每天收到的那封特定邮件
  // 比如指定发件人、主题,避免误处理其他邮件
  const searchQuery = "from:daily-alert@example.com subject:每日更新"; // 替换成你的实际发件人/主题
  const threads = GmailApp.search(searchQuery, 0, 1); // 只取最新的1封,防止重复处理

  if (threads.length === 0) return; // 没找到目标邮件就直接退出

  const thread = threads[0];
  const latestMessage = thread.getMessages()[thread.getMessageCount() - 1];
  const emailBody = latestMessage.getBody();

  // 2. 提取邮件里的链接,这里用正则匹配常见的URL格式
  const urlPattern = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g;
  const foundLinks = emailBody.match(urlPattern);

  if (!foundLinks || foundLinks.length === 0) return; // 没找到链接就退出

  // 取第一个匹配到的链接,如果需要多个可以调整逻辑
  const targetLink = foundLinks[0];

  // 3. 设置转发的收件人、主题和内容
  const forwardRecipient = "your-target-email@example.com"; // 替换成要转发的邮箱
  const forwardSubject = "每日提取的链接";
  const forwardContent = `提取到的链接:\n${targetLink}`;

  // 发送邮件,完全自定义内容,不会带原邮件的发件人信息
  GmailApp.sendEmail(forwardRecipient, forwardSubject, forwardContent);

  // 可选:标记原邮件为已读,避免重复触发处理
  thread.markRead();
}
Step 3: Set Up a Trigger to Run Automatically

You need to tell Google when to run this script—here's how:

  • In the script editor, click the clock icon (Triggers) in the left sidebar.
  • Click Add Trigger in the bottom right corner.
  • Configure the trigger settings:
    • Choose autoForwardOnlyLink as the function to run.
    • For event source, pick Time-driven if you want it to run at a specific time every day (since you get the email daily), or Gmail > New email received if you want it to trigger right when the email arrives.
    • Adjust the frequency (e.g., "Day timer" > choose a time window when you expect the email to arrive) and save.
Step 4: Test It Out

Before relying on it, run the script manually once to make sure it works:

  • Click the run button (▶️) in the script editor. You'll need to grant permissions the first time—follow the prompts to allow the script to access your Gmail.
  • Check the recipient email inbox—you should see a new email with just the extracted link, no original sender info.
Quick Notes
  • If your email has multiple links, you can modify the script to pick a specific one (e.g., check for links containing a certain keyword) or include all of them.
  • The regex for links works for most standard URLs, but if your link has unusual formatting, you might need to tweak the pattern a bit.
  • If you change the search query later, make sure to update it in the script to keep targeting the right email.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:18:55