0

我正在尝试在 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)
4

4 回答 4

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'
于 2013-09-30T09:05:16.430 回答
2

我在通过 Gmail 从 Web2Py 发送电子邮件(“无法发送电子邮件”)时遇到的所有问题都是由我的 gmail 帐户启用的两步验证引起的。

为了让 Web2Py 能够通过 gmail 发送电子邮件:

  1. 在https://support.google.com/mail/answer/1173270?hl=en阅读 Google 说明
  2. 使用您的 gmail 帐户登录后,转到https://security.google.com/settings/security/apppasswords?pli=1并输入 Google 为您打算允许使用 gmail 服务的应用程序提供的一次性密码(我已经提到了“Web2Py”)
  3. 在定义邮件服务的模型中——完全使用 W2P 书中推荐的内容和上面提到的新密码——它可以工作!

新托伦

于 2014-01-21T21:46:35.577 回答
0

对于其他在发现邮件设置时遇到问题的人,这是一种蛮力方法:

也可以在这个要点上找到。

代码:

'''
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))
于 2016-04-01T16:29:45.830 回答
0

通过修改此设置,我收到了与 gmail 合作的电子邮件:

mail.settings.server = 'smtp.gmail.com'
于 2015-08-22T05:39:05.827 回答