求助:在Google Sheets邮件App Script指定位置插入图片
Fixing Inline Image Embedding with MailApp
Hey there! I’ve run into this exact snag before—when using Google Apps Script’s MailApp, just dropping an <img> tag with a direct URL doesn’t always work reliably, and inline images need a specific setup using content IDs (CID). Let’s get that logo placed exactly where you want it: right after "Kathy McKeown" and before the nebigdatahub.org link.
Here’s a working code example that fixes this issue:
function sendEmailWithEmbeddedLogo() { // 1. Fetch the logo and convert it to a blob with a unique content ID const logoUrl = "https://industry.datascience.columbia.edu/sites/default/files/images/NSF-NORTHEAST-BIG-DATA-INDUSTRY-LOGO.png"; const logoBlob = UrlFetchApp.fetch(logoUrl) .getBlob() .setName("ne-big-data-logo.png") .setContentId("ne-big-data-logo"); // This CID links the blob to the HTML image // 2. Build your email HTML with the logo in the correct position const emailHtml = ` <div style="font-family: Arial, sans-serif;"> <!-- Your existing email content goes here --> <p>Kathy McKeown</p> <!-- Inline logo referenced via CID --> <img src="cid:ne-big-data-logo" alt="NSF Northeast Big Data Industry Logo" style="margin: 12px 0; max-width: 320px;" /> <p>Visit our site at <a href="https://nebigdatahub.org">nebigdatahub.org</a></p> <!-- Rest of your email content --> </div> `; // 3. Send the email with MailApp, including the embedded image blob MailApp.sendEmail({ to: "recipient@example.com", // Replace with your actual recipient subject: "Your Email Subject Line", htmlBody: emailHtml, attachments: [logoBlob] // The CID-marked blob renders as inline, not a separate attachment }); }
Critical details to make this work:
- Content ID Match: The
setContentId()value on the blob must exactly match thecid:reference in the<img>tag. This tells MailApp to embed the image directly in the email body instead of attaching it separately. - Blob Reliability: Using
UrlFetchAppto pull the image and convert it to a blob ensures email clients won’t block the image due to external URL restrictions. - Positioning: The logo is placed directly between "Kathy McKeown" and the website link, just as you requested.
If you were previously using a plain text body or didn’t set up the CID correctly, that’s almost certainly why your inline image failed to show up. This method works consistently across most major email clients.
内容的提问来源于stack exchange,提问作者Katherine Malave Brasa
相关产品推荐
相关产品推荐

