Getting response of smtp server

I am using golang net/smtp to send mails

Whenever I send to my smtp server I need to capture the response from the server Especially the DSN

For example my local smtp server gives a "ok queued as " at the end of the mail

I need to capture this and print in the logs

How can I do this

package main

import (
    "log"
    "net/smtp"
)

func sendEmail(msg []byte) {
    c, err := smtp.Dial("localhost:25")
    if err != nil {
        log.Fatal(err)
    }
    if err := c.Mail("sender@example.org"); err != nil {
        log.Fatal(err)
    }

    if err := c.Rcpt("recipient@example.net"); err != nil {
        log.Fatal(err)
    }
    wc, err := c.Data()
    if err != nil {
        log.Fatal(err)
    }

    _, err = wc.Write(msg)
    if err != nil {
        log.Fatal(err)
    }

    //How do I get the response here ??
    err = wc.Close()
    if err != nil {
        log.Fatal(err)
    }

    err = c.Quit()
    if err != nil {
        log.Fatal(err)
    }
}

Do you mean a response like this?

S: 250 Ok: queued as 12345

The methods of Client don’t extract the message phrase and they even abstract the return codes into err being nil or not. So what you ask for is not possible using the net/smtp package.

Since the smtp package is frozen and is not accepting new features, you may search for an alternative package at https://godoc.org/?q=smtp.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.