-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
57 lines (41 loc) · 1.32 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from flask import Flask, render_template, request
import smtplib
import os
from dotenv import load_dotenv
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
app = Flask(__name__)
load_dotenv()
def send_email(subject, message, toEmail):
# Email Configuration
smtp_server = "smtp.gmail.com"
smtp_port = 587
smtp_username = os.getenv('EMAIL')
smtp_password = os.getenv('EMAIL_PASSWORD')
# Email Structure
msg = MIMEMultipart()
msg['From'] = smtp_username
msg['To'] = toEmail
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
# Connection to the SMTP Server
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
# login acc
server.login(smtp_username, smtp_password)
# Send Message
server.sendmail(smtp_username, toEmail, msg.as_string())
# Quit the server
server.quit()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/send_email', methods=['POST'])
def send_email_route():
subject = request.form.get('subject')
message = request.form.get('message')
to_email = request.form.get('to_email')
send_email(subject, message, to_email)
return render_template('index.html', message='Email sent successfully')
if __name__ == '__main__':
app.run(debug=True)