本文介绍了我正在尝试使用带有gmail的web2py和smtp设置发送电子邮件,我已附加了所有代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在web2py中创建一个表单,该表单在提交时将消息发送到电子邮件帐户主要是我使用SQLFORM.factory创建表单,然后使用gluon.tools导入邮件导入发送电子邮件功能.我已经设置了所有我能想到的东西,但是仍然在web2py中运行此代码,它发出了无法发送电子邮件抱歉"的信息.
i am trying to create a form in web2py which sends message to an email account on submissionmainly i used SQLFORM.factory to create the form then i used gluon.tools import mail to import the send email functionality. i have set up everything i can think of but still on running this code in web2py it gives out that "fail to send email sorry".
from gluon.tools import Mail
mail = Mail()
mail.settings.server = '[email protected]:465'
mail.settings.sender = '[email protected]'
mail.settings.login = '[email protected]: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=['[email protected]'],
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)
推荐答案
在使用mail.send()之前,我建议测试邮件是否设置正确:
Before using mail.send() I would recommend to test if mail is correctly set :
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=['[email protected]'],
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 = '[email protected]:465'
在
mail.settings.server = 'smtp.gmail.com:465'
或
mail.settings.server = 'smtp.gmail.com:587'
这篇关于我正在尝试使用带有gmail的web2py和smtp设置发送电子邮件,我已附加了所有代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!