NewMailClient creates and returns a new SMTP or Sendmail client based on the current app settings.
()
| 618 | // NewMailClient creates and returns a new SMTP or Sendmail client |
| 619 | // based on the current app settings. |
| 620 | func (app *BaseApp) NewMailClient() mailer.Mailer { |
| 621 | var client mailer.Mailer |
| 622 | |
| 623 | // init mailer client |
| 624 | if app.Settings().SMTP.Enabled { |
| 625 | client = &mailer.SMTPClient{ |
| 626 | Host: app.Settings().SMTP.Host, |
| 627 | Port: app.Settings().SMTP.Port, |
| 628 | Username: app.Settings().SMTP.Username, |
| 629 | Password: app.Settings().SMTP.Password, |
| 630 | TLS: app.Settings().SMTP.TLS, |
| 631 | AuthMethod: app.Settings().SMTP.AuthMethod, |
| 632 | LocalName: app.Settings().SMTP.LocalName, |
| 633 | } |
| 634 | } else { |
| 635 | client = &mailer.Sendmail{} |
| 636 | } |
| 637 | |
| 638 | // register the app level hook |
| 639 | if h, ok := client.(mailer.SendInterceptor); ok { |
| 640 | h.OnSend().Bind(&hook.Handler[*mailer.SendEvent]{ |
| 641 | Id: "__pbMailerOnSend__", |
| 642 | Func: func(e *mailer.SendEvent) error { |
| 643 | appEvent := new(MailerEvent) |
| 644 | appEvent.App = app |
| 645 | appEvent.Mailer = client |
| 646 | appEvent.Message = e.Message |
| 647 | |
| 648 | return app.OnMailerSend().Trigger(appEvent, func(ae *MailerEvent) error { |
| 649 | e.Message = ae.Message |
| 650 | |
| 651 | // print the mail in the console to assist with the debugging |
| 652 | if app.IsDev() { |
| 653 | logDate := new(strings.Builder) |
| 654 | log.New(logDate, "", log.LstdFlags).Print() |
| 655 | |
| 656 | mailLog := new(strings.Builder) |
| 657 | mailLog.WriteString(strings.TrimSpace(logDate.String())) |
| 658 | mailLog.WriteString(" Mail sent\n") |
| 659 | fmt.Fprintf(mailLog, "├─ From: %v\n", ae.Message.From) |
| 660 | fmt.Fprintf(mailLog, "├─ To: %v\n", ae.Message.To) |
| 661 | fmt.Fprintf(mailLog, "├─ Cc: %v\n", ae.Message.Cc) |
| 662 | fmt.Fprintf(mailLog, "├─ Bcc: %v\n", ae.Message.Bcc) |
| 663 | fmt.Fprintf(mailLog, "├─ Subject: %v\n", ae.Message.Subject) |
| 664 | |
| 665 | if len(ae.Message.Attachments) > 0 { |
| 666 | attachmentKeys := make([]string, 0, len(ae.Message.Attachments)) |
| 667 | for k := range ae.Message.Attachments { |
| 668 | attachmentKeys = append(attachmentKeys, k) |
| 669 | } |
| 670 | fmt.Fprintf(mailLog, "├─ Attachments: %v\n", attachmentKeys) |
| 671 | } |
| 672 | |
| 673 | if len(ae.Message.InlineAttachments) > 0 { |
| 674 | attachmentKeys := make([]string, 0, len(ae.Message.InlineAttachments)) |
| 675 | for k := range ae.Message.InlineAttachments { |
| 676 | attachmentKeys = append(attachmentKeys, k) |
| 677 | } |