DevAddict
DevAddict

Reputation: 2015

Is it possible to set both email body and text separately using wneessen/go-mail

I'm using wneessen/go-mail to send emails using go, but after I set the body HTML, the library updates the HTML text that is already set and if I set the body text after the body HTML, it will remove the body HTML. I'm using Hermes to generate emails and Hermes generates both text and HTML separately and I'm trying to set the text generated by Hermes as plain text.

mail/main.go:

package mail

import (
    "context"

    "github.com/matcornic/hermes/v2"
    "github.com/wneessen/go-mail"

    "github.com/Naisum/NaisumServer/internal/config"
)

var (
    Client *mail.Client
    Hermes hermes.Hermes
)

func Init() error {
    var err error
    Client, err = mail.NewClient(config.Get().SMTP.Host, mail.WithPort(config.Get().SMTP.Port), mail.WithTLSPolicy(config.Get().SMTP.TLS), mail.WithSMTPAuth(config.Get().SMTP.Auth), mail.WithUsername(config.Get().SMTP.User), mail.WithPassword(config.Get().SMTP.Pass))
    if err != nil {
        return err
    }

    err = Client.DialWithContext(context.Background())

    Hermes = hermes.Hermes{
        Product: hermes.Product{
            Name: config.Get().Hermes.Product.Name,
            Link: config.Get().Hermes.Product.Link,
            Logo: config.Get().Hermes.Product.Logo,
        },
    }

    return err
}

func makeMessage(to string, subject string) (*mail.Msg, error) {
    message := mail.NewMsg()
    if err := message.From(config.Get().SMTP.Email); err != nil {
        return nil, err
    }
    if err := message.To(to); err != nil {
        return nil, err
    }

    message.Subject(subject)

    return message, nil
}

func Send(to string, subject string, body string) error {
    message, err := makeMessage(to, subject)
    if err != nil {
        return err
    }

    message.SetBodyString(mail.TypeTextPlain, body)

    if err := Client.DialAndSend(message); err != nil {
        return err
    }

    return nil
}

func SendHTML(to string, subject string, html string, text string) error {
    message, err := makeMessage(to, subject)
    if err != nil {
        return err
    }

    message.SetBodyString(mail.TypeTextPlain, text)
    message.SetBodyString(mail.TypeTextHTML, html)

    if err := Client.DialAndSend(message); err != nil {
        return err
    }

    return nil
}

func Close() error {
    if err := Client.Close(); err != nil {
        return err
    }

    return nil
}

mail/send-welcome.go:

package mail

import (
    "github.com/matcornic/hermes/v2"

    "github.com/Naisum/NaisumServer/internal/models"
)

func SendWelcome(u *models.User) error {
    email := hermes.Email{
        Body: hermes.Body{
            Name: u.DisplayName,
            Intros: []string{
                "Welcome to Naisum! We're very excited to have you on board.",
            },
            Actions: []hermes.Action{
                {
                    Instructions: "To verify your email, please click here:",
                    Button: hermes.Button{
                        Color: "#22BC66",
                        Text:  "Verify your email",
                        Link:  "http://localhost:3000/test",
                    },
                },
            },
            Outros: []string{
                "Need help, or have any questions? Just reply to this email, we'd love to help.",
            },
        },
    }

    emailHTML, err := Hermes.GenerateHTML(email)
    if err != nil {
        return err
    }

    emailText, err := Hermes.GeneratePlainText(email)
    if err != nil {
        return err
    }

    err = SendHTML(*u.Email, "Welcome to Naisum!", emailHTML, emailText)
    if err != nil {
        return err
    }

    return nil
}

Upvotes: 1

Views: 40

Answers (1)

Winni
Winni

Reputation: 41

Using Msg.SetBodyString twice will override the first execution. For mails with alternative body options go-mail has Msg.AddAlternativeString which allows to add alternative body strings (see: https://pkg.go.dev/github.com/wneessen/go-mail#Msg.AddAlternativeString)

The bulk-mailer example provided in the go-mail wiki also shows the correct code for mixing HTML and plain text mails.

Btw. you can always asks questions in the discussion forum of the repo. This is much better monitored than SO.

Upvotes: 1

Related Questions