Advertisement
Guest User

Untitled

a guest
Jul 4th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. ------- Views.py ---------------
  2. @csrf_exempt
  3. def counter(request):
  4. template = "test.html"
  5. return render(request, template)
  6.  
  7. @csrf_exempt
  8. def enviar_emails(request):
  9. email = request.POST.get('emails', None)
  10. segundos = request.POST.get('segundos', 15)
  11. if not email == None:
  12. email = str(email).split(',')
  13. SMTPserver = 'host'
  14. sender = 'mail'
  15.  
  16. USERNAME = "user"
  17. PASSWORD = "pass"
  18.  
  19. # typical values for text_subtype are plain, html, xml
  20. text_subtype = 'html'
  21.  
  22.  
  23. content="Email"
  24.  
  25. subject="Subject"
  26.  
  27. import sys
  28. import os
  29. import re
  30.  
  31. from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
  32.  
  33. from email.mime.text import MIMEText
  34.  
  35. try:
  36. msg = MIMEText(content, text_subtype)
  37. msg['Subject']= subject
  38. msg['From'] = sender # some SMTP servers will do this automatically, not all
  39.  
  40. conn = SMTP(SMTPserver)
  41. conn.set_debuglevel(False)
  42. conn.login(USERNAME, PASSWORD)
  43. try:
  44. for destination in email:
  45. conn.sendmail(sender, destination, msg.as_string())
  46. time.sleep(segundos)
  47. #return HttpResponse(destination) #Esto rompe todo
  48. finally:
  49. conn.quit()
  50.  
  51. except Exception as exc:
  52. sys.exit( "mail failed; %s" % str(exc) )
  53.  
  54. return HttpResponse(email)
  55.  
  56. -------------- JAVASCRIPT CODE ---------------------
  57. <script>
  58. $('#send_emails').submit(function(e){
  59. e.preventDefault();
  60. $.ajax({
  61. url : "/emails/",
  62. type : "POST",
  63. data : {'emails' : $('input[name="emails"]').val()},
  64.  
  65. success : function(data) {
  66. console.log(data);
  67. },
  68.  
  69. // handle a non-successful response
  70. error : function(xhr,errmsg,err) {
  71. $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
  72. " <a href='#' class='close'>&times;</a></div>"); // add the error to the dom
  73. console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
  74. }
  75. });
  76. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement