0

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
Eliot Kim
  • 63
  • 8

1 Answers1

0

Google in their unlimited wisdom dropped support for gmail.com accounts sending via username and password authenticated SMTP.

If your .edu account is a Google Workspace account this should still work (for now) but they tend to turn "Less Trusted Apps" the setting you need on for this to work back off "for your security"

Also you do not need ehlo call

smtp = SMTP(server, port)
smtp.starttls()
smtp.login(user, pass)
smtp.sendmail(frmaddr, toaddr, message)
  • The email gets sent when I run it in a normal python script. The issue is when I build a docker image and run the port, it doesn't do anything – Eliot Kim Jun 12 '22 at 01:25
  • If that is the case I would assume it's an issue with your usage of the app decorator. From the example here it may be that it's generating an internal not awaited error for your async function (all their examples are non async), Unsure if them naming the function read_root rather than just root matters. –  Jun 12 '22 at 01:36
  • So if I build a docker container without the FASTAPI and the root function I am able to send the email. The problem comes when I build a container of the FASTAPI. Do you know if I can deploy on Azure without using Flask or Fastapi? – Eliot Kim Jun 12 '22 at 01:52
  • This may be helpful to you Sorry have little to no experience with Azure. –  Jun 12 '22 at 02:07
  • This suggests Azure block this kind of behavior without it being specifically requested –  Jun 12 '22 at 02:15