[App Server] Sending e-mail from local App Server - No SMTP server

Hi,

I’ve completed the tutorial for the feedback form, and it runs ok in the Anvil hosting.
Now, I cloned it and tried to run it on a local installation of the App Server.
It fails because there is no SMTP server configured for the app.

I will appreciate any guidance on how to install/configure the SMTP server for the app.

Thank you very much!!
Warm regards
Ricardo

Have you seen the readme at GitHub - anvil-works/anvil-runtime: The runtime engine for hosting Anvil web apps ? That shows options you can give to the open source server, and those include setting the SMTP port, host, etc.

You will need an SMTP server of some sort to point it to.

1 Like

Hi,
You are right! , and surely it is the way to go.
So, according to this it might be possible to use for example Gmail as my outgoing SMTP server? and configure the runtime engine to point at it as if it were a client?
Or do I have to install my own email server? (I hope not :wink:)

Thank you!

Yes, you can use Gmail or any external mail server. Check that your service provider doesn’t block SMTP traffic (ie. residential ISPs).

Also, if you run into any issues with Gmail’s SMTP working, be sure to read up on using “less secure apps”:

https://support.google.com/a/answer/6260879?hl=en

Cheers,
Ken

1 Like

Hi,
How are you?
Well, things are getting interesting.

I tried to configure the SMTP server in the yaml file like this: (with and without the double quotes (just in case), as individual parameters)
smtp-host: “smtp.gmail.com
smtp-username: “account@gmail.com
smtp-password: “password”
smtp-port: 465
smtp-encryption: “ssl” (also tried other options here)

And I got an “No SMTP server has been configured …” error


Then I configured the same but in the server_config: {} parameter of the yaml file (because this is were Anvil server writes the configuration when you mark the option for a custom SMTP server in the editor)
And I got "InternalError: Internal server error: 60220e234353"


I read that as the function is calling anvil.email.send which uses Anvil servers, it will not work in the runtime engine.

So, I run the python debug smtp server
python -m smtpd -c DebuggingServer -n localhost:1025

And I replaced the original code with:

def add_feedback(name, email, feedback):
import smtplib
from email.mime.text import MIMEText

sender = 'admin@example.com'
receivers = ['info@example.com']


port = 1025
msg = MIMEText('This is test mail')

msg['Subject'] = 'Test mail'
msg['From'] = 'admin@example.com'
msg['To'] = 'info@example.com'

with smtplib.SMTP('localhost', port) as server:
    
    # server.login('username', 'password')
    server.sendmail(sender, receivers, msg.as_string())
    print("Successfully sent email")

And I got "AnvilWrappedError: [Errno 99] Cannot assign requested address"

Then I realized that was running the engine inside a Docker container, so I run the smtpd inside the container and this last option worked.

It seems there is a considerable loss of smoothness when moving to the runtime engine with respect to the hosted solution. Is this so? or am I missing some things? or making things the wrong way?

Regarding the first two attempts, is there any way to make them work?, or these are just dead ends? Does the fact of the Docker container could have something to do with it?

Thank you!!!

I’m running multiple Anvil app servers/runtimes on my own Debian box, have no issues using my own SMTP server. anvil.email.send uses the SMTP settings you pass it on the command line or in the config file, it’s not tied to the anvil.works mail servers.

Cheers,
Ken

Hi Ken,
Thank you for your answer.

Would you mind sharing were and how you configured the SMTP server (which configuration file, and the format of the configuration), because as I described before, my attempts to do so in the app yaml file, failed, and when I try to pass anything in the command line using --parameter-name, no matter what parameter I pass, it returns this:
“The current directory does not contain an Anvil app.
Specify the app to serve with the --app option.”

And the server doesn’t start.
I erase the --parameter from the command line, execute the same command again, and the server starts up fine.
I couldn’t figure out what I might be doing wrong.

Thank you!

As an old mentor once said, solve one problem at a time :wink: If you’re getting that error message, you need to sort it out first. Are you running it from the correct folder? Is your virtual environment set up and activated? Start with just getting:

anvil-app-server --app MY_APP_NAME

… working correctly before you tackle the other parameters. Once that’s sorted, try passing the SMTP parameters on the command line before tackling the YAML config file.

My command line is just:

anvil-app-server --config-file anvil_app.conf

and my anvil_app.conf has:

app: MY_APP_NAME
port: 8080
manual-cert-file: MY_DIGITAL_CERT.pem
manual-cert-key-file: MY_DIGITAL_CERT_KEY.pem
smtp-host: MY.SMTP.SERVER.COM
smtp-port: 25
origin: https://MY.FQDN.COM

You’ll need to add your SMTP authentication info as well. I don’t have any additional authentication parameters for my SMTP server because of how my IT department has the VLANs setup, IP restrictions, etc.

Cheers,
Ken

1 Like

Just to address this: I would say that’s where the value-add of any managed service comes from. If you want smoothness, yes, you pay someone else to look after the administration and the virtualization environment and the mail server and the web server that reverse proxies the application server and so on…

With the self-hosted app server/runtime, you look after all of that yourself, including troubleshooting any misconfiguration, system integration, weirdness, etc. That’s the price you pay, so to speak.

I think the price of both routes are totally worth it, depending on your needs and how much you value your time :wink:

Cheers,
Ken

Hi Ken, (this one is in reply to … “As an old mentor once said”)
:grin: :grin:

You are right. I stacked a bunch of problems and they ended up mixing and making difficult to find root causes.

Regarding the first problem I believe it has to do with the fact that I run the server inside Docker.
Here is the result of my investigation.

I was trying to add the parameters like you said, which is ok when running without Docker. (I mentioned that I was running inside Docker, but I didn’t emphasize on that fact, so probably it passed unnoticed)

Now, with Docker the thing is a bit different as the image has an Entrypoint section which has the command that is going to be executed on start, with some predefined parameters.
So, to be able to add parameters to the ones existing you have to somehow override that Entrypoint.
The way I found, was directly in the docker run command (there are other was for sure).
But something to take into consideration is that all the parameters must be passed after the name of the image and not after the command of the Entrypoint. (I copy the command I used to make the point clearer)

docker run –entrypoint anvil-app-server -v /C/ProyectosSemanticos/Anvil/AppsWeb/FeedbackFormMio:/apps/MainApp -v /C/ProyectosSemanticos/Anvil/anvil-data:/anvil-data -p 3030:3030 anvilworks/anvil-app-server –app MainApp --data-dir /anvil-data

… and here you should be able to add the rest of the parameters as well.

A bit more explanation to put context to the problem:

The application resides in the host machine, outside the Docker container.

What I do is to map the application folder “FeedbackFormMio” (on the host machine, Windows) to the default application folder of the runtime engine “MainApp” (inside the Docker container ), using the -v option.
With this command it runs fine, and it doesn’t matter in which folder I’m standing, because I’m mapping the right folder.

And now, after discovering the Entrypoint matter, it all runs as expected.

I’m glad I could find out this solution, and I hope this helps anyone else trying to do things the hard way (like me :smile: :smile:)

Thank you!!
Warm regards!!!
Ricardo

100% agreed !
And it is completely fair.
Please don’t see it as a criticism, it’s just an observation.
I’ve been trying Anvil for just a few days, and I’m already loving it.

“That being said” (paraphrasing Mr. Spock :flying_saucer:), I was a bit surprised, because I sometimes tend to idealize things, and from what it says here: Anvil Docs | Host apps on your own server

  • No configuration required - The Anvil App Server includes its own database (Postgres) and reverse proxy (Traefik), so all you need to do is launch it. No need to spend half an hour setting up your environment.

I thought smoothness will be consistent on every kind of deployment. My fault.

The other thing I really like is the response from the community. Fast, well intented, and assertive.

Thank you !!!