go-gomail / gomail

The best way to send emails in Go.
MIT License
4.34k stars 574 forks source link

Attach PDF #179

Closed rngallen closed 1 year ago

rngallen commented 1 year ago

How can I attach pdf which are not from disk?


func main(){

    pdf := gofpdf.New("L", "mm", "A5", "")
    pdf.SetProtection(gofpdf.CnProtectPrint, pin, "")
    fmt.Println(pin)
    pdf.SetMargins(10, 5, 10)
    pdf.AddPage()

    pdf.SetFont("Helvetica", "", 8)
    pdf.SetX(95)
    pdf.CellFormat(20, 4, "Payslip", "", 1, "C", false, 0, "")

       d := gomail.NewDialer(Host, Port, Username, Password)

    s, err := d.Dial()
    if err != nil {
        panic(err.Error())
    }

    m := gomail.NewMessage()
        m.SetHeader("From", Username)
        m.SetHeader("To", "abc@efg.com")
    m.SetHeader("Subject", "Sample")
    m.SetBody("text/html","Sample body")
    m.Attach(pdf)                            //<=== create pdf should be attached here
        if err := gomail.Send(s, m); err != nil {
            log.Printf("Could not send email to %q: %v", "abc@efg.com", err)
    }

}```
wneessen commented 1 year ago

This package does not support attachments from io.Readers. You either need to store the file temporarily on disk and attach it or use a package like github.com/wneessen/go-mail which has support for io.Reader (assuming that pdf offers a io.Reader access).

rngallen commented 1 year ago

I used the same approach but i forgot to close the issue