Add subtitle

This commit is contained in:
Ben Visness 2023-09-16 18:10:06 -05:00
parent 13389fa2db
commit e74d278f55
2 changed files with 26 additions and 17 deletions

View File

@ -13,7 +13,8 @@ Sends out newsletters
2. Create an email file 2. Create an email file
* Name it whatever you want. * Name it whatever you want.
* The first line of the file will be used as {{ subject }} in the postmark template. * The first line of the file will be used as {{ subject }} in the postmark template.
* The rest of the file will be used as {{{ content_body }}}. * The second line of the file, if not empty, will be used as {{ subtitle }} in the postmark template.
* After a blank line, the rest of the file will be used as {{{ content_body }}}.
* Newlines between the subject and body will be removed. * Newlines between the subject and body will be removed.
3. Do a test run 3. Do a test run

40
main.go
View File

@ -75,14 +75,19 @@ func verifyConfig() (Config, bool) {
return config, valid return config, valid
} }
func parseMailFile(contents string) (subject string, body string, valid bool) { func parseMailFile(contents string) (subject string, subtitle string, body string, valid bool) {
valid = false valid = false
parts := strings.SplitN(string(contents), "\n", 2) parts := strings.SplitN(string(contents), "\n\n", 2)
if len(parts) < 2 { if len(parts) < 2 {
fmt.Printf("File does not contain subject and body\n") fmt.Printf("File does not contain subject and body\n")
return return
} }
subject = strings.TrimSpace(parts[0]) subjectAndSubtitle := strings.TrimSpace(parts[0])
subjectParts := strings.SplitN(subjectAndSubtitle, "\n", 2)
subject = subjectParts[0]
if len(subjectParts) > 1 {
subtitle = subjectParts[1]
}
rawBody := strings.Trim(parts[1], "\n") rawBody := strings.Trim(parts[1], "\n")
if subject == "" { if subject == "" {
fmt.Printf("Subject is empty\n") fmt.Printf("Subject is empty\n")
@ -108,7 +113,7 @@ func sendTest(newsletterFile string) {
} }
return return
} }
subject, body, valid := parseMailFile(string(contents)) subject, subtitle, body, valid := parseMailFile(string(contents))
if !valid { if !valid {
return return
} }
@ -117,7 +122,7 @@ func sendTest(newsletterFile string) {
logFile := newsletterFile + "." + time.Now().Format("20060102T150405") + ".test.log" logFile := newsletterFile + "." + time.Now().Format("20060102T150405") + ".test.log"
os.Truncate(trackingFile, 0) os.Truncate(trackingFile, 0)
blastMail(cfg, logFile, trackingFile, audience, subject, body) blastMail(cfg, logFile, trackingFile, audience, subject, subtitle, body)
sha1File := newsletterFile + ".sha1" sha1File := newsletterFile + ".sha1"
sum := sha1.Sum(contents) sum := sha1.Sum(contents)
@ -139,7 +144,7 @@ func sendNews(audienceFile string, newsletterFile string) {
} }
return return
} }
subject, body, valid := parseMailFile(string(contents)) subject, subtitle, body, valid := parseMailFile(string(contents))
if !valid { if !valid {
return return
} }
@ -180,10 +185,10 @@ func sendNews(audienceFile string, newsletterFile string) {
trackingFile := newsletterFile + ".track" trackingFile := newsletterFile + ".track"
logFile := newsletterFile + "." + time.Now().Format("20060102T150405") + ".log" logFile := newsletterFile + "." + time.Now().Format("20060102T150405") + ".log"
blastMail(cfg, logFile, trackingFile, audience, subject, body) blastMail(cfg, logFile, trackingFile, audience, subject, subtitle, body)
} }
func blastMail(cfg Config, logFile string, trackingFile string, audience []string, subject string, body string) { func blastMail(cfg Config, logFile string, trackingFile string, audience []string, subject, subtitle, body string) {
log, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) log, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {
fmt.Printf("Can't open log file %s: %v\n", logFile, err) fmt.Printf("Can't open log file %s: %v\n", logFile, err)
@ -218,7 +223,7 @@ func blastMail(cfg Config, logFile string, trackingFile string, audience []strin
if !found { if !found {
group = append(group, a) group = append(group, a)
if len(group) == cfg.BatchSize { if len(group) == cfg.BatchSize {
results, err := sendMail(cfg, group, subject, body) results, err := sendMail(cfg, group, subject, subtitle, body)
if err != nil { if err != nil {
fmt.Printf("Error while sending mail: %v\n", err) fmt.Printf("Error while sending mail: %v\n", err)
return return
@ -233,7 +238,7 @@ func blastMail(cfg Config, logFile string, trackingFile string, audience []strin
} }
} }
if len(group) > 0 { if len(group) > 0 {
results, err := sendMail(cfg, group, subject, body) results, err := sendMail(cfg, group, subject, subtitle, body)
if err != nil { if err != nil {
fmt.Printf("Error while sending mail: %v\n", err) fmt.Printf("Error while sending mail: %v\n", err)
return return
@ -249,8 +254,9 @@ func blastMail(cfg Config, logFile string, trackingFile string, audience []strin
var postmarkClient = http.Client{} var postmarkClient = http.Client{}
type PostmarkTemplateModel struct { type PostmarkTemplateModel struct {
Subject string `json:"subject"` Subject string `json:"subject"`
Body string `json:"content_body"` Subtitle string `json:"subtitle,omitempty"`
Body string `json:"content_body"`
} }
type PostmarkTemplateMessage struct { type PostmarkTemplateMessage struct {
@ -272,7 +278,7 @@ type PostmarkBatchResult struct {
Message string `json:"Message"` Message string `json:"Message"`
} }
func sendMail(cfg Config, recipients []string, subject, contentHtml string) ([]PostmarkBatchResult, error) { func sendMail(cfg Config, recipients []string, subject, subtitle, contentHtml string) ([]PostmarkBatchResult, error) {
fmt.Printf("Sending batch [%d recipients]...", len(recipients)) fmt.Printf("Sending batch [%d recipients]...", len(recipients))
from := cfg.FromAddress from := cfg.FromAddress
if cfg.FromName != "" { if cfg.FromName != "" {
@ -285,8 +291,9 @@ func sendMail(cfg Config, recipients []string, subject, contentHtml string) ([]P
To: r, To: r,
TemplateAlias: cfg.PostmarkTemplateAlias, TemplateAlias: cfg.PostmarkTemplateAlias,
TemplateModel: PostmarkTemplateModel{ TemplateModel: PostmarkTemplateModel{
Subject: subject, Subject: subject,
Body: contentHtml, Subtitle: subtitle,
Body: contentHtml,
}, },
TrackOpens: false, TrackOpens: false,
TrackLinks: "None", TrackLinks: "None",
@ -342,7 +349,8 @@ func main() {
2. Create an email file 2. Create an email file
* Name it whatever you want. * Name it whatever you want.
* The first line of the file will be used as {{ subject }} in the postmark template. * The first line of the file will be used as {{ subject }} in the postmark template.
* The rest of the file will be used as {{{ content_body }}}. * The second line of the file, if not empty, will be used as {{ subtitle }} in the postmark template.
* After a blank line, the rest of the file will be used as {{{ content_body }}}.
* Newlines between the subject and body will be removed. * Newlines between the subject and body will be removed.
3. Do a test run 3. Do a test run