I wrote a json2smtp proxy server that can be self hosted in Go (golang)
See here:
https://www.c2kb.com/json2smtp
https://github.com/caviv/json2smtp
The reason why I needed to write an email proxy was because I was called to do some maintenance on an old legacy system. This system was running on Windows Server 2012, with php version 5.5.6 and Apache 2.4. Very old versions of software. The system was sending emails using the php smtp package and old libssl.dll which supports only TLSv1.0. The smtp service they were using (AWS SES Simple Email Service) has recently dropped the support for SMTP connection via TLS version 1.0. Now in order to support TLS v1.2 for emails on smtp I have to upgrade the whole system. Windows version, php version, Apache version and more - this would have taken me a very long time with many obstacles on the way.
Exim is another popular open-source mail server that can be used as an email proxy. It can be configured to receive emails in JSON format and send emails through its own SMTP servers. | might be ok but very hard to configure.
Mailcow Dockerized is a Docker image that contains a number of email server components, including Postfix, Dovecot, and MySQL. It can be configured to receive emails in JSON format and send emails through its own SMTP servers. | Couldn't make it work
An email proxy: input: json, output: smtp call
For a legacy project I needed to have a proxy email that reads json input and execute a smtp calls in order to send emails. So I created a small proxy for emails in go (golang)
See the code on github: https://github.com/caviv/json2smtp
Download json2smtp binaries executables
Ubuntu linux: json2smtp
Windows amd64: json2smtp-amd64.exe
How it works:
Simple calling diagram:
The json struct object
Simple object:
curl -X POST \
-H "Content-Type: application/json" \
-d '{ \
"from": "john doe <john@example.com&rt;", \
"to": ["kermit@muppets.com", "oneperson@example.com"], \
"cc": ["email1@example.com"], \
"bcc": ["secret@example.com"], \
"subject": "email subject line", \
"message": "message body in text/html to be sent", \
"attachments": {"filename.pdf": "base64 file encoded",
"anotherfilename.txt": "base64 file encoded"}, \
}' \
http://localhost:8080/
Full object with smtp data:
curl -X POST \
-H "Content-Type: application/json" \
-d '{ \
"from": "john doe <john@example.com&rt;", \
"to": ["kermit@muppets.com", "oneperson@ex", \
"to": ["kermit@muppets.com", "oneperson@example.com"], \
"cc": ["email1@example.com"], \
"bcc": ["secret@example.com"], \
"subject": "email subject line", \
"message": "message body in text/html to be sent", \
"attachments": {"filename.pdf": "base64 file encoded",
"anotherfilename.txt": "base64 file encoded"}, \
"smtphost": "smtp.example.com - optional parameter", \
"smtpport": 587 - optional paramater, \
"smtpuser": "username - optional parameter", \
"smtppassword": "password - optional parameter" \
}' \
http://localhost:8080/
Attachments
In order to send attachments with your json email struct you need to construct an object of base64 encoded string of your binary file.How to install email proxy server:
Download the code and run it
git clone https://github.com/caviv/json2smtp.git
go run ./
go run ./ --help
Build and compile
Download the code compile it and run with help command
git clone https://github.com/caviv/json2smtp.git
go build ./
./json2smtp --help
Execute smtp2json emal proxy and samples
Command line help:
json2smtp utility https://www.c2kb.com/json2smtp v1.0.1 2023-11-13
Get json input and calls smtp - function as a json to smtp proxy
Options:
-port int
the port to listen on (default 8080)
-smtphost string
smtp host, e.g. smtp.example.com
-smtpoverride
true - allows to pass smtp parameters in the json call,
false will always use the config smtp data (default true)
-smtppassword string
password for the smtp user
-smtpport int
the port to listen on (default 587)
-smtpuser string
username for the smtp
Example:
json2smtp --port=8200 --smtphost='smtp.example.com'
--smtpport=587 --smtpuser='username'
--smtppassword='password'
--smtpoverride=false
Run service in the background:
nohup json2smtp --port=8200 --smtphost='smtp.example.com'
--smtpport=587 --smtpuser='username'
--smtppassword='password' >> logfile.log 2>&1 &
Simple execute smtp2json service:
In this way the calling client will have to pass the smtp server details in each call because we don't set the smtp default server for the proxy. The default port to listen on is 8080.
json2smtp
Recommended architecture to run the service:
Libraries used
This external libraries are used in the project:
require gopkg.in/mail.v2 v2.3.1
require gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0
Comments
Post a Comment