Issue
I'm trying to send an email after a post request from a template blog site made with flask. All the variables i need are appearing correctly, but the send_mail
function appears to not work within the code block.
def send_mail(who_will_receive: str, subject: str, message: str):
"""Sends an email to someone.
:param who_will_receive: str - user who will receive the mail.
:param subject: str - Title of the email.
:param message: str - Message on the body of the mail
"""
my_google_email = "[email protected]"
# noinspection SpellCheckingInspection
password_google = "apppassword"
with smtplib.SMTP(host="smtp.gmail.com", port=587, timeout=120) as connection:
connection.starttls()
connection.login(user=my_google_email, password=password_google)
connection.sendmail(
from_addr=my_google_email,
to_addrs=who_will_receive,
msg=f"Subject: {subject}\n\n{message}".encode("utf8")
) # envia um email para uma conta específicada
@app.route("/contact", methods=["POST", "GET"])
def contact_page():
if request.method == "POST":
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
message = request.form['message']
print(name)
print(email)
print(phone)
print(message) # all these variables are working, but when it comes to send the email, it simply does not appear on my inbox.
send_mail("[email protected]", f"Contact from {name}", f"Name: {name}\nEmail: {email}\nPhone Number: {phone}\nMessage: {message}") # The program executes fine but the email does not appear.
return render_template("contact.html", form_submitted=True)
if request.method == "GET":
return render_template("contact.html")
I've tried changing the google app password thinking it might be the problem, but it still keeps happening.
I've also tried to add timeout=120
keyword argument to the connection, but it seems unrelated to my problem.
Solution
First thing firs make sure you allowed less secure apps on your google account. secondly you may try to use a library called flask-mail which is working great
app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = '[email protected]'
app.config['MAIL_PASSWORD'] = 'mailpassword'
app.config['MAIL_DEFAULT_SENDER'] = '[email protected]'
app.config['MAIL_MAX_EMAILS'] = 50
mail = Mail(app)
@app.route("/contact", methods=["POST", "GET"])
def contact_page():
if request.method == "POST":
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
message = request.form['message']
recipient_list = []
with mail.connect() as conn:
for recipient in recipient_list:
msg = Message(subject='subject', recipients=[recipient])
msg.body = f'''
{name}
{email}
{phone}
{message}
'''
conn.send(msg)
return render_template("contact.html", form_submitted=True)
if request.method == "GET":
return render_template("contact.html")
again if you want you can move this to function but I bealive in this way it is more understandable.
Answered By - ct-7567
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.