|
1 | 1 | package gomail |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/domodwyer/mailyak" |
| 7 | + "github.com/mrz1836/go-logger" |
| 8 | +) |
| 9 | + |
| 10 | +// sendWithSmtp sends an email using the smtp service |
| 11 | +func (m *MailService) sendWithSmtp(email *Email) (err error) { |
| 12 | + |
| 13 | + // Create new mail message |
| 14 | + mail := mailyak.New(fmt.Sprintf("%s:%d", m.SmtpHost, m.SmtpPort), m.smtpAuth) |
| 15 | + |
| 16 | + // Add the to recipients |
| 17 | + mail.To(email.Recipients...) |
| 18 | + |
| 19 | + // Add the cc recipients |
| 20 | + if len(email.RecipientsCc) > 0 { |
| 21 | + mail.Cc(email.RecipientsCc...) |
| 22 | + } |
| 23 | + |
| 24 | + // Add the bcc recipients |
| 25 | + if len(email.RecipientsBcc) > 0 { |
| 26 | + mail.WriteBccHeader(true) |
| 27 | + mail.Bcc(email.RecipientsBcc...) |
| 28 | + } |
| 29 | + |
| 30 | + // Add the basics |
| 31 | + mail.From(email.FromAddress) |
| 32 | + mail.FromName(email.FromName) |
| 33 | + mail.Subject(email.Subject) |
| 34 | + |
| 35 | + // Add a custom reply to address |
| 36 | + if len(email.ReplyToAddress) > 0 { |
| 37 | + mail.ReplyTo(email.ReplyToAddress) |
| 38 | + } |
| 39 | + |
| 40 | + // Add plain text |
| 41 | + if len(email.PlainTextContent) > 0 { |
| 42 | + mail.Plain().Set(email.PlainTextContent) |
| 43 | + } |
| 44 | + |
| 45 | + // Add html |
| 46 | + if len(email.HTMLContent) > 0 { |
| 47 | + mail.HTML().Set(email.HTMLContent) |
| 48 | + } |
| 49 | + |
| 50 | + // Add any attachments |
| 51 | + if len(email.Attachments) > 0 { |
| 52 | + for _, att := range email.Attachments { |
| 53 | + mail.Attach(att.FileName, att.FileReader) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Add importance? |
| 58 | + if email.Important { |
| 59 | + mail.AddHeader("X-Priority", "1 (Highest)") |
| 60 | + mail.AddHeader("X-MSMail-Priority", "High") |
| 61 | + mail.AddHeader("Importance", "High") |
| 62 | + } |
| 63 | + |
| 64 | + // Warn about features that are set but not available |
| 65 | + if email.TrackClicks { |
| 66 | + logger.Data(2, logger.WARN, "track clicks is enabled, but aws ses does not offer this feature") |
| 67 | + } |
| 68 | + if email.TrackOpens { |
| 69 | + logger.Data(2, logger.WARN, "track opens is enabled, but aws ses does not offer this feature") |
| 70 | + } |
| 71 | + if email.AutoText { |
| 72 | + logger.Data(2, logger.WARN, "auto text is enabled, but aws ses does not offer this feature") |
| 73 | + } |
| 74 | + |
| 75 | + // Send via smtp |
| 76 | + return mail.Send() |
| 77 | +} |
0 commit comments