简体   繁体   中英

Send email flask google app engine - Invalid sender format

I'm trying to make a simple flask contact form on google app engine. I'm new to both.

There are two links which I have used to help me: https://cloud.google.com/appengine/docs/python/mail/sendingmail http://www.boxcontrol.net/adding-contact-form-to-your-site-using-flask-and-python3.html#.VWnRUlzBzGc

This is my code:

main.py

from flask import Flask, render_template, request
from google.appengine.api import mail
from forms import ContactForm


app = Flask(__name__)
app.secret_key = 'YourSuperSecreteKey'


@app.route('/')
def hello():
    """Return a friendly HTTP greeting."""
    return 'Hello World!'


@app.route('/contact', methods=('GET', 'POST'))
def contact():
    form = ContactForm()

    if request.method == 'POST':
        if form.validate() == False:
            return 'Please fill in all fields <p><a href="/contact">Try Again!!!</a></p>'
        else:
            message = mail.EmailMessage(sender=form.name.data,
                            subject="Contact")
            message.to = form.email.data
            message.body = form.message.data
            message.send()
            return "Successfully  sent message!"
        elif request.method == 'GET':
        return render_template('contact.html', form=form)

if __name__ == '__main__':
    app.run()

form.py

from flask_wtf import Form
from wtforms import StringField, TextAreaField, SubmitField, validators

def CheckNameLength(form, field):
  if len(field.data) < 4:
    raise ValidationError('Name must have more then 3 characters')

class ContactForm(Form):
    name = StringField('Your Name:', [validators.DataRequired(), CheckNameLength])
    email = StringField('Your e-mail address:', [validators.DataRequired(), validators.Email('your@email.com')])
    message = TextAreaField('Your message:', [validators.DataRequired()])
    submit = SubmitField('Send Message')

The error I get is: Sorry, unexpected error: Invalid sender format

Can anyone help me understand what I'm doing wrong?


Exception on /contact [POST]
Traceback (most recent call last):
  File "/base/data/home/apps/s~smart-cove-95709/1.384668257912944999/lib/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/base/data/home/apps/s~smart-cove-95709/1.384668257912944999/lib/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/base/data/home/apps/s~smart-cove-95709/1.384668257912944999/lib/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/base/data/home/apps/s~smart-cove-95709/1.384668257912944999/lib/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/base/data/home/apps/s~smart-cove-95709/1.384668257912944999/lib/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/base/data/home/apps/s~smart-cove-95709/1.384668257912944999/main.py", line 40, in contact
    message.send()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py", line 1115, in send
    raise ERROR_MAP[e.application_error](e.error_detail)
BadRequestError: Invalid sender format

Are you following the rules they have for who is allowed to be in the from field of the emails?

The email address of the sender, the From address. The sender address must be one of the following types:

  • The address of a registered administrator for the application. You can add administrators to an application using the Administration Console.

  • The address of the user for the current request signed in with a Google Account. You can determine the current user's email address with the Users API. The user's account must be a Gmail account, or be on a domain managed by Google Apps.

  • Any valid email receiving address for the app (such as xxx@APP-ID.appspotmail.com).

  • Any valid email receiving address of a domain account, such as support@example.com. Domain accounts are accounts outside of the Google domain with email addresses that do not end in @gmail.com or @APP-ID.appspotmail.com.

https://cloud.google.com/appengine/docs/python/mail/sendingmail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM