Google Apps Script中HTML网格布局样式在MailApp中无法正常渲染的问题排查求助
Hey there! The core issues you're facing stem from two big limitations of email clients: poor support for modern CSS features like Grid, and strict filtering of styles in <head> tags. Let's break this down step by step:
1. Why CSS Grid Fails in Emails
Nearly all major email clients (Gmail, Outlook, Yahoo, etc.) don't support CSS Grid layout. Email rendering engines rely on outdated HTML/CSS standards, and modern layout tools like Grid (especially syntax like repeat(auto-fit, minmax(...))) are almost universally ignored. That's why your grid setup looks broken no matter how you tweak the styles.
2. Why Your <style> Tag Styles Aren't Working
- Clients like Gmail strip out the entire
<head>section of emails, so any styles defined there get deleted entirely. - Even if a client keeps the
<style>tag, it often restricts which selectors and properties can be used—class selectors like.appmight be ignored in favor of inline styles or very basic global rules.
3. The Fix: Use Table Layout (Email Development Standard)
The only reliable way to build layouts that work across all email clients is to use HTML tables. Tables have been supported in emails for decades, so they'll render consistently everywhere. Here's a table-based version of your grid layout that matches your desired tile structure:
Updated test.html Code
<!DOCTYPE html> <html> <head> <base target="_top"> <!-- Inline styles are safest, but we can add a small style block for basic reusable styles (some clients will respect this) --> <style> .item { padding: 12px; border: 1px solid #e0e0e0; background-color: #f9f9f9; } </style> </head> <body> <!-- Outer table ensures full-width container --> <table width="100%" cellpadding="0" cellspacing="0" border="0"> <tr> <td align="center"> <!-- Inner table controls max width to prevent overly wide layouts --> <table width="600" cellpadding="0" cellspacing="25" border="0"> <!-- Row 1 --> <tr> <td class="item">item 1</td> <td class="item">item 2</td> <td class="item">item 3</td> </tr> <!-- Row 2 --> <tr> <td class="item">item 4</td> <td class="item">item 5</td> <td class="item">item 6</td> </tr> <!-- Row 3 --> <tr> <td class="item">item 7</td> <td class="item">item 8</td> <td class="item">item 9</td> </tr> <!-- Row 4 --> <tr> <td class="item">item 10</td> <td class="item">item 11</td> <td> </td> <!-- Empty cell to fill the row --> </tr> </table> </td> </tr> </table> </body> </html>
Optional: Add Responsiveness (Mobile-Friendly)
If you want the grid to collapse into a single column on mobile, you can add a media query (note: Outlook desktop doesn't support media queries, but most mobile clients do):
<style> @media only screen and (max-width: 600px) { table[width="600"] tr td { display: block !important; width: 100% !important; margin-bottom: 25px !important; } } </style>
4. A Quick Note on Your Code.gs
The setSandboxMode(HtmlService.SandboxMode.IFRAME) setting doesn't affect email rendering at all—this only applies when using HtmlService to serve web apps. You can safely remove it to clean up your code:
var message = templ.evaluate().setTitle('Weekly Performance Review').getContent();
内容的提问来源于stack exchange,提问作者João Rafael Borowski Tedeschi

