我正在尝试创建一个电子邮件发送所有数据的联系表单。我有各种问题与PHP等,所以我决定替代谷歌MailApp。 当我想用该电子邮件附加文件时,问题就出现了。 当我运行表单将数据发送到脚本时,我收到一个错误,这表明该文件不能作为参数在事件中发送。 这是mailApp脚本:
var TO_ADDRESS = "someone@gmail.com"; // change this ...
function doGet(e){
doPost(e);
}
function formatMailBody(obj) { // function to spit out all the keys/values from the form in HTML
var result = "";
for (var key in obj) { // loop over the object passed to the function
result += "<h4 style='text-transform: capitalize; margin-bottom: 0'>" + key + "</h4><div>" + obj[key] + "</div>";
// for every key, concatenate an `<h4 />`/`<div />` pairing of the key name and its value,
// and append it to the `result` string created at the start.
}
return result; // once the looping is done, `result` will be one long string to put in the email body
}
function doPost(e) {
try {
Logger.log(e); // the Google Script version of console.log see: Class Logger
record_data(e);
var mailData = e.parameters; // just create a slightly nicer variable name for the data
var blob = e.parameter.fileToUpload;
MailApp.sendEmail({
to: TO_ADDRESS,
subject: "Contact form submitted",
htmlBody: formatMailBody(mailData),
attachments: [blob]
});
return ContentService // return json success results
.createTextOutput(
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}
/**
* record_data inserts the data received from the html form submission
* e is the data received from the POST
*/
function record_data(e) {
Logger.log(JSON.stringify(e)); // log the POST data in case we need to debug it
try {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName('responses'); // select the responses sheet
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow()+1; // get next row
var row = [ new Date() ]; // first element in the row should always be a timestamp
// loop through the header columns
for (var i = 1; i < headers.length; i++) { // start at 1 to avoid Timestamp column
if(headers[i].length > 0) {
row.push(e.parameter[headers[i]]); // add data to row
}
}
// more efficient to set values as [][] array than individually
sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
}
catch(error) {
Logger.log(e);
}
finally {
return;
}
}
这里也是发送信息的表单:
<form name ="frm"
method ="post"
action="https://script.google.com/macros/s/AKfycbwnz1pa4p8tzf5wPqdlvgy7YVU1qPWV_ZQvX84ecSetjMX8pL8/exec"
enctype="multipart/form-data"
>
<table>
<tr>
<td><p align="right" style="width:90%;"><input name="email" type="email" required placeholder="your.name@email.com" class="form-control"></p></td>
<td>email</td>
</tr>
<tr>
<td><p align="right" style="width:90%;"><input name="subject" type="text" placeholder="subject" class="form-control"></p></td>
<td>subject</td>
</tr>
<tr>
<td><p align="right" style="width:90%;"><textarea rows="15" cols="40" name="comment" placeholder="write the message" class="form-control" ></textarea></p></td>
<td>comment</td>
</tr>
<tr>
<td><input type="file" name="fileToUpload" id="fileToUpload"/></td>
<td>file</td>
</tr>
<tr>
<td><input type ="submit" name="submit" class="btn btn-primary" value ="send"/></td>
<td><input type ="reset" class="btn btn-primary" value ="reset" /></td>
</tr>
</table>
</form>
我真的很感激任何帮助,因为这对我来说是一个全新的事情。