我想创建一个HTML文件,该文件使用javascript函数来获取HTML输入并将它们发送到预定的电子邮件地址。
到目前为止,我有这个,但我的sendMail功能不起作用,我不确定如何修复它。
<!DOCTYPE html>
<html>
<body>
<p>Complete fields and click submit:</p>
<form id="from">
Contacts First name: <input type="string" id="fname"><br>
Contacts Last name: <input type="string" id="lname"><br>
Contacts Phone Number: <input type="string" id="phoneNum"><br>
E-mail Address: <input type ="string" id="email"><br>
Description: <input type="string" id="desc"<br>
Attachments: <input type="file" id="fileupload" accept="image/*"<br><br>
<input type="button" onclick="sendMail()" value="Submit">
</form>
</body>
</html>
<script type="text/javascript">
function sendMail() {
var message = document.getElementById("fname").value()
+ document.getElementById("lname").value()
+ document.getElementById("phoneNum").value()
+ document.getElementById("email").value()
+ document.getElementById("desc").value()
+ document.getElementById("fileupload").value();
var subject = "email address";
window.location.href = "mailtto:email address?subject=subject&body=message;";
}
</script>
</body>
</html>
答案 0 :(得分:0)
变量不会在字符串内扩展(除非您使用ES6模板字符串)。你需要连接:
var address = document.getElementById("email").value;
window.location.href = "mailto:yourAddress?subject=" + encodeURIComponent(subject) + "&body=" + encodeURIComponent(message);
顺便说一句,这不会在电子邮件中附加文件。