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

时间:2013-09-30 02:48:24

标签: python smtp web2py smtp-auth

我正在尝试在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 个答案:

答案 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发送电子邮件:

  1. 阅读https://support.google.com/mail/answer/1173270?hl=en
  2. 上的Google说明
  3. 使用您的Gmail帐户登录后,转到https://security.google.com/settings/security/apppasswords?pli=1,然后输入Google提供的一次性广告,以及您打算允许使用gmail服务的应用程序(我已经提到过“Web2Py”)
  4. 在定义邮件服务的模型中 - 使用W2P书中推荐的内容与上面提到的新pwd一起使用 - 并且它有效!
  5. 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))