This is a simple email server built with Flask and Flask-Mail. It works fast and seamlessly with your contact form on your static website.
Check online demo on my website haozhe.li
-
Click here to
to vercel (it is free and only requires a acount if you haven't had one)
-
Configure the environment variables as mentioned, also configure your domain (it would be ok if you use the
vercel.app
). Here is a quick example for the environment variables:MAIL_SERVER=smtp.mail.me.com (this one is for icloud) MAIL_PASSWORD=abcdefg MAIL_USERNAME=demo@haozheli.com MAIL_SENDER=demo@haozheli.com MAIL_FORWARD=your_email_address_to_be_forwarded@haozheli.com MAIL_NAME=Haozhe Li VALID_REQUEST_SOURCE=haozhe.li
-
Create a contact form in your static website. if you already have one, then copy and paste all the
id=...
andname=...
to your own form. Here is an example:<form id="emailForm"> <label for="name">Name:</label> <input type="text" id="name" name="name" required><br><br> <label for="email">Email:</label> <input type="email" id="email" name="email" required><br><br> <label for="message">Message:</label> <textarea id="message" name="message" required></textarea><br><br> <button type="submit">Send</button> </form>
-
Add the javascript at the buttom of your html file before
</body>
, remember to changeYOUR_DOMAIN
to the one you have deployed on vercel.<script> document.getElementById('emailForm').addEventListener('submit', function(event) { event.preventDefault(); const name = document.getElementById('name').value; const email = document.getElementById('email').value; const message = document.getElementById('message').value; const data = { name: name, email: email, message: message }; fetch('YOUR_DOMAIN/send_email', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) .then(response => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); }) .then(data => { console.log('Success:', data); alert('Email sent successfully!'); }) .catch((error) => { console.error('Error:', error); alert('There was an error sending the email. Check the console for more details.'); }); }); </script>
-
If you are still confused, check out the
demo.html
file. Feel free to download it and use it on your own website.
To use this email server, follow these steps:
-
Clone the repository:
git clone https://github.com/Haozhe-Li/Contact_Flask.git
-
Install the required dependencies:
pip install -r requirements.txt
-
Set up the environment variables by creating an
.env
file in your project directory:MAIL_SERVER
: SMTP server addressMAIL_USERNAME
: SMTP server username.MAIL_PASSWORD
: SMTP server password. You may need to generate a secret key or an application-specific password. Don't know how? Check FAQ.MAIL_SENDER
: Email address to send emails fromMAIL_FORWARD
: Email address to forward emails toMAIL_NAME
: Name to display in the emailVALID_REQUEST_SOURCE
: The source of the request, e.g. your website example.com. This will helps you filter the malicious injection. Set to FALSE to disable flitering.
-
Run the Flask application:
python3 app.py
-
Access the contact form on your static website and start receiving emails!
This project is built on flask (python) , so it works on almost all serverless platform. Click here to on vercel, or you can choose any platform that supports flask if you like.
A: The most common reason is that your email provider has enforced an app-specific password to protect your account. You will probably need to generate a unique password for this project.
Common Email Provider SMTP articles here:
Your email provider is not listed here? Try searching Your email provider
+ SMTP
on Google, then you will find the solution.
A: This usually happens when you have multiple email addresses under the same account. Please make sure you select your main email address instead of the alias or forwarded address.
Contributions are welcome! If you have any suggestions or improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more information.