使用外部HTML文件的电子邮件地址通过谷歌应用脚本发送电子邮件 [英] Using email address from external HTML file to send email via google apps script
问题描述
我在Google Apps中创建了一个功能,我需要通过电子邮件将我正在处理的电子表格作为PDF进行发送。用户应该点击一个按钮,并打开一个对话框,他们可以输入电子邮件地址,主题和消息,并从那里发送电子邮件。
我创建了电子邮件对话作为一个侧边栏与以下HTML:
<!DOCTYPE html>
< html>
< head>
< link rel =stylesheethref =https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css>
< base target =_ top>
< / head>
< body>
< form>
< div class =form-group>
< label for =exampleInputEmail1>电子邮件地址< / label>
< input type =emailclass =form-controlid =exampleInputEmail1ar。 ia-describedby =emailHelpvalue =d.bage@group-miki.com>
< / div>
< div class =form-group>
< label for =subject> Subject< / label>
< input type =inputclass =form-controlid =subjectvalue =合约续订 - 三木旅游>
< / div>
< div class =form-group>
< label for =subject> Message< / label>
< / div>
< button type =submitclass =btn btn-primary>提交< / button>
< / form>
< / body>
< / html>
但是,当我编写脚本发送电子邮件时,如何存储这里的电子邮箱是用来发送电子邮件的变量吗?这同样适用于主题和消息本身。
<1>为输入定义名称属性。目前,您的任何表单字段都没有名称属性。以下是它应该是什么样子的例子
< input type =emailname =emailclass =form -controlid =exampleInputEmail1aria-describeby =emailHelpvalue =d.bage@group-miki.com>
这将允许您在提交表单时访问表单对象的属性(例如,表单.email,form.subject等);
2)在客户端(html),你必须编写代码来处理'submit'事件。在结束< / body>标记之前添加以下内容:
< script>
window.onload = function(){
document.getElementsByTagName('form')[0]
.addEventListener( 'submit',函数(e){
e.preventDefault(); //拦截重定向到另一页
google.script.run.handleFormSubmit(this); //传递表单对象到服务器端函数。
});
}
< / script>
您也可以使用jQuery来获得相同的结果。 使用google.script.run进行客户端 - 服务器通信。有关详情,请参阅文档 https://developers.google.com/apps-脚本/指南/ html /通信 I'm creating a function within Google Apps whereby I need to email the spreadsheet I am working on as a PDF. The user should click on a button and a dialogue box should open whereby they can enter email address, subject and message and send the email from there. I've created the email dialogue as a sidebar with the following HTML: However, when I'm writing the script to send the email, how do I store the content of the email box here as variable to use for sending the email? The same will apply obviously to the subject and the message itself. 1) Define name properties for your inputs. Currently, none of your form fields has the 'name' property. Here's the example of how it should look like This will allow you to access the properties of the form object when the form is submitted (e.g., form.email, form.subject, etc); 2) On the client-side (html), you must write the code to handle the 'submit' event. Add the following before the closing </body> tag: You can also use jQuery to achieve the same result.
Here's my server-side (.gs) function that logs the contents of the input fields Use google.script.run for client-server communication. See the docs for more details https://developers.google.com/apps-script/guides/html/communication 这篇关于使用外部HTML文件的电子邮件地址通过谷歌应用脚本发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
这是我的服务器端(.gs)函数,用于记录输入字段的内容。
$ p $ function handleFormSubmit(form ){
Logger.log(form.email);
Logger.log(form.subject);
Logger.log(form.message);
$ / code> <!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
<base target="_top">
</head>
<body>
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" ar. ia-describedby="emailHelp" value="d.bage@group-miki.com">
</div>
<div class="form-group">
<label for="subject">Subject</label>
<input type="input" class="form-control" id="subject" value="Contract renewal - Miki Travel">
</div>
<div class="form-group">
<label for="subject">Message</label>
<textarea class="form-control" rows="12" id="text"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</body>
</html>
<input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="d.bage@group-miki.com">
<script>
window.onload = function() {
document.getElementsByTagName('form')[0]
.addEventListener('submit', function(e){
e.preventDefault(); // intercept redirect to another page
google.script.run.handleFormSubmit(this); // pass the form object to the server-side function.
});
}
</script>
function handleFormSubmit(form) {
Logger.log(form.email);
Logger.log(form.subject);
Logger.log(form.message);
}