Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------- Views.py ---------------
- @csrf_exempt
- def counter(request):
- template = "test.html"
- return render(request, template)
- @csrf_exempt
- def enviar_emails(request):
- email = request.POST.get('emails', None)
- segundos = request.POST.get('segundos', 15)
- if not email == None:
- email = str(email).split(',')
- SMTPserver = 'host'
- sender = 'mail'
- USERNAME = "user"
- PASSWORD = "pass"
- # typical values for text_subtype are plain, html, xml
- text_subtype = 'html'
- content="Email"
- subject="Subject"
- import sys
- import os
- import re
- from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
- from email.mime.text import MIMEText
- try:
- msg = MIMEText(content, text_subtype)
- msg['Subject']= subject
- msg['From'] = sender # some SMTP servers will do this automatically, not all
- conn = SMTP(SMTPserver)
- conn.set_debuglevel(False)
- conn.login(USERNAME, PASSWORD)
- try:
- for destination in email:
- conn.sendmail(sender, destination, msg.as_string())
- time.sleep(segundos)
- #return HttpResponse(destination) #Esto rompe todo
- finally:
- conn.quit()
- except Exception as exc:
- sys.exit( "mail failed; %s" % str(exc) )
- return HttpResponse(email)
- -------------- JAVASCRIPT CODE ---------------------
- <script>
- $('#send_emails').submit(function(e){
- e.preventDefault();
- $.ajax({
- url : "/emails/",
- type : "POST",
- data : {'emails' : $('input[name="emails"]').val()},
- success : function(data) {
- console.log(data);
- },
- // handle a non-successful response
- error : function(xhr,errmsg,err) {
- $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
- " <a href='#' class='close'>×</a></div>"); // add the error to the dom
- console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement