b3c22c261e
Add explicit TLS modes, context-aware delivery, and typed transport errors. Preserve raw MIME messages and cover the new delivery paths with local SMTP tests.
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"log"
|
|
"net/smtp"
|
|
"time"
|
|
|
|
"gitea.wittrail.com/urko/emailsender/pkg/email"
|
|
)
|
|
|
|
func main() {
|
|
emailService, err := email.New(email.Config{
|
|
Auth: smtp.PlainAuth("", "your@email.com", "your-password", "smtp.youremail.com"),
|
|
Host: "smtp.youremail.com",
|
|
Port: "587",
|
|
From: "your@email.com",
|
|
TLSMode: email.TLSModeSTARTTLS,
|
|
ConnectTimeout: 10 * time.Second,
|
|
OperationTimeout: 30 * time.Second,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
err = emailService.SendEmailContext(ctx, email.MessageWithAttachments{
|
|
To: "other@email.com",
|
|
Subject: "Test Email",
|
|
Body: "<html><body><p>Here your body, you can attach as html<p/></body></html>",
|
|
Attachments: []email.EmailAttachment{
|
|
{
|
|
File: bytes.NewBuffer([]byte("test")), // This is an io.Reader
|
|
Title: "document.pdf",
|
|
},
|
|
},
|
|
})
|
|
cancel()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|