如何在自提交表单上使用 mailto?
How to use mailto on a self-submit form?
我必须对现有代码进行修改,而且我正在努力使用 mailto 函数。我希望当我提交我的表单时,预填充的邮件会打开 Outlook(或配置的任何其他邮件程序),以便用户可以检查是否一切正常,然后提交带有表单数据的邮件。
我想使用 mailto:
因为目前该页面(在本地服务器上,而不是在 Internet 上)只能在 IE 上运行(最后一个开发人员使用 ActiveX 数据对象,但它不起作用在 Edge/Chrome/Firefox).
我已经尝试将 mailto:
放入表单的 action
标记中,但没有用,我也尝试了一些 javascript 并且发生了同样的情况。此外,我的表单使用 $_SERVER['PHP_SELF']
全局变量提交给自己。
form/code篇幅比较大,所以我只把可能对你有用的部分放上来帮助我。
这是我使用的表格:
echo "<FORM action=". $_SERVER['PHP_SELF'] . " name=\"form\" method=\"POST\">";
"my form here"
echo "<input type=\"submit\" name=\"Envoyer\" value=\"Send\"/>";
echo "</FORM>";
然后,我想从表单中获取数据,并在用户点击提交按钮时打开一个邮件程序,其中包含一封预填充的邮件,其中包含数据。
这是我用的:
echo "<script>
var email = $maildest;
var subject = \"Request of the user\";
var emailBody = $MAIL;
window.location = \"mailto:\"+email+\"?subject=\"+subject+\"&body=\"+emailBody;";
echo "</script>";
echo "An OUTLOOK window opened with the informations you entered. <br/>Send the email so we can take it into account.<br/>
exit(0);
我没有收到任何错误消息,页面工作正常,但是当我点击提交按钮时没有任何反应,甚至邮件程序打开 window 或其他任何东西也没有。我确定我缺少某些东西,而且我对 php/html/javascript 有点生疏,所以我来这里寻求你的帮助。
如果你不回显内容,它会更容易阅读
如果这样做,您需要连接 PHP 样式并使用 JavaScript
的反引号
注意:您需要为带有 ${} javascript 变量的行加上单引号,否则 $ 被解释为:
echo "<script>
var email = \"".$maildest."\";
var subject = encodeURIComponent(\"Request of the user\");
var emailBody = encodeURIComponent(\`".$MAIL."\`);"
echo 'window.location = `mailto:${email}?subject=${subject}&body=${emailBody}`;';
echo "</script>";
请注意 $MAIL 和最后一行的反引号,因为那里可能有换行符
你还需要转义空格和特殊字符
查看控制台 - mailto 通常作为表单操作效果更好
没有回显:
?>
An OUTLOOK window opened with the informations you entered. <br/>Send the email so we can take it into account.<br/>
<script>
var email = "<?= $maildest ?>";
var subject = encodeURIComponent("Request of the user");
var emailBody = encodeURIComponent(`<?= $MAIL ?>`);
window.location = `mailto:${email}?subject=${subject}&body=${emailBody}`;
</script>
<?
exit(0);
?>
这里是不使用服务器的版本:
window.addEventListener("load", function() {
document.getElementById("form").addEventListener("submit", function(e) {
e.preventDefault();
var email = this.email.value,
subject = encodeURIComponent("Request of the user"),
elements = this.elements,
emailTexts = [];
[...elements].forEach(function(ele) {
if (ele.type!="submit") emailTexts.push(ele.name + ":" + ele.value)
})
var emailBody = encodeURIComponent(emailTexts.join("\n"));
var loc = `mailto:${email}?subject=${subject}&body=${emailBody}`;
console.log(loc)
window.location = loc;
});
});
An OUTLOOK window will open with the informations you enter. <br/>Send the email so we can take it into account.<br/>
<form id="form" ....>
Email: <input type="text" name="email" value="" /><br/> Name: <input type="text" name="Name" value="" /><br/> Comment: <textarea name="Comment"></textarea><br/>
<input type="submit" />
</form>
您必须在表单上设置事件,因为您的 JS 代码只在页面呈现时起作用,而不是在表单提交时起作用。
<form action=". $_SERVER['PHP_SELF'] . " name="form" method="POST" onsubmit="sendMail">
// Html form here
</form>
<script>
function sendMail(event) {
// This method will stop default action of form's submit
event.preventDefault();
var email = "<?= $maildest ?>";
var subject = encodeURIComponent("Request of the user");
var emailBody = encodeURIComponent(`<?= $MAIL ?>`);
window.location = `mailto:${email}?subject=${subject}&body=${emailBody}`;
}
</script>
我必须对现有代码进行修改,而且我正在努力使用 mailto 函数。我希望当我提交我的表单时,预填充的邮件会打开 Outlook(或配置的任何其他邮件程序),以便用户可以检查是否一切正常,然后提交带有表单数据的邮件。
我想使用 mailto:
因为目前该页面(在本地服务器上,而不是在 Internet 上)只能在 IE 上运行(最后一个开发人员使用 ActiveX 数据对象,但它不起作用在 Edge/Chrome/Firefox).
我已经尝试将 mailto:
放入表单的 action
标记中,但没有用,我也尝试了一些 javascript 并且发生了同样的情况。此外,我的表单使用 $_SERVER['PHP_SELF']
全局变量提交给自己。
form/code篇幅比较大,所以我只把可能对你有用的部分放上来帮助我。
这是我使用的表格:
echo "<FORM action=". $_SERVER['PHP_SELF'] . " name=\"form\" method=\"POST\">";
"my form here"
echo "<input type=\"submit\" name=\"Envoyer\" value=\"Send\"/>";
echo "</FORM>";
然后,我想从表单中获取数据,并在用户点击提交按钮时打开一个邮件程序,其中包含一封预填充的邮件,其中包含数据。
这是我用的:
echo "<script>
var email = $maildest;
var subject = \"Request of the user\";
var emailBody = $MAIL;
window.location = \"mailto:\"+email+\"?subject=\"+subject+\"&body=\"+emailBody;";
echo "</script>";
echo "An OUTLOOK window opened with the informations you entered. <br/>Send the email so we can take it into account.<br/>
exit(0);
我没有收到任何错误消息,页面工作正常,但是当我点击提交按钮时没有任何反应,甚至邮件程序打开 window 或其他任何东西也没有。我确定我缺少某些东西,而且我对 php/html/javascript 有点生疏,所以我来这里寻求你的帮助。
如果你不回显内容,它会更容易阅读
如果这样做,您需要连接 PHP 样式并使用 JavaScript
的反引号
注意:您需要为带有 ${} javascript 变量的行加上单引号,否则 $ 被解释为:
echo "<script>
var email = \"".$maildest."\";
var subject = encodeURIComponent(\"Request of the user\");
var emailBody = encodeURIComponent(\`".$MAIL."\`);"
echo 'window.location = `mailto:${email}?subject=${subject}&body=${emailBody}`;';
echo "</script>";
请注意 $MAIL 和最后一行的反引号,因为那里可能有换行符
你还需要转义空格和特殊字符
查看控制台 - mailto 通常作为表单操作效果更好
没有回显:
?>
An OUTLOOK window opened with the informations you entered. <br/>Send the email so we can take it into account.<br/>
<script>
var email = "<?= $maildest ?>";
var subject = encodeURIComponent("Request of the user");
var emailBody = encodeURIComponent(`<?= $MAIL ?>`);
window.location = `mailto:${email}?subject=${subject}&body=${emailBody}`;
</script>
<?
exit(0);
?>
这里是不使用服务器的版本:
window.addEventListener("load", function() {
document.getElementById("form").addEventListener("submit", function(e) {
e.preventDefault();
var email = this.email.value,
subject = encodeURIComponent("Request of the user"),
elements = this.elements,
emailTexts = [];
[...elements].forEach(function(ele) {
if (ele.type!="submit") emailTexts.push(ele.name + ":" + ele.value)
})
var emailBody = encodeURIComponent(emailTexts.join("\n"));
var loc = `mailto:${email}?subject=${subject}&body=${emailBody}`;
console.log(loc)
window.location = loc;
});
});
An OUTLOOK window will open with the informations you enter. <br/>Send the email so we can take it into account.<br/>
<form id="form" ....>
Email: <input type="text" name="email" value="" /><br/> Name: <input type="text" name="Name" value="" /><br/> Comment: <textarea name="Comment"></textarea><br/>
<input type="submit" />
</form>
您必须在表单上设置事件,因为您的 JS 代码只在页面呈现时起作用,而不是在表单提交时起作用。
<form action=". $_SERVER['PHP_SELF'] . " name="form" method="POST" onsubmit="sendMail">
// Html form here
</form>
<script>
function sendMail(event) {
// This method will stop default action of form's submit
event.preventDefault();
var email = "<?= $maildest ?>";
var subject = encodeURIComponent("Request of the user");
var emailBody = encodeURIComponent(`<?= $MAIL ?>`);
window.location = `mailto:${email}?subject=${subject}&body=${emailBody}`;
}
</script>