web mail interface using pi and flask

raspberry pi

have you ever used gmail or hotmail on the web? it seems so natural that we take it for granted. in this post we’ll see how to build a basic web mail client to send mail … using flask and pi as server. this is content =======

flask intro : folder structure

flask intro : requests

flask : basic app

flask : post flask intro : folder structure ==============================

flask is a python web framework, it is recommended to learn it before django. a typical flask project structure is as follows:

let us say we name our project sendmail, the project structure is

sendmail [folder]
|_ _ app.py
|_ _ templates [folder]
|     |_ _ file.html
|_ _ static [folder]
      |_ _ style.css

to run a flask app you don’t need the templates and static folder but they are here for good reasons the templates folder

let us say you have a website and for each page there are some elements recurring like the header and footer. having a template allows you to specify only the changing data templating engine

flask uses the jinja templating engine for rendering variables etc

{{name}} will display the variable name on rendering. more on that later static folder

static means it won’t change, load your non-changing assets in there flask intro : requests ======================

for a webserver, whatever you type in the address bar after specifying the correct ip is a request


requests the file water.html

in flask however (and python), you need not request for only files, you can pretty much mould the request pattern



flask : basic app

a basic app in flask is as follows :

from flask import Flask

app = Flask(__name__)
def basic():
    return 'welcome to my site'
if __name__ == '__main__': # if file run
    app.run(debug=True, host='')



means the first slash after the domain name if requested

you define the action in the subsequent function, which can be named as you want

let us say the domain is www.d.com, we are specifying what to do when www.d.com/ is requested

return 'welcome to my site'

displays a single string

app.run(debug=True, host='')

host=‘’ runs the app on 192.168. … flask : basic app : return page ===============================

from flask import Flask, render_tempplate

app = Flask(__name__)
def basic():
    return render_template('index.html')
if __name__ == '__main__':
    app.run(debug=True, host='')


return render_template('index.html')

returns the file index.html in the templates folder flask : post ============

there are two request methods viz post and get, post is more secure than get

here is a post demo :

our html

<form method="POST">
<input type="text" name="xyz">
<input type="submit" value="Send">

our post handling method:

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST': #this block is only entered when the form is submitted
        data = request.form['xyz']
        return 'data entered :{}'.format(data)

    return render_template('index.html')

our app

here is the code for our index.html file in our templates folder :

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<form method="POST">
<input type="text" name="to">
<input type="text" name="subject">
<textarea rows="4" cols="50" type="text" name="body">

<input type="submit" value="Send">

our app.py code :

from flask import Flask, render_template, request

# send mail function
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header

def send_mail(to_, subject_, body_):
    smtpport= 587 #needs integer not string

    msg = MIMEMultipart('alt text here')

    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = Header(thesub,'utf8')
    _attach = MIMEText(thebody.encode('utf8'),'html','UTF-8')

    server = smtplib.SMTP(domsmtp, smtpport)
    server.login(fromaddr, thepassword)
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)

    print('mail sent')

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST': #this block is only entered when the form is submitted
        to = request.form['to']
        subject = request.form['subject']
        body = request.form['body']
        send_mail(to, subject, body)
        return 'mail sent to {}'.format(to)

    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True, host='')

just replace [address] and [pass] by your actual data now run app.py, and see the output

you can grab another device on the network, go to 192.168. … depending, and … you can send a mail from another device, from a web interface