我使用Google Spreadsheets的内置表单功能在我的网站上构建联系表单。
现在,请考虑以下代码:
function sendFormByEmail(e)
{
var email = "team@example.com";
subject = e.namedValues["Subject"].toString();
message = "Time: " + e.namedValues["Timestamp"].toString() + "\n\n"
+ "Name: " + e.namedValues["Name"].toString() + "\n\n"
+ "Email: " + e.namedValues["Email Address"].toString() + "\n\n"
+ "Website: " + e.namedValues["Website"].toString() + "\n\n"
+ "Reason For Contacting?: " + e.namedValues["Reason For Contacting?"].toString() + "\n\n"
+ "Message: " + e.namedValues["Message"].toString() + "\n\n";
MailApp.sendEmail(email, subject, message);
}
一旦有人提交表单,我确保收到电子邮件,电子邮件的正文包含信息。像这样(例子):
时间:2012/02/25 11:53
姓名:John Davis
电子邮件:John@google.com
联系原因?:只想和你聊聊
消息:已经很久了。今晚抓住我。
所以,现在你应该清楚地了解代码的作用。问题是,我希望输出看起来像这样(即粗体一些文字):
时间: 2012/02/25 11:53
姓名: John Davis
电子邮件: John@google.com
联系原因?:只是想和你聊聊
消息:已经很久了。今晚抓住我。
如何修改代码才能实现此目的?感谢。
答案 0 :(得分:11)
MailApp.sendEmail可以将htmlBody作为advancedArgs。这里描述http://code.google.com/googleapps/appsscript/class_mailapp.html
你可以像
一样发送htmlBodyfunction sendFormByEmail(e) {
var email = "team@example.com";
var subject = e.namedValues["Subject"].toString();
var msgHtml = "<b>Time</b>: " + e.namedValues["Timestamp"].toString() + "<br/>"
+ "<b>Name:</b> " + e.namedValues["Name"].toString() + "<br/>"
+ "<b>Email:</b> " + e.namedValues["Email Address"].toString() + "<br/>"
+ "<b>Website:</b> " + e.namedValues["Website"].toString() + "<br/>"
+ "<b>Reason For Contacting?:</b> " + e.namedValues["Reason For Contacting?"].toString() + "<br/>"
+ "<b>Message:</b> " + e.namedValues["Message"].toString() + "<br/>";
var msgPlain = msgHtml.replace(/\<br\/\>/gi, '\n').replace(/(<([^>]+)>)/ig, ""); // clear html tags and convert br to new lines for plain mail
MailApp.sendEmail(email, subject, msgPlain, { htmlBody: msgHtml });
}
以上是针对换行的。用它来分隔段落:
function sendFormByEmail(e) {
var email = "team@example.com";
var subject = e.namedValues["Subject"].toString();
var msgHtml = "<p>" + "<b>Time</b>: " + e.namedValues["Timestamp"].toString() + "</p>"
+ "<p>" + "<b>Name:</b> " + e.namedValues["Name"].toString() + "</p>"
+ "<p>" + "<b>Email:</b> " + e.namedValues["Email Address"].toString() + "</p>"
+ "<p>" + "<b>Website:</b> " + e.namedValues["Website"].toString() + "</p>"
+ "<p>" + "<b>Reason For Contacting?:</b> " + e.namedValues["Reason For Contacting?"].toString() + "</p>"
+ "<p>" + "<b>Message:</b> " + e.namedValues["Message"].toString() + "</p>";
var msgPlain = msgHtml.replace(/(<([^>]+)>)/ig, ""); // clear html tags for plain mail
MailApp.sendEmail(email, subject, msgPlain, { htmlBody: msgHtml });
}
我没有尝试,但它应该有用。
答案 1 :(得分:0)
answer提供的Abe.S比较简单。我将其与arunes' edited answer的一部分结合在一起。现在以its_me在他们对arunes的评论中要求的方式编写:
function inlineImage() {
MailApp.sendEmail({
to: "team@example.com",
subject: e.namedValues["Subject"].toString(),
htmlBody: "<b>Time</b>: " + e.namedValues["Timestamp"].toString() + "<br/>"
+ "<b>Name:</b> " + e.namedValues["Name"].toString() + "<br/>"
+ "<b>Email:</b> " + e.namedValues["Email Address"].toString() + "<br/>"
+ "<b>Website:</b> " + e.namedValues["Website"].toString() + "<br/>"
+ "<b>Reason For Contacting?:</b> " + e.namedValues["Reason For Contacting?"].toString() + "<br/>"
+ "<b>Message:</b> " + e.namedValues["Message"].toString() + "<br/>",
});
}
我使用自己的变量代替了我的应用程序的所有“ e.namedValues [...””对其进行了测试,并且它可以正常工作。我仍然是一个新手,所以我不确定为什么将“ msgPlain”替换为“ msgHtml”会更好。
顺便说一句,我试图将其写为评论,但我的观点不够。尽管我想我写的从技术上来说是its_me最初正在寻找的答案。 非常感谢Abe.S和arunes教给我有关这两个脚本的信息。
答案 2 :(得分:-1)
一种对我有用的简单方法。
function inlineImage() {
MailApp.sendEmail({
to: "Your@email.com",
subject: "Subject",
htmlBody: '<a href="http://google.com"> <b>Google</b></a> ' ,
});
}