Advertisement
Guest User

Untitled

a guest
Aug 11th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. from flask import Flask
  2. from flask_cors import CORS, cross_origin
  3. from flask import request,jsonify
  4.  
  5. import smtplib, os, cgi
  6. from email.mime.application import MIMEApplication
  7. from email.mime.multipart import MIMEMultipart
  8. from email.mime.base import MIMEBase
  9. from email.mime.text import MIMEText
  10. from email import encoders
  11.  
  12. app = Flask(__name__)
  13. CORS(app)
  14.  
  15. @app.route('/Send',methods=['POST'])
  16. def Send():
  17. fromaddress='rake.son25@gmail.com'
  18. content = request.json
  19. toaddress=content['emailTo']
  20. subject=content['subject']
  21. text=content['body']
  22. username = 'rake.son25@gmail.com' # username
  23. password ='**********************' # password(here I gave my password)
  24. msg = MIMEMultipart() # this is the message object
  25. msg['From'] =fromaddress
  26. msg['To'] = toaddress
  27. msg['Subject'] =subject
  28. files =content['file']
  29.  
  30. msg.attach(MIMEText(text)) # Attaching subject tect to body, you can provide you own custom
  31.  
  32. try:
  33. for f in files or []:
  34. path = os.path.join(os.getcwd(), f)
  35. if os.path.isfile(path):
  36. part = MIMEBase('application', "octet-stream")
  37. part.set_payload(open(path, "rb").read())
  38. encoders.encode_base64(part)
  39. part.add_header('Content-Disposition', 'attachment; filename=' + os.path.basename(path))
  40. msg.attach(part)
  41. server = smtplib.SMTP('smtp.googlemail.com')
  42. server.ehlo()
  43. server.starttls()
  44. server.login(username,password)
  45. server.sendmail(fromaddress,toaddress,msg.as_string())
  46. server.quit()
  47. return("Mail send successfully")
  48. except IOError:
  49. return("Error sending mail")
  50.  
  51. if __name__=="__main__":
  52. app.run(debug=True)
  53.  
  54. <div class="input-group">
  55. <input type="file" value="Browse" id="fl" /><button ng-click="addFile()">Add Files</button>
  56. <span ng-repeat="f in fileName"><a>{{f}} </a> <a href="" ng-click="removeFile(f)">Remove</a></span>
  57. </div>
  58.  
  59. myApp.controller('formController',['$scope','$http', function($scope,$http) {
  60. //$('.datepicker').datepicker();
  61. $scope.fileName=[];
  62. debugger;
  63. //$scope.formData = {};
  64. // $scope.data = {};
  65. $scope.save=function(){
  66. console.log($scope.formData)
  67. debugger
  68. $scope.formData.to = "rakeshlochansarma25@gmail.com";
  69. $scope.formData.from = "rake.son25@gmail.com";
  70. $scope.formData.subject="test";
  71. $scope.formData.body="test me"
  72. var body="Email :"+$scope.formData.email+', Phone No.'+$scope.formData.phno+';'
  73. $http({
  74. method:'POST',
  75. url:'http://127.0.0.1:5000/Send',
  76. headers: {
  77. 'Content-Type': 'application/json;charset=utf-8'
  78. },
  79. data:{emailTo:'rakeshlochansarma25@gmail.com',subject:'Candidate Profile :'+$scope.formData.fullName,body:body,file:$scope.fileNameString}
  80. })
  81. .then(function(resp){
  82. console.log(resp);
  83. },function(error){
  84. console.log(error);
  85. });
  86. };
  87. $scope.addFile = function(){
  88. debugger
  89. var flname=[]
  90. flname = document.getElementById("fl").files;
  91. for (var i = 0; i < flname.length; i++){$scope.fileName.push(flname[i].name);}
  92. $scope.fileNameString = $scope.fileName.join();
  93.  
  94. // alert(JSON.stringify($scope.fileName));
  95. };
  96. $scope.removeFile = function(fl) {
  97. $scope.fileName.splice($scope.fileName.indexOf(fl),1);
  98. };
  99. $scope.confirm = function(){
  100. debugger
  101. $scope.modalInstance.close({ delete : true});
  102. };
  103.  
  104. $scope.cancel = function(){
  105. $scope.modalInstance.dismiss('cancel');
  106. };
  107.  
  108. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement