我正在尝试使用带有 smtplib 和 imaplib 的 python cgi 编写我自己的 webmail 应用程序。但是,当我点击“发送”表单上的发送按钮时,会发送电子邮件,之后每次刷新都会再次发送消息。页面(cgi 脚本)通过 javascript 每两分钟自动刷新一次:
<script language="javascript">
window.setInterval('refresh()', 180000);
function refresh()
{
// this checks to see which tab is selected, page is refreshed only if compose tab is not selected
elList = document.getElementsByTagName("a");
for (i = 0; i < elList.length; i++)
{
if (elList[i].className === 'tab active')
{
var activeTab = elList[i].innerHTML;
}
}
if (activeTab.indexOf("Unread") >= 0)
{
window.location.reload(true);
}
}
</script>
我真的无法判断表单数据是多次发送还是只是多次读取。但是,我认为没有理由在刷新时再次提交表单,所以我不得不相信它只是被多次阅读。以下是能够读取表单数据的组件:
# Create instance of FieldStorage
form = cgi.FieldStorage()
if form.keys() != []:
for x in form.keys():
if x == 'send':
send_email()
我尝试在页面头标记中使用 javascript 重置表单,但这不起作用:
<script language="javascript">
Form.reset('send'); // reset send form so email is not sent again
</script>
为了完整起见,这里是表单定义:
<form name="send" action="/cgi-bin/myemail.py" method="post" >
<table id="example" class="compose" >
<tr>
<td><input type="submit" value="To:" style="height: 1.95em; width: 4em"></td><td><input type="text" name="send_to" size="90"></td><td><input type="submit" name="send" value="SEND" style="height: 1.95em; width: 12em"></td>
</tr>
<tr>
<td><input type="submit" value="Cc:" style="height: 1.95em; width: 4em"></td><td><input type="text" name="send_cc" size="90"></td> <td><input type="submit" name="send" value="SAVE" style="height: 1.95em; width: 12em"></td>
</tr>
<tr>
<td><input type="submit" value="Bcc:" style="height: 1.95em; width: 4em"></td><td><input type="text" name="send_bcc" size="90"></td> <td><input type="submit" name="send" value="DELETE" style="height: 1.95em; width: 12em"></td>
</tr>
<tr>
<td><input type="submit" value="Subj:" style="height: 1.95em; width: 4em"></td><td><input type="text" name="send_subj" size="90"></td> <td><input type="submit" name="send" size="200" value="ATTACH" style="height: 1.95em; width: 12em"></td>
</tr>
<tr>
<td colspan="3">
<!-- <td colspan="3"> <div id="result" class="body" style="background-color:#000000"></div></td> -->
<textarea name="send_body" cols="118" rows="23"> </textarea>
</td>
</tr>
</table>
</form>
如何防止此表单数据被多次读取?谢谢。