Three examples for logging and sending emails using smtp when receiving a POST request with a "mailto" key. Examples are in php, Node.js and Python (flask). The apps expect a posted JSON (content-type:application/json) in a format like:
{
"mailto": "[email protected]",
"message": "string",
"datetime": "string",
"location": {
"address": "string",
"latitude": float,
"longitude": float
}
}
The mailto
key/value pair is optional,
it can also be set as a GET argument (http(s)://your.server.com/prefix?mailto=[email protected]).
Libraries used for mail:
- php: phpmailer, licence: LGPL
- Node.js: nodemailer, licence: MIT
- Python: Flask-Mail, licence: BSD 3-clause
create a .env (see: .env.template) file in the same folder with index.(php/js/py) with:
SMTP_SERVER=smtp.eaxmple.com
SMTP_PORT=587
[email protected]
SMTP_PASSWORD=_secret_
SMTP_AUTH=true
SMTP_SECURE=tls|ssl
SMTP_FROM='User First Name - Last Name'
SMTP_MAIL_SUBJECT='Mail Subject'
cd node && yarn # or npm install
yarn start # or npm run start
make sure composer is installed
cd php && composer install
in virtualenv or not:
cd python && pip install -r requirements.txt
start server with
uwsgi --ini mailer.ini
# PHP:
root /path/to/simple-mailer/php;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass unix:///path/to/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# Python / uwsgi:
location / {
include uwsgi_params;
uwsgi_pass unix:///path/to/simple-mailer/python/uwsgi-mailer.sock;
uwsgi_ignore_client_abort on;
}
# NODE JS:
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://localhost:3000/;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}