Example: sendmail
Sending email through web interface demonstrates:
- web input of the following data: From, To, Subject and Message for an email,
- submittal of the form that sends an email, and confirmation displayed.
In a nutshell: web browser; Apache; Unix sockets; 1 source files, 79 lines of code.
Screenshots of application
An HTML form will accept the necessary information to send an email. Your server must be enabled to send email via Internet; if not, you can send email to localhost (i.e. to users on your own computer) and for that you must have an MTA like sendmail installed:
Once email is sent, a confirmation is sent to the user.:
Setup prerequisites
Install Vely - you can use standard packaging tools such as
apt,
dnf,
pacman or
zypper.
Because it is used in this example, you will need to install
Apache as a web server.
After installing Vely, turn on syntax highlighting in vim if you're using it:
Get the source code
The source code is a part of Vely installation. It is a good idea to create a separate source code directory for each application (and you can name it whatever you like). In this case, unpacking the source code will do that for you:
tar xvf $(vv -o)/examples/sendmail.tar.gz
cd sendmail
Setup application
The very first step is to create an application. The application will be named "sendmail", but you can name it anything (if you do that, change it everywhere). It's simple to do with
vf:
sudo vf -i -u $(whoami) sendmail
This will create a new application home (which is "/var/lib/vv/sendmail") and do the application setup for you. Mostly that means create various subdirectories in the home folder, and assign them privileges. In this case only current user (or the result of "whoami" Linux command) will own those directories with 0700 privileges; it means a secure setup.
Build application
Use
vv utility to make the application:
Start your application server
To start the application server for your web application use
vf FastCGI process manager. The application server will use a Unix socket to communicate with the web server (i.e. a reverse-proxy):
This will start 3 daemon processes to serve the incoming requests. You can also start an adaptive server that will increase the number of processes to serve more requests, and gradually reduce the number of processes when they're not needed:
See
vf for more options to help you achieve best performance.
If you want to stop your application server:
Setup web server
This shows how to connect your application listening on a Unix socket (started with
vf) to Apache web server.
- Step 1:
To setup Apache as a reverse proxy and connect your application to it, you need to enable FastCGI proxy support, which generally means "proxy" and "proxy_fcgi" modules - this is done
only once:
- For Debian (like Ubuntu) and OpenSUSE systems you need to enable proxy and proxy_fcgi modules:
sudo a2enmod proxy
sudo a2enmod proxy_fcgi
- For Fedora systems (or others like Archlinux) enable proxy and proxy_fcgi modules by adding (or uncommenting) LoadModule directives in the Apache configuration file - the default location of this file on Linux depends on the distribution. For Fedora (such as RedHat), Archlinux:
sudo vi /etc/httpd/conf/httpd.conf
For OpenSUSE:
sudo vi /etc/apache2/httpd.conf
Add this to the end of the file:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
- Step 2:
Edit the Apache configuration file:
- For Debian (such as Ubuntu):
sudo vi /etc/apache2/apache2.conf
- for Fedora (such as RedHat), Archlinux:
sudo vi /etc/httpd/conf/httpd.conf
- and for OpenSUSE:
sudo vi /etc/apache2/httpd.conf
Add this to the end of file ("/sendmail" is the application path (see
request_URL) and "sendmail" is your application name):
ProxyPass "/sendmail" unix:///var/lib/vv/sendmail/sock/sock|fcgi://localhost/sendmail
- Step 3:
Finally, restart Apache. On Debian systems (like Ubuntu) or OpenSUSE:
sudo systemctl restart apache2
On Fedora systems (like RedHat) and Arch Linux:
sudo systemctl restart httpd
Note: you must not have any other URL resource that starts with "/sendmail" (such as for example "/sendmail.html" or "/sendmail_something" etc.) as the web server will attempt to pass them as a reverse proxy request, and they will likely not work. If you need to, you can change the application path to be different from "/sendmail", see
request_URL.
Setup local mail
This example uses email as a part of its function. If your server already has capability to send email, you can skip this.
Otherwise, you can use local mail, and that means email addresses such as "myuser@localhost". To do that, install postfix (or sendmail). On Debian systems (like Ubuntu):
sudo apt install postfix
sudo systemctl start postfix
and on Fedora systems (like RedHat):
sudo dnf install postfix
sudo systemctl start postfix
When the application sends an email to a local user, such as <OS user>@localhost, then you can see the email sent at:
sudo vi /var/mail/<OS user>
Access application server from the browser
Use the following URL(s) to access your application server from a client like browser (see
request_URL). Use actual IP or web address instead of 127.0.0.1 if different.
#Display a send-mail form
http://127.0.0.1/sendmail/mail?action=show_form
Note: if your server is on the Internet and it has a firewall, you may need to allow HTTP traffic - see
ufw,
firewall-cmd etc.
Files
You are now done with the example! What follows are the source files in this project so you can examine how it works:
Send email on the web (mail.vely)
Here, the user can enter information necessary to send an email, such as the recipient email, the subject and message etc. The form to do this is displayed with sub
request "show_form". When the user clicks Submit, the request is sent back with subrequest "submit_form" which uses
input-param to collect all the user data, construct an email string and then execute "sendmail" program to send email.
Note that you must have an MTA (Mail Transfer Agent) installed (such as postfix or sendmail), and your computer must be authorized to send email on the internet. Otherwise you can test this by sending emails to your localhost.
2.0
2018
#include "vely.h"
void mail()
{
out-header default
input-param action
if (!strcmp (action, "show_form")) {
@<h2>Enter email and click Send to send it</h2>
@Note: 'From' field must be the email address from the domain of your server.<br/><br/>
@<form action="<<p-path>>/mail" method="POST">
@ <input type="hidden" name="action" value="submit_form">
@ <label for="from_mail">From:</label><br>
@ <input type="text" name="from_mail" value=""><br>
@ <label for="to_mail">To:</label><br>
@ <input type="text" name="to_mail" value=""><br><br>
@ <label for="subject_mail">Subject:</label><br>
@ <input type="text" name="subject_mail" value=""><br><br>
@ <label for="message">Message:</label><br>
@ <textarea name="message" rows="3" columns="50"></textarea>
@ <br/><br/>
@ <input type="submit" value="Send">
@</form>
} else if (!strcmp (action, "submit_form")) {
input-param from_mail
input-param to_mail
input-param message
input-param subject_mail
write-string define msg
@From: <<p-out from_mail>>
@To: <<p-out to_mail>>
@Subject: <<p-out subject_mail>>
@
<<p-out message>>
end-write-string
num st;
exec-program "/usr/sbin/sendmail" args "-i", "-t" input msg status st
if (st!=0) {
@Could not send email!
} else {
@Email sent!
}
@<hr/>
} else {
@Unrecognized action!<hr/>
}
}
See also
Examples (
example_cookies example_create_table example_docker example_file_manager example_form example_hash example_hello_world example_json example_multitenant_SaaS examples example_sendmail example_shopping example_stock example_utility example_write_report )
SEE ALL (
documentation)