2017-02-10 129 views
0

以下JavaScript代码通过Gmail API成功发送电子邮件。但表格格式,特别是表格和列(单元格)宽度设置,似乎被忽略。使用Gmail API和Javascript发送HTML格式的电子邮件

function sendMail(email) { 
    var to = email; 
    var subject = "HTML formatted email"; 
    var content = ""; 
    content += "<html><body>"; 
    content += "<table width='100%'><tr><td>"; // Outer table 
    content += "<table width='60%'>"; // Nested table 
    content += "<tr><td width='70%'>This is a row</td><td width='30%'>999999</td></tr>"; 
    content += "<tr><td width='70%'>So is this</td><td width='30%'>9999</td></tr>"; 
    content += "</table>"; 
    content += "</td></tr></table>"; 
    content += "</body></html>"; 
    var email = 
      "From: 'me'\r\n" + 
      "To: " + to + "\r\n" + 
      "Subject: " + subject + "\r\n" + 
      "Content-Type: text/html; charset=utf-8\r\n" + 
      "Content-Transfer-Encoding: quoted-printable\r\n\r\n" + 
      content; 
    var base64EncodedEmail = window.btoa(email).replace(/\+/g, "-").replace(/\//g, "_"); 
    var request = gapi.client.gmail.users.messages.send({ 
     "userId": "me", 
     "resource": { 
      "raw": base64EncodedEmail 
     } 
    }); 
    request.execute(); 
} 

的电子邮件消息如下所示:

This is a row 999999 
So is this 9999 

一切我已经上发送HTML格式的邮件研究说,使用嵌套表和内嵌样式。为什么我的宽度样式不起作用?

+0

尝试删除'%'符号。 'width ='400''应该可以做到。 –

回答

2

一番搜索,试验和测试后,我终于想通了,我需要的内容传输编码值设置为base64:

var email = 
     "From: 'me'\r\n" + 
     "To: " + to + "\r\n" + 
     "Subject: " + subject + "\r\n" + 
     "Content-Type: text/html; charset='UTF-8'\r\n" + 
     "Content-Transfer-Encoding: base64\r\n\r\n" + 
     "<html><body>" + 
     content + 
     "</body></html>"; 

希望这会节省你有类似的问题了一段时间。

+0

内容传输编码更改为我工作..并挽救了我的一天 – PHPCoder

相关问题