def contact(request): if request.method == 'POST': # If the form has been submitted... form = contact_form(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data # ... return HttpResponseRedirect('/thanks/') # Redirect after POST else: form = contact_form() # An unbound form return render(request, 'contact.html', { 'form': form, })
def POST(self, user_id): user = users.get_current_user() if user: d = web.input() f = contact_form(message=d.message) if f.validate() and util.user_exists(user_id.lower()): taskqueue.add( url="/task/send_mail", queue_name="email-throttle", params={"sender_id": user.user_id(), "recipient_id": user_id, "message": f.message.data}, ) raise web.seeother("/" + user_id + "#message_sent") elif f.validate() and user_id.lower() == "us": taskqueue.add( url="/task/send_mail", queue_name="email-throttle", params={"sender_id": user.user_id(), "recipient_id": "us", "message": f.message.data}, ) raise web.seeother("/" + user_id + "#message_sent") else: return t.render( util.data( title="Get in touch!", instructions="""You will always reveal your email address when you send a message!""", form=f, subject=" ".join([user.nickname(), "wants to get in touch!"]), ) ) else: return t.render(util.data(title="Not allowed!", instructions="You must be signed in to send messages!"))
def GET(self, user_id): if util.user_exists(user_id.lower()) or user_id.lower() == "us": t = template.env.get_template("contact.html") f = contact_form() user = users.get_current_user() if user: return t.render( util.data( title="Get in touch!", instructions="""You will always reveal your email address when you send a message!""", form=f, subject=" ".join([user.nickname(), "wants to get in touch!"]), ) ) else: return t.render(util.data(title="Not allowed!", instructions="You must be signed in to send messages!")) else: raise web.notfound()
def form(): form = contact_form( ) #make one of the forms in the "forms" file an object to be used below if form.validate_on_submit( ): #will tell me if the form was validated when it was submitted flash( "Your message has been sent successfully. I'll be in touch shortly.", "success_flash_message" ) #the message to notify the user of a successfull submition of the form, the second argument is a category to enable different styling for different flash messages new_data = form_database( name=form.name.data, email=form.email.data, message=form.message.data, privacy_pol_checkbox=form.privacy_pol_checkbox.data ) #grabs user input from the form and prepares it for the database push MY_EMAIL = os.environ.get( 'MY_EMAIL') #gets my email from the environmental variable MY_EMAIL_PASSWORD = os.environ.get( 'MY_EMAIL_PASSWORD' ) #gets the password to my email from the environmental variable message_for_me = EmailMessage( ) #It is the base class for the email object model. EmailMessage provides #the core functionality for setting and querying header fields, for accessing message bodies, and for # creating or modifying structured messages. message_for_me = f"<b>Client name:</b> {form.name.data}<br></br> <b>Email:</b> {form.email.data} <br></br> <b>Message:</b> {form.message.data}" #an email content formatted in "f" string which uses the data a user inputed in the form msg_for_me = MIMEText( message_for_me, "html") #allows to use HTML in the "f" string above msg_for_me['Subject'] = "New form submitted" #email subject msg_for_me[ 'From'] = MY_EMAIL #uses one of my emails to send this email from to my email below msg_for_me['To'] = "*****@*****.**" message_for_customer = EmailMessage() message_for_customer = f"<p>Dear {form.name.data},<br></br> Thank you for expressing your interest in Browebgen web services. I will be in touch with you in the next 24 hours.<br></br> Yours sincerely, <br></br> Dmitry </p>" msg_for_customer = MIMEText(message_for_customer, "html") msg_for_customer['Subject'] = "Thank you from Browebgen" msg_for_customer['From'] = MY_EMAIL msg_for_customer['To'] = form.email.data with smtplib.SMTP_SSL( 'smtp.gmail.com', 465 ) as smtp: #An SMTP instance encapsulates an SMTP connection. It has methods that support a full repertoire of SMTP and ESMTP operations.'smtp.gmail.com' is Gmail server (use this because email is sent from a gmail email address) and 465 is corresponding port smtp.login( MY_EMAIL, MY_EMAIL_PASSWORD) #logs in to my gmail using its password smtp.send_message( msg_for_me ) #sends an email with the form data to me once a new form has been submitted smtp.send_message( msg_for_customer ) #sends an email to the customer adressing them by the name they specified in the form with an HTML formatted email above try: db.session.add( new_data) #adds new data from the form to the database db.session.commit() #comitts this data to the database return redirect('/main_page') except: return redirect( '/main_page' ) #redirects to the same page if something went wrong elif not form.validate_on_submit( ) and request.method == "POST": #if the form failed validation after the data was sent by the user then flash the error massage below flash( "Ooops. Please check the fields below.", "error_flash_message" ) #the message to notify the user of a mistake upon submition of the form, the second argument is a category to enable different styling for different flash messages return render_template( "/Pages/Main page/main_page.html", form=form ) #renders the template at first access to the page and displays the form on the page