The Problem
Whenever I try to make a POST request to send an email using SMTPlib through my Flask web application hosted in Vercel I get the following error message FUNCTION_INVOCATION_TIMEOUT
. The Flask route the I use looks as follows:
@app.route("/", methods=["GET", "POST"])
def home():
form = ContactForm()
if form.validate_on_submit():
print("Sent")
username = form.name.data
email = form.email.data
phone_number = form.phone.data
message = form.message.data
full_email = (f"Subject: Message from Personal Website\n\n{message.encode('utf-8')}\n\n\n"
f"From: {username}\nEmail: {email}\nPhone Number: {phone_number}")
with smtplib.SMTP("smtp.gmail.com") as connection:
connection.starttls()
connection.login(user=USERNAME,
password=PASSWORD)
connection.sendmail(from_addr=USERNAME,
to_addrs=USERNAME,
msg=full_email)
return redirect(url_for("receive_data"))
return render_template("index.html",
form=form,
year=str(CURRENT_YEAR))
Also, I am using the default vercel.json
file as:
{
"version": 2,
"builds": [
{
"src":"app.py",
"use":"@vercel/python"
}
],
"routes": [
{
"src":"/(.*)",
"dest":"app.py"
}
]
}
The file structure of my application is:
my-flask-app/
│
├── static
│ ├── assets
│ ├── files
│ └── images
├── templates
├── app.py
├── flaskforms.py
├── Procfile
├── README.md
├── requirements.txt
└── vercel.json
Things I've tried
I tried extending the timeout duration using the function
property within the vercel.json
file as in the code below. However, this creates a deploy error in Vercel.
{
"version": 2,
"builds": [
{
"src":"app.py",
"use":"@vercel/python"
}
],
"routes": [
{
"src":"/(.*)",
"dest":"app.py"
}
],
"functions": {
"app.py": {
"maxDuration": 60
}
}
}
I think that the problem is the time Vercel takes to run a serverless function. However, all the solution I've seen so far apply for NodeJS applications. If anyone could provide me with some advise, I would appreciate it a lot.
Try the Asynchronous Email Sending which may solve this FUNCTION_INVOCATION_TIMEOUT