Skip to content

Commit

Permalink
Add support for seting e-mail author name
Browse files Browse the repository at this point in the history
Add "{author}" as a format specifier in the NameFormat configuration
setting.
  • Loading branch information
jamesd committed Jan 15, 2021
1 parent 64152f6 commit c042a3b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,28 @@ type Email struct {
Body string
}

func (email *Email) setFrom(feedName string, feedTitle string, account config.AccountConfig, conf *config.GrueConfig) {
r := strings.NewReplacer("{name}", feedName, "{title}", feedTitle)
func (email *Email) setFrom(feedName string, feed *gofeed.Feed, item *gofeed.Item, account config.AccountConfig, conf *config.GrueConfig) {
var author gofeed.Person
if item.Author != nil {
author = *item.Author
} else if feed.Author != nil {
author = *feed.Author
}
if author.Name == "" {
author.Name = feedName
}
r := strings.NewReplacer("{name}", feedName, "{title}", feed.Title,
"{author}", author.Name)
if account.NameFormat != nil {
email.FromName = r.Replace(*account.NameFormat)
} else {
email.FromName = r.Replace(conf.NameFormat)
}
email.FromAddress = conf.FromAddress
if author.Email == "" {
email.FromAddress = conf.FromAddress
} else {
email.FromAddress = author.Email
}
}

func (email *Email) setUserAgent(conf *config.GrueConfig) {
Expand Down Expand Up @@ -99,9 +113,9 @@ func (email *Email) format() *gomail.Message {
return m
}

func createEmail(feedName string, feedTitle string, item *gofeed.Item, date time.Time, account config.AccountConfig, conf *config.GrueConfig) *Email {
func createEmail(feedName string, feed *gofeed.Feed, item *gofeed.Item, date time.Time, account config.AccountConfig, conf *config.GrueConfig) *Email {
email := new(Email)
email.setFrom(feedName, feedTitle, account, conf)
email.setFrom(feedName, feed, item, account, conf)
email.Recipient = conf.Recipient
email.Subject = item.Title
email.Date = date
Expand Down
2 changes: 1 addition & 1 deletion rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func fetchFeed(fp FeedFetcher, feedName string, account *RSSFeed, config *config
_, exists := guids[item.GUID]
date, newer := hasNewerDate(item, account.LastFetched)
if !exists || (item.GUID == "" && newer == DateNewer) {
e := createEmail(feedName, feed.Title, item, date, account.config, config)
e := createEmail(feedName, feed, item, date, account.config, config)
err = e.Send()
}
if err == nil {
Expand Down

0 comments on commit c042a3b

Please sign in to comment.