Search code examples
htmlgoogle-apps-scripthtml-emailtemplate-engine

Exception: Malformed HTML content: </div> Error


Im trying to get my Google spreadsheet to send an email with the data from the sheet thats also html formatted. Whenever i try to run it i get I keep getting the following error...

Error Exception: Malformed HTML content: . eval
eval
myFunction @ waiverClaim.gs:37

and i cant find the issue. I assume the issue is with line 37 "const htmlForEmail = htmlTemplate.evaluate().getContent();" but i dont know whats wrong, hopefully someone here can help!

Here is the script im running

function myFunction() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName("Football");

  const h1 = ws.getRange("A1").getValue();
  const subheader = ws.getRange("A4:F4").getValues();
  const priority = subheader[0][0];
  const player = subheader[0][1];
  const transaction = subheader[0][2];
  const bid = subheader[0][3];
  const years = subheader[0][4]; 
  const dropped = subheader[0][5];

  const lr =  ws.getLastRow();

  const tableRangeValues = ws.getRange(5,1,lr-5,6).getValues();

  const totalLine = ws.getRange(lr,3,1,6).getValues();
  const totalText = totalLine[0][0];
  const totalSum = totalLine[0][1];

const htmlTemplate = HtmlService.createTemplateFromFile("email");

htmlTemplate.h1 = h1;
htmlTemplate.subheader = subheader;
htmlTemplate.priority = priority;
htmlTemplate.player = player;
htmlTemplate.transaction = transaction;
htmlTemplate.bid = bid;
htmlTemplate.years = years;
htmlTemplate.dropped = dropped;
htmlTemplate.totalText = totalText;
htmlTemplate.totalSum = totalSum;
htmlTemplate.tableRangeValues = tableRangeValues;


const htmlForEmail = htmlTemplate.evaluate().getContent();

console.log(htmlForEmail);

GmailApp.sendEmail(
  "me@gmail.com",
  "Waiver Claim Submission",
  "Tom Brady $45 1 year",
  { htmlBody: htmlForEmail }
);

}

and the corresponding html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
       </head>
        <body>
             <div>

                <div>
                      <h1><?= h1 ?></h1>
                </div>
                      
                <div> 
                      <div <?= subheader ?></div>
                </div>

              </div> 

        <table>
              <thead> 
                        <tr> 
                            <th><?= priority ?></th>
                            <th><?= player ?></th>
                            <th><?= transaction ?></th>
                            <th><?= bid ?></th>
                            <th><?= years ?></th>
                            <th><?= dropped ?></th>
                        </tr>  
                
               </thead>
       
               <tbody> 
                        <tr> 
                            <td>Col D1</td>
                            <td>Col D2</td>
                            <td>Col D3</td>
                            <td>Col D4</td>
                            <td>Col D5</td>
                            <td>Col D6</td>
                        </tr>  
              </tbody>


                <tfoot>
                        <tr>
                            <td></td>
                            <td></td>
                            <td><?= totalText ?></td>
                            <td><?= totalSum ?></td>
                            <td></td>
                            <td></td>     
                        </tr> 
                </tfoot>
          </table>    
     </body>
</html>

If i need to offer any more information on the situation i would be glad to.


Solution

  • You missed > in <div <?= subheader ?></div>.

    Try with this:

    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
           </head>
            <body>
                 <div>
    
                    <div>
                          <h1><?= h1 ?></h1>
                    </div>
                          
                    <div> 
                          <div> <?= subheader ?></div>
                    </div>
    
                  </div> 
    
            <table>
                  <thead> 
                            <tr> 
                                <th><?= priority ?></th>
                                <th><?= player ?></th>
                                <th><?= transaction ?></th>
                                <th><?= bid ?></th>
                                <th><?= years ?></th>
                                <th><?= dropped ?></th>
                            </tr>  
                    
                   </thead>
           
                   <tbody> 
                            <tr> 
                                <td>Col D1</td>
                                <td>Col D2</td>
                                <td>Col D3</td>
                                <td>Col D4</td>
                                <td>Col D5</td>
                                <td>Col D6</td>
                            </tr>  
                  </tbody>
    
    
                    <tfoot>
                            <tr>
                                <td></td>
                                <td></td>
                                <td><?= totalText ?></td>
                                <td><?= totalSum ?></td>
                                <td></td>
                                <td></td>     
                            </tr> 
                    </tfoot>
              </table>    
         </body>
    </html>