更新
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package mailx_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
mailx "code.yun.ink/pkg/mailx"
|
||||
)
|
||||
|
||||
func ExampleNewMessage() {
|
||||
msg := mailx.NewMessage().
|
||||
From("noreply@example.com").
|
||||
To("user@example.com").
|
||||
Cc("cc@example.com").
|
||||
Subject("Hello").
|
||||
HTML("<h1>Hello</h1>").
|
||||
ReplyTo("support@example.com").
|
||||
AttachBytes("report.txt", []byte("data")).
|
||||
Build()
|
||||
|
||||
fmt.Println(msg.From, msg.To[0], msg.Subject, len(msg.Attachments))
|
||||
// Output: noreply@example.com user@example.com Hello 1
|
||||
}
|
||||
|
||||
func ExampleManager_Send() {
|
||||
mgr := mailx.NewManager()
|
||||
_ = mgr.Register(newMockSender("mock"))
|
||||
|
||||
err := mgr.Send(context.Background(), mailx.NewMessage().
|
||||
To("user@example.com").
|
||||
Subject("Hello").
|
||||
Build())
|
||||
fmt.Println("send err:", err)
|
||||
// Output: send err: <nil>
|
||||
}
|
||||
|
||||
func ExampleManager_SendWith() {
|
||||
mgr := mailx.NewManager()
|
||||
_ = mgr.Register(newMockSender("smtp"))
|
||||
_ = mgr.Register(newMockSender("aliyun"))
|
||||
_ = mgr.SetDefault("smtp")
|
||||
|
||||
ctx := context.Background()
|
||||
msg := mailx.NewMessage().To("user@example.com").Subject("Hi").Build()
|
||||
|
||||
_ = mgr.Send(ctx, msg) // 默认通道 smtp
|
||||
err := mgr.SendWith(ctx, "aliyun", msg)
|
||||
fmt.Println("send with aliyun err:", err)
|
||||
// Output: send with aliyun err: <nil>
|
||||
}
|
||||
|
||||
func ExampleManager_SendBy() {
|
||||
mgr := mailx.NewManager()
|
||||
|
||||
// 无需注册,发送时临时指定通道
|
||||
err := mgr.SendBy(context.Background(), newMockSender("temp"), mailx.NewMessage().
|
||||
To("user@example.com").
|
||||
Subject("Hi").
|
||||
Build())
|
||||
fmt.Println("send by err:", err)
|
||||
// Output: send by err: <nil>
|
||||
}
|
||||
|
||||
func ExampleMessage_Validate() {
|
||||
msg := mailx.NewMessage().Subject("no recipients").Build()
|
||||
err := msg.Validate()
|
||||
fmt.Println(errors.Is(err, mailx.ErrInvalidMessage))
|
||||
fmt.Println(err)
|
||||
// Output:
|
||||
// true
|
||||
// mailx: invalid message: requires at least one recipient
|
||||
}
|
||||
Reference in New Issue
Block a user