2015-10-06 63 views
2

所以我在我的web應用程序中有一個表單,我試圖從用戶那裏收集反饋。該表格由主題,電子郵件和內容組成,我希望將電子郵件發送到我的帳戶。我對此有很多驗證和理解麻煩。我的代碼在下面,我會在以後進一步解釋我的問題。Python /電子郵件 - 如何通過send_email接收用戶反饋?

Settings.py

EMAIL_HOST_USEREMAIL_HOST_PASSWORD充滿我的應用程序。

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" 

EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_PORT = 587 

EMAIL_HOST_USER = '#' 
EMAIL_HOST_PASSWORD = '#' 

EMAIL_USE_TLS = True 
EMAIL_USE_SSL = False 

Forms.py

class Contact(forms.Form): 
    subject = forms.CharField(required=True) 
    content = forms.CharField(widget=forms.Textarea) 
    email = forms.EmailField(required=True) 

Views.py

def about(request): 
    if request.method == 'GET': 
     form = Contact() 
    else: 
     form = Contact(request.POST) 
     if form.is_valid(): 
      subject = form.cleaned_data['subject'] 
      content = form.cleaned_data['content'] 
      email = ['[email protected]'] 


      #try: 
      server = smtplib.SMTP('smtp.gmail.com:587') 
      server.starttls() 
      server.login(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD) 
      server.sendmail(email, email, content) 
      server.quit() 
      print "Successfully sent email" 
     #except SMTPException: 
      print "Error: unable to send email" 

所以我有點困惑,我如何能夠從用戶發送一封電子郵件帳戶礦因爲我必須讓用戶登錄,所以我正在嘗試從我的帳戶發送一封電子郵件到我的帳戶,然後我只是將用戶電子郵件附加到內容中。然而,這也不起作用。

一,是發送電子郵件給自己一個愚蠢的做法嗎?

另外,關於代碼問題,我不斷收到驗證錯誤,谷歌說我應該設置雙向驗證。這是我應該做的嗎?

有人可以給我一些想法,我應該去哪裏?謝謝。

+1

通過gmail發送東西有點奇怪。看看這個其他SO問題:http://stackoverflow.com/questions/25944883/how-to-send-an-email-through-gmail-without-enabling-insecure-access – spectras

+0

使用從mailgun.com或類似的SMTP服務器服務。每月可免費發送10000封郵件。 – Lucas03

回答

0

首先,允許從您的Gmail帳戶訪問應用程序提供 you will get the mail when you will try this code and you have to allow access and don't allow 2-step verification 使用如下代碼:

forms.py

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

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

contact.html

<!DOCTYPE html> 
<html> 
<head lang="en"> 
<meta charset="UTF-8"> 
<title>Contact</title> 
</head> 
<body> 
<h1>Contact Form:</h1> 

<form action="/contact" method="post"> 
     {{ form.hidden_tag() }} 
    <p> 
     {{ form.name.label }} 
     {{ form.name }} 
    </p> 
    <p> 
     {{ form.email.label }} 
     {{ form.email }} 
    </p> 
    <p> 
     {{ form.message.label }} 
     {{ form.message }} 
    </p> 
    <p> 
     {{ form.submit }} 
    </p> 
    </form> 
    </body> 
    </html> 

main.py

from flask import Flask, render_template, request 
from flask_mail import Mail, Message 
from forms import ContactForm 


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

# add mail server config 
app.config['MAIL_SERVER'] = 'smtp.gmail.com' 
app.config['MAIL_PORT'] = 465 
app.config['MAIL_USE_SSL'] = True 
app.config['MAIL_USERNAME'] = '[email protected]' 
app.config['MAIL_PASSWORD'] = 'yourMailPassword' 

mail = Mail(app) 

@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: 
     msg = Message("Message from your visitor" + form.name.data, 
         sender='[email protected]', 
         recipients=['[email protected]', '[email protected]']) 
     msg.body = """ 
     From: %s <%s>, 
     %s 
     """ % (form.name.data, form.email.data, form.message.data) 
     mail.send(msg) 
     return "Successfully sent message!" 
elif request.method == 'GET': 
    return render_template('contact.html', form=form) 

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