An email proxy: input: json, output: smtp call
For a legacy project I needed to have a proxy that reads json input and execute a smtp call in order to send emails. So I created a small proxy for emails in go (golang)
Read more about why I needed it here: https://www.c2kb.com/json2smtp
NEW: If you want to use an online service of json to smtp: https://www.json2smtp.net
Simple calling diagram
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"from": "john doe <john@example.com>",
"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/
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"from": "john doe <john@example.com>",
"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/
In order to send attachments with your json email struct you need to construct an object of base64 encoded string of your binary file.
Download the code and run it
git clone https://github.com/caviv/json2smtp.git
go run ./
go run ./ --help
Download the code compile it and run with help command
git clone https://github.com/caviv/json2smtp.git
go build ./
./json2smtp --help
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
json2smtp --port=8200 --smtphost='smtp.example.com' --smtpport=587 --smtpuser='username' --smtppassword='password' --smtpoverride=false
nohup json2smtp --port=8200 --smtphost='smtp.example.com' --smtpport=587 --smtpuser='username' --smtppassword='password' >> logfile.log 2>&1 &
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
https://www.c2kb.com/json2smtp
- Replace the mail.v2 package to a newer one - as recommended here: https://www.reddit.com/r/golang/comments/17w2l9s/comment/k9fve4r/
- Create delayed sending of emails / asynchronic sending - I think that could be useful
- Allow sending of plain/html and plain/text body in emails
( Look at the issues of this project: https://github.com/caviv/json2smtp/issues )
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
contact me on support at c2kb.com