add transactions to email body

This commit is contained in:
Peter Rounce 2022-09-25 10:33:24 +00:00
parent ab1b7c464f
commit 94267c47cf
2 changed files with 112 additions and 10 deletions

View file

@ -9,19 +9,65 @@ import (
log "github.com/sirupsen/logrus"
"os"
"strconv"
"strings"
)
func send_balance_email(recipient_email string, card_id int) {
c, err := db_get_card_from_card_id(card_id)
if err != nil {
log.Warn(err.Error())
return
}
card_total_sats, err := db_get_card_total(card_id)
if err != nil {
log.Warn(err.Error())
return
}
txs, err := db_get_card_txs(card_id)
if err != nil {
log.Warn(err.Error())
return
}
subject := c.card_name + " balance = " + strconv.Itoa(card_total_sats) + " sats"
// add transactions to the email body
var html_body_sb strings.Builder
var text_body_sb strings.Builder
html_body_sb.WriteString("<!DOCTYPE html><html><head><style> table, " +
"th, td { border: 1px solid black; border-collapse: collapse; } " +
"</style></head><body>")
html_body_sb.WriteString("<h3>transactions</h3><table><tr><th>date</th><th>action</th><th>amount</th>")
text_body_sb.WriteString("transactions\n\n")
for _, tx := range txs {
html_body_sb.WriteString(
"<tr>" +
"<td>" + tx.tx_time + "</td>" +
"<td>" + tx.tx_type + "</td>" +
"<td style='text-align:right'>" + strconv.Itoa(tx.tx_amount_msats / 1000) + "</td>" +
"</tr>")
text_body_sb.WriteString(tx.tx_type +
" " + strconv.Itoa(tx.tx_amount_msats / 1000))
}
html_body_sb.WriteString("</table></body></html>")
html_body := html_body_sb.String()
text_body := text_body_sb.String()
send_email(recipient_email,
"bolt card balance: " + strconv.Itoa(card_total_sats) + " sats",
"html body",
"text body")
subject,
html_body,
text_body)
}
// https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/ses-example-send-email.html