我正在尝试使用 web2py 和 gmail 发送电子邮件并使用 smtp 设置我已附加所有代码

2022-01-17 00:00:00 python smtp web2py smtp-auth

问题描述

我正在尝试在 web2py 中创建一个表单,该表单在提交时将消息发送到电子邮件帐户主要是我使用 SQLFORM.factory 创建表单,然后我使用 gluon.tools import mail 导入发送电子邮件功能.我已经设置了我能想到的所有东西,但仍然在 web2py 中运行此代码,它给出了未能发送电子邮件抱歉".

i am trying to create a form in web2py which sends message to an email account on submission mainly 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 = '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.
Name:"+ session.name+" 
Email : " + session.email +"
Subject : "+session.subject +"
Message : "+session.message+ ".
 "
        )

        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=['otheremail@yahoo.com'],
            subject='project minerva',
            message= "Hello this is an email send from minerva.com from contact us form.
Name:"+ session.name+" 
Email : " + session.email +"
Subject : "+session.subject +"
Message : "+session.message+ ".
 "
        ):
            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'

相关文章