我正在寻求与Cherrypy一起实施“联系我们”表格并且想知道:有没有一个好的配方(或BSD许可的代码集)我可以使用而不是重新发明轮子?
理想情况下,这将与Cherrpy 3.1兼容。
答案 0 :(得分:3)
好吧,我不得不寻找一个解决方案。这工作(丑陋,没有Javascript验证) - 使用smtplib lib。另外,请注意我为此示例窃取了Jeff的验证码。使用此功能的任何人都需要更改它。
编辑:我添加了验证。
#!/usr/local/bin/python2.4
import smtplib
import cherrypy
class InputExample:
@cherrypy.expose
def index(self):
return "<html><head></head><body><a href="contactus">Contact Us</a></body></html>"
@cherrypy.expose
def contactus(self,message=''):
return """
<html>
<head><title>Contact Us</title>
<script type="text/javascript">
function isNotEmpty(elem)
{
var str = elem.value;
var re = /.+/;
if (!str.match(re))
{
elem.focus();
return false;
}
else
{
return true;
}
}
function isEMailAddr(elem)
{
var str = elem.value;
var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
if (!str.match(re))
{
return false;
}
else
{
return true;
}
}
function validateForm(form)
{
if (isNotEmpty(form.firstName) && isNotEmpty(form.lastName))
{
if (isNotEmpty(form.email))
{
if (isEMailAddr(form.email))
{
if (isNotEmpty(form.captcha))
{
if ( form.captcha.value=='egnaro'.split("").reverse().join(""))
{
if (isNotEmpty(form.subject))
{
alert("All required fields are found. We will respond shortly.");
return true;
}
}
else
{
alert("Please enter the word as displayed in the image.");
return false;
}
}//captcha empty
}
else
{
alert("Please enter a valid email address.");
return false;
} //email
} //email
} //first and last name
alert("Please fill in all required fields.");
return false;
}
</script>
</head>
<body>
<p>%(message)s</p>
<form method='POST' action='contactUsSubmitted' onsubmit='return validateForm(this)'>
<label for="firstName">First Name: </label>
<input type="text" id="firstName" name="firstName" /> (required)<br/>
<label for="lastName">Last Name: </label>
<input type="text" id="lastName" name="lastName" /> (required)<br/>
<label for="email">E-mail address: </label>
<input type="text" id="email" name="email" /> (required)<br/>
<label for="phone">Phone number: </label>
<input type="text" id="phone" name="phone" /> <br/><br/>
<!--THIS NEEDS TO BE CHANGED TO MATCH YOUR OWN CAPTCHA SCHEME!! -->
<label for="captcha">Enter the word<br /><img alt="rhymes with.." src="http://www.codinghorror.com/blog/images/word.png" width="99" height="26" border="0" /></label><br />
(<a href="http://www.codinghorror.com/blog/sounds/captcha-word-spoken.mp3">hear it spoken</a>)<br />
<input tabindex="3" id="captcha" name="captcha" /><br /><br />
<label for="subject">Subject: </label>
<input type="text" id="subject" name="subject" /> (required)<br/>
<label for="body">Details: </label>
<textarea id="body" name="body"></textarea><br/>
<input type='submit' value='Contact Us' />
</form>
</body>
</html>
"""%{'message':message}
@cherrypy.expose
def contactUsSubmitted(self, firstName, lastName, email, phone, captcha, subject, body ):
if captcha[::-1] != 'egnaro':
return self.contactus("Please reenter the word you see in the image." )
self.sendEmail('mail2.example.com','mailbox_account','mailbox_pwd','me@example.com',email,
'Website Contact: '+subject, 'Sender Email: ' + email + '\r\n'
'Name: ' + firstName + ' ' + lastName + '\r\n' + 'Phone: ' + phone + '\r\n' + body)
return self.index()
def sendEmail(self,smtpServer, mailboxName, mailboxPassword, contactEmail,senderEmail,subject,body):
server = smtplib.SMTP(smtpServer) #'smtp1.example.com')
server.login(mailboxName, mailboxPassword)
msg = "To: %(contactEmail)s\r\nFrom: %(senderEmail)s\r\nSubject: %(subject)s\r\nContent-type: text/plain\r\n\r\n%(body)s"
msg = msg%{'contactEmail':contactEmail,'senderEmail':mailboxName + '@example.com','subject':subject,'body':body}
server.sendmail(contactEmail, contactEmail, msg) #This is to send it from an internal account to another internal account.
server.quit()
cherrypy.root = InputExample()
cherrypy.config.update ( file = 'development.conf' )
cherrypy.server.start()
答案 1 :(得分:3)
我意识到这个问题已经有四年了,但对于那些仍在寻找的人来说:
我建议使用python库Marrow Mailer。它抽象整个smtp与sendmail等,并提供出色的服务器端验证。该代码属于MIT许可证(BSD-esque和GPL兼容),这正是您所寻求的。 github上的自述文件提供了如何使用的示例。该库与Python 2.6+和3.1 +兼容。
Marrow Mailer结合 torial 的答案,为CherryPy / python提供了一个简单的“联系我们”表单。我发现Voluptuous是一个非常简单的验证器(可以用于表单服务器端; BSD许可证)或WIP Validator。还有几个用于验证客户端的Javascript / jQuery插件(例如http://docs.jquery.com/Plugins/Validation)。