Microservice for Distributing Email Messages
For the time being, Mailgun is too expensive so intentionally it was not implemented
Is tested to be working with a GMail account
These variables must be present in the environment
FROM is the sender address
FROM=someone@gmail.com
SENDEMAIL_DIRECTORY is the directory on a docker container where the emails are stored
SENDEMAIL_DIRECTORY=/emails
It is possible also to just create JSON files in a given directory and let a bash script send the emails using any schedule tool, such as crontab
SENDEMAIL_DIRECTORY=/home/ubuntu/emails
sendemail program has these dependencies that need to be installed before using it
sudo apt-get install -y sendemail libnet-ssleay-perl libio-socket-ssl-perl
Make use of the exported functions in email_utils package
To send sync email with immediate response on the outcome
func SendEmail(nc *nats.Conn, timeout time.Duration, params struct {
Recipients []string
Subject string
Body string
IsHTML bool
}) error
To send async email messages in a queue
func SendEmailQueue(sc stan.Conn, params struct {
Recipients []string
Subject string
Body string
IsHTML bool
}) error
type EmailMessage struct {
From string `json:"from"`
Tos []string `json:"tos"`
Subject string `json:"subject"`
Body string `json:"body"`
}
From a docker compose file
nats-server:
image: nats-streaming:0.12.2
restart: always
command: "--store=FILE --dir=/files/nats-data -m 8222"
expose:
- 4222
email-sender:
image: takasago/email-sender:latest
env_file:
- .env
command: "-v=2 -log_dir=/files -logtostderr"
volumes:
- $HOME/data/files:/files
sudo apt-get install -y sendemail libnet-ssleay-perl libio-socket-ssl-perl
To send a message
cat mailbody.txt | sendemail -l email.log \
-f "sender@domain.com" \
-u "Email Test - Ignore" \
-t "receiver@domain.com" \
-cc "receiver2@domain.com" \
-bcc "receiver3@domain.com" \
-s "smtp.gmail.com:587" \
-o tls=yes \
-xu "youremail@gmail.com" \
-xp "Email Password"
.env settings
# email settings
FROM=whatever@example.com
SENDEMAIL_DIRECTORY=/emails
Docker Compose Settings
email-sender:
image: takasago/email-sender:latest
env_file:
- .env
command: "-v=2 -log_dir=/files -logtostderr"
volumes:
- /home/ubuntu/data/files:/files
- /home/ubuntu/emails:/emails
Crontab
# process email messages each minute based in the file queue
* * * * * /usr/bin/pgrep -f $HOME/sender_crontab.sh > /dev/null 2> /dev/null || $HOME/sender_crontab.sh HOME/emails/ >> email.log
~$ cat sender_crontab.sh
#!/usr/bin/env bash
cd $HOME/emails
sudo $HOME/scripts/send.sh .
