我正在尝试在web2py中创建一个表单,该表单在提交时将消息发送到电子邮件帐户 主要是我使用SQLFORM.factory来创建表单然后我用gluon.tools导入邮件来导入发送邮件功能。我已经设置了我能想到的一切,但仍然在web2py中运行此代码,它给出了“无法发送电子邮件抱歉”。
from gluon.tools import Mail
mail = Mail()
mail.settings.server = 'smtp@gmail.com:465'
mail.settings.sender = 'myemail@gmail.com'
mail.settings.login = 'myemail@gmail.com:secret'
def index():
form = SQLFORM.factory(
Field('name', requires=IS_NOT_EMPTY()),
Field('email', requires =[ IS_EMAIL(error_message='invalid email!'), IS_NOT_EMPTY() ]),
Field('subject', requires=IS_NOT_EMPTY()),
Field('message', requires=IS_NOT_EMPTY(), type='text')
)
if form.process().accepted:
session.name = form.vars.name
session.email = form.vars.email
session.subject = form.vars.subject
session.message = form.vars.message
x = mail.send(to=['otheremail@yahoo.com'],
subject='project minerva',
message= "Hello this is an email send from minerva.com from contact us form.\nName:"+ session.name+" \nEmail : " + session.email +"\nSubject : "+session.subject +"\nMessage : "+session.message+ ".\n "
)
if x == True:
response.flash = 'email sent sucessfully.'
else:
response.flash = 'fail to send email sorry!'
#response.flash = 'form accepted.'
elif form.errors:
response.flash='form has errors.'
return dict(form=form)
答案 0 :(得分:3)
在使用mail.send()之前,我建议测试邮件是否设置正确:
if form.process().accepted:
session.name = form.vars.name
session.email = form.vars.email
session.subject = form.vars.subject
session.message = form.vars.message
if mail:
if mail.send(to=['otheremail@yahoo.com'],
subject='project minerva',
message= "Hello this is an email send from minerva.com from contact us form.\nName:"+ session.name+" \nEmail : " + session.email +"\nSubject : "+session.subject +"\nMessage : "+session.message+ ".\n "
):
response.flash = 'email sent sucessfully.'
else:
response.flash = 'fail to send email sorry!'
else:
response.flash = 'Unable to send the email : email parameters not defined'
elif form.errors:
response.flash='form has errors.'
然后尝试改变:
mail.settings.server = 'smtp@gmail.com:465'
在
mail.settings.server = 'smtp.gmail.com:465'
或
mail.settings.server = 'smtp.gmail.com:587'
答案 1 :(得分:2)
我通过Gmail从Web2Py发送电子邮件(“无法发送电子邮件”)时遇到的所有问题都是由我的Gmail帐户启用的两步验证引起的。
为了让Web2Py能够通过gmail发送电子邮件:
NeoToren
答案 2 :(得分:0)
我收到了使用gmail修改此设置的电子邮件:
mail.settings.server = 'smtp.gmail.com'
答案 3 :(得分:0)
对于其他遇到问题的人来说,发现他们的邮件设置,这是一种蛮力方法:
此处{{3}}也可以使用。
代码:
'''
Place this in a controller and call it, either by url or directly from code.
An email(or multiple) with the correct settings will be sent to the
test address.
'''
def test_mail():
bases = ['yourdomain.com', 'yourhosting.company.net']
prefixes = ['smtp.', 'mail.', '']
ports = [':25' ':465', ':993', ':587', '']
sender = 'someone@yourdomain.com'
login = 'someone@yourdomain.com:password'
send_test_to = 'probably.you@gmail.com'
count = 0
mail.settings.tls = True #Here so you can set to False if things fail?
for base in bases:
for prefix in prefixes:
for port in ports:
server = '{0}{1}{2}'.format(prefix, base, port)
msg = 'server: {0} login: {1}'.format(server, login)
# So you can correlate with error codes. Note some servers don't like print!
print msg
mail.settings.server = server
mail.settings.sender = sender
mail.settings.login = login
mail.send(to=[send_test_to],
subject='hello',
reply_to='us@example.com',
message=msg
)
count += 1
return dict(message="tried {0} combinations".format(count))