The image gets build but when I go to the port http://127.0.0.1/ I see the following {"message":"Hello world! From FastAPI running on Uvicorn with Gunicorn. Using Python 3.7"}
However, the email does not get sent. When I run python script without containerizing it the email gets sent so there are no security firewalls preventing the email to be sent. What am I doing wrong? Please help I have been stuck on this problem for a while. I am trying to containerize my application so that I can deploy it on Azure. It is a simple email sending application for practice. Is it even running the test.py? Here's the code:
test.py
from fastapi import FastAPI
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
app = FastAPI()
@app.get("/")
async def root():
from_address = 'myemail@harvard.edu'
to_address = 'bilalhussain.v1@gmail.com'
message = MIMEMultipart('Foobar')
message['From'] = from_address
message['To'] = to_address
content = MIMEText('Here is the second email', 'plain')
message.attach(content)
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login(from_address, 'password')
mail.sendmail(from_address,to_address, message.as_string())
mail.close()
requirements.txt
certifi==2020.12.5
click==7.1.2
fastapi==0.63.0
h11==0.11.0
starlette==0.13.6
uvicorn==0.13.3
Dockerfile
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
ADD find.json .
ADD test.py .
COPY requirements.txt .
RUN pip --no-cache-dir install -r requirements.txt
And I run the following commands in the terminal to build and run the image:
docker build -t test .
docker run -p 80:80 -it test