def email_message(receiver,message): sg = sendgrid.SendGridAPIClient(apikey='') from_email = Email("*****@*****.**") to_email = Email(receiver) subject = "Message from My Email App" content = Content("text/plain", message + "\n\n Sent by " + "*****@*****.**") mail = Mail(from_email, subject, to_email, content) response = sg.client.mail.send.post(request_body=mail.get()) print(response.status_code) print(response.body) print(response.headers)
def send_email(row, from_email, subject, text, sendgrid_api_key): subject2 = subject.replace('SURNAME', row[0]).replace('NAME', row[1]).replace( 'EMAIL', row[2]).replace('SCHOOL', row[3]) text2 = text.replace('SURNAME', row[0]).replace('NAME', row[1]).replace( 'EMAIL', row[2]).replace('SCHOOL', row[3]) sg = sendgrid.SendGridAPIClient(apikey=sendgrid_api_key) from_email = Email(from_email) to_email = Email(row[2]) content = Content("text/plain", text2) mail = Mail(from_email, subject2, to_email, content) response = sg.client.mail.send.post(request_body=mail.get()) return response
def register(): data = request.json emailUser = data['email'] test = User.query.filter_by(email=emailUser).first() if test: return jsonify(message='A user with this email already exists.'), 409 if data['firstName'] == "": return jsonify(message="Enter a first name") if data['lastName'] == "": return jsonify(message="Enter a last name") if not re.search(regex, data['email']): return jsonify(message="Invalid email") if len(data['password']) < 8: return jsonify(message="Password must be more then 8 characters") if data['password'] != data['confirmPassword']: return jsonify(message='Passwords do not match') else: hashed_password = generate_password_hash(data['password'], method='sha256') new_user = User(public_id=str(uuid.uuid4()), firstName=data['firstName'], lastName=data['lastName'], email=data['email'], healthCard=data['healthCard'], phoneNumber=data['phoneNumber'], password=hashed_password, confirmedEmail=False, confirmedOn=None) email = data['email'] from_email = Email("*****@*****.**") to_email = To(email) subject = "Verify your email" token = s.dumps(email, salt='email-confirm') link = url_for('confirm_email', token=token, _external=True) content = Content("text/plain", "Your link is {}".format(link)) mail = Mail(from_email, to_email, subject, content) response = sg.client.mail.send.post(request_body=mail.get()) print(response.status_code) print(response.body) print(response.headers) db.session.add(new_user) db.session.commit() return jsonify(message='User Created'), 201
def query(): feedback_file = './image_feedback/' feedback_file_name = os.listdir(feedback_file)[0] #replace function to remove the word jpg if request.method == 'GET': return render_template("feedback.html") classification = format(request.form['classification']) reason = format(request.form['reason']) mail = format(request.form['email']) feedback_folder = './image_feedback/' feedback_file = os.listdir(feedback_folder)[0] feedback_file_full = './image_feedback/' + feedback_file with open(feedback_file_full, 'rb') as f: data = f.read() encoded = base64.b64encode(data).decode() attachment = Attachment() attachment.content = encoded attachment.type = "application/jpg" if attachment.type != "application/jpg": #allows only png and jpg attachment.type = "application/png" attachment.type = "application/png" attachment.filename = feedback_file_name attachment.disposition = "attachment" attachment.content_id = "image file" from_email = Email("*****@*****.**") sg = sendgrid.SendGridAPIClient( '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' ) subject = "Concrete Surface misclassification query" to_email = Email(mail) cc_email = Email("*****@*****.**") p = Personalization() p.add_to(to_email) p.add_cc(cc_email) message = "your query:" + "" + classification + " " + reason + ". " + "This is the query receipt. The incorrectly classified image is attached to the email " #print(message) content = Content("text/plain", message) mail = Mail(from_email, subject, to_email, content) mail.add_personalization(p) # allows you to send CC = carbon copy mail.add_attachment(attachment) response = sg.client.mail.send.post(request_body=mail.get()) print(response.status_code) print(response.body) print(response.headers) return render_template("finished_feedback.html")
def send(): datetime.datetime.now() time = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) result_file = './image_email/' result_file_name = os.listdir(result_file)[0] result_file_name_no_ext = (os.path.splitext(result_file_name)[0]) #replace function to remove the word jpg if request.method == 'GET': # if someone presses on that send button it renders the email template return render_template("send_email.html") #return'<form action="send" method = "POST"><input name ="email"><input type ="submit"></form>' #code above was used to test if email could be sent mail = format(request.form['email']) location = format(request.form['location']) email_folder = './image_email/' files = os.listdir(email_folder)[0] image_file = './image_email/' + files with open(image_file, 'rb') as f: data = f.read() encoded = base64.b64encode(data).decode() attachment = Attachment() #attachment.set_content(encoded) # there was no documentation to handle this problem figure this out by. attachment.content = encoded attachment.type = "application/jpg" if attachment.type != "application/jpg": #allows only png and jpg attachment.type = "application/png" attachment.type = "application/png" attachment.filename = result_file_name attachment.disposition = "attachment" attachment.content_id = "image file" from_email = Email("*****@*****.**") sg = sendgrid.SendGridAPIClient( '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' ) subject = "Concrete Surface Results" to_email = Email(mail) message = "this is the results" + " " + result_file_name_no_ext + " " + "for building in:" + location + ". " + "Concrete surface found at:" + time + ". " + "The attachment has been named to the classification results" content = Content("text/plain", message) mail = Mail(from_email, subject, to_email, content) mail.add_attachment(attachment) response = sg.client.mail.send.post(request_body=mail.get()) print(response.status_code) print(response.body) print(response.headers) return render_template("finished.html")
def resend(): data = request.json test = User.query.filter_by(email=data['email']).first() if not test: return jsonify(message="Invalid Email") if test.confirmedEmail: return jsonify(message="Already Verified!") else: email = data['email'] from_email = Email("*****@*****.**") to_email = To(email) subject = "Verify your email" token = s.dumps(email, salt='email-confirm') link = url_for('confirm_email', token=token, _external=True) content = Content("text/plain", "Your link is {}".format(link)) mail = Mail(from_email, to_email, subject, content) response = sg.client.mail.send.post(request_body=mail.get()) print(response.status_code) print(response.body) print(response.headers) return jsonify(message='Sent a new token!')