-->

i am trying send email using web2py with gmail and

2019-03-04 10:46发布

问题:

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.\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)

回答1:

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.\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.'

Then try to change :

mail.settings.server = 'smtp@gmail.com:465'

in

mail.settings.server = 'smtp.gmail.com:465'

or

mail.settings.server = 'smtp.gmail.com:587'


回答2:

All the issues I have experienced with sending email ("unable to send email") from Web2Py via Gmail, have been caused by the 2-steps authentication which was enabled on my gmail account.

In order for Web2Py to be able to send email via gmail:

  1. Read Google instructions at https://support.google.com/mail/answer/1173270?hl=en
  2. When logged in with your gmail account, go to https://security.google.com/settings/security/apppasswords?pli=1 and enter the once-only pwd Google provides with the app you intend to allow use the gmail services (I have mentioned "Web2Py")
  3. In the model where the mail services are defined - use exactly what's recommended in the W2P book with the new pwd mentioned above - and it works !

NeoToren



回答3:

I got the email to work with gmail with the modification of this setting:

mail.settings.server = 'smtp.gmail.com'


回答4:

For anyone else experiencing issues discovering their mail setting, here's a brute force approach:

Also available at this gist.

Code:

'''
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))