Skip to content

Commit

Permalink
DE-1343 Create func NewMessage and NewMIMEMessage (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtopc authored Oct 31, 2024
1 parent 2285d22 commit cf1ace9
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 101 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func main() {
recipient := "[email protected]"

// The message object allows you to add attachments and Bcc recipients
message := mg.NewMessage(sender, subject, body, recipient)
message := mailgun.NewMessage(sender, subject, body, recipient)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

// Send the message with a 10 second timeout
// Send the message with a 10-second timeout
resp, id, err := mg.Send(ctx, message)

if err != nil {
Expand Down Expand Up @@ -281,7 +281,7 @@ func main() {
subject := "HTML email!"
recipient := "[email protected]"

message := mg.NewMessage(sender, subject, "", recipient)
message := mailgun.NewMessage(sender, subject, "", recipient)
body := `
<html>
<body>
Expand All @@ -292,7 +292,7 @@ func main() {
</html>
`

message.SetHtml(body)
message.SetHTML(body)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
Expand Down Expand Up @@ -342,7 +342,7 @@ func main() {
recipient := "[email protected]"

// The message object allows you to add attachments and Bcc recipients
message := mg.NewMessage(sender, subject, body, recipient)
message := mailgun.NewMessage(sender, subject, body, recipient)
message.SetTemplate("passwordReset")
err := message.AddTemplateVariable("passwordResetLink", "some link to your site unique to your user")
if err != nil {
Expand All @@ -352,9 +352,8 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

// Send the message with a 10 second timeout
// Send the message with a 10-second timeout
resp, id, err := mg.Send(ctx, message)

if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestMultipleAttachments(t *testing.T) {

var ctx = context.Background()

m := mg.NewMessage("root@"+testDomain, "Subject", "Text Body", "attachment@"+testDomain)
m := mailgun.NewMessage("root@"+testDomain, "Subject", "Text Body", "attachment@"+testDomain)

// Add 2 attachments
m.AddAttachment(createAttachment(t))
Expand Down
4 changes: 2 additions & 2 deletions events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestEventPoller(t *testing.T) {
}()

// Send an email
m := mg.NewMessage("root@"+testDomain, "Subject", "Text Body", "user@"+testDomain)
m := mailgun.NewMessage("root@"+testDomain, "Subject", "Text Body", "user@"+testDomain)
msg, id, err := mg.Send(ctx, m)
ensure.Nil(t, err)

Expand Down Expand Up @@ -126,7 +126,7 @@ func ExampleMailgunImpl_ListEvents() {
for it.Next(ctx, &page) {
for _, e := range page {
// You can access some fields via the interface
//fmt.Printf("Event: '%s' TimeStamp: '%s'\n", e.GetName(), e.GetTimestamp())
// fmt.Printf("Event: '%s' TimeStamp: '%s'\n", e.GetName(), e.GetTimestamp())

// and you can act upon each event by type
switch event := e.(type) {
Expand Down
31 changes: 16 additions & 15 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package examples
import (
"context"
"fmt"
"github.com/mailgun/mailgun-go/v4"
"github.com/mailgun/mailgun-go/v4/events"
"os"
"time"

"github.com/mailgun/mailgun-go/v4"
"github.com/mailgun/mailgun-go/v4/events"
)

func AddBounce(domain, apiKey string) error {
Expand Down Expand Up @@ -696,15 +697,15 @@ func ResendMessage(domain, apiKey string) (string, string, error) {

func SendComplexMessage(domain, apiKey string) (string, error) {
mg := mailgun.NewMailgun(domain, apiKey)
m := mg.NewMessage(
m := mailgun.NewMessage(
"Excited User <YOU@YOUR_DOMAIN_NAME>",
"Hello",
"Testing some Mailgun awesomeness!",
"[email protected]",
)
m.AddCC("[email protected]")
m.AddBCC("[email protected]")
m.SetHtml("<html>HTML version of the body</html>")
m.SetHTML("<html>HTML version of the body</html>")
m.AddAttachment("files/test.jpg")
m.AddAttachment("files/test.txt")

Expand All @@ -717,7 +718,7 @@ func SendComplexMessage(domain, apiKey string) (string, error) {

func SendWithConnectionOptions(domain, apiKey string) (string, error) {
mg := mailgun.NewMailgun(domain, apiKey)
m := mg.NewMessage(
m := mailgun.NewMessage(
"Excited User <YOU@YOUR_DOMAIN_NAME>",
"Hello",
"Testing some Mailgun awesomeness!",
Expand All @@ -736,15 +737,15 @@ func SendWithConnectionOptions(domain, apiKey string) (string, error) {

func SendInlineImage(domain, apiKey string) (string, error) {
mg := mailgun.NewMailgun(domain, apiKey)
m := mg.NewMessage(
m := mailgun.NewMessage(
"Excited User <YOU@YOUR_DOMAIN_NAME>",
"Hello",
"Testing some Mailgun awesomeness!",
"[email protected]",
)
m.AddCC("[email protected]")
m.AddBCC("[email protected]")
m.SetHtml(`<html>Inline image here: <img alt="image" src="cid:test.jpg"/></html>`)
m.SetHTML(`<html>Inline image here: <img alt="image" src="cid:test.jpg"/></html>`)
m.AddInline("files/test.jpg")

ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
Expand All @@ -756,7 +757,7 @@ func SendInlineImage(domain, apiKey string) (string, error) {

func SendMessageNoTracking(domain, apiKey string) (string, error) {
mg := mailgun.NewMailgun(domain, apiKey)
m := mg.NewMessage(
m := mailgun.NewMessage(
"Excited User <YOU@YOUR_DOMAIN_NAME>",
"Hello",
"Testing some Mailgun awesomeness!",
Expand All @@ -778,7 +779,7 @@ func SendMimeMessage(domain, apiKey string) (string, error) {
return "", err
}

m := mg.NewMIMEMessage(mimeMsgReader, "[email protected]")
m := mailgun.NewMIMEMessage(mimeMsgReader, "[email protected]")

ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
Expand All @@ -789,7 +790,7 @@ func SendMimeMessage(domain, apiKey string) (string, error) {

func SendScheduledMessage(domain, apiKey string) (string, error) {
mg := mailgun.NewMailgun(domain, apiKey)
m := mg.NewMessage(
m := mailgun.NewMessage(
"Excited User <YOU@YOUR_DOMAIN_NAME>",
"Hello",
"Testing some Mailgun awesomeness!",
Expand All @@ -806,7 +807,7 @@ func SendScheduledMessage(domain, apiKey string) (string, error) {

func SendSimpleMessage(domain, apiKey string) (string, error) {
mg := mailgun.NewMailgun(domain, apiKey)
m := mg.NewMessage(
m := mailgun.NewMessage(
"Excited User <mailgun@YOUR_DOMAIN_NAME>",
"Hello",
"Testing some Mailgun awesomeness!",
Expand All @@ -822,7 +823,7 @@ func SendSimpleMessage(domain, apiKey string) (string, error) {

func SendTaggedMessage(domain, apiKey string) (string, error) {
mg := mailgun.NewMailgun(domain, apiKey)
m := mg.NewMessage(
m := mailgun.NewMessage(
"Excited User <YOU@YOUR_DOMAIN_NAME>",
"Hello",
"Testing some Mailgun awesomeness!",
Expand All @@ -843,12 +844,12 @@ func SendTaggedMessage(domain, apiKey string) (string, error) {

func SendTemplateMessage(domain, apiKey string) (string, error) {
mg := mailgun.NewMailgun(domain, apiKey)
m := mg.NewMessage(
m := mailgun.NewMessage(
"Excited User <YOU@YOUR_DOMAIN_NAME>",
"Hey %recipient.first%",
"If you wish to unsubscribe, click http://mailgun/unsubscribe/%recipient.id%",
) // IMPORTANT: No To:-field recipients!

// Set template to be applied to this message.
m.SetTemplate("my-template")

Expand Down Expand Up @@ -937,7 +938,7 @@ func SendMessageWithTemplate(domain, apiKey string) error {
time.Sleep(time.Second * 1)

// Create a new message with template
m := mg.NewMessage("Excited User <[email protected]>", "Template example", "")
m := mailgun.NewMessage("Excited User <[email protected]>", "Template example", "")
m.SetTemplate("my-template")

// Add recipients
Expand Down
8 changes: 4 additions & 4 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -80,7 +80,7 @@ func ExampleMailgunImpl_Send_constructed() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

m := mg.NewMessage(
m := mailgun.NewMessage(
"Excited User <[email protected]>",
"Hello World",
"Testing some Mailgun Awesomeness!",
Expand All @@ -89,7 +89,7 @@ func ExampleMailgunImpl_Send_constructed() {
)
m.SetTracking(true)
m.SetDeliveryTime(time.Now().Add(24 * time.Hour))
m.SetHtml("<html><body><h1>Testing some Mailgun Awesomeness!!</h1></body></html>")
m.SetHTML("<html><body><h1>Testing some Mailgun Awesomeness!!</h1></body></html>")
_, id, err := mg.Send(ctx, m)
if err != nil {
log.Fatal(err)
Expand All @@ -112,7 +112,7 @@ Testing some Mailgun MIME awesomeness!
defer cancel()

mg := mailgun.NewMailgun("example.com", "my_api_key")
m := mg.NewMIMEMessage(ioutil.NopCloser(strings.NewReader(exampleMime)), "[email protected]")
m := mailgun.NewMIMEMessage(io.NopCloser(strings.NewReader(exampleMime)), "[email protected]")
_, id, err := mg.Send(ctx, m)
if err != nil {
log.Fatal(err)
Expand Down
4 changes: 3 additions & 1 deletion mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ type Mailgun interface {
AddOverrideHeader(k string, v string)
GetCurlOutput() string

Send(ctx context.Context, m *Message) (string, string, error)
Send(ctx context.Context, m *Message) (mes string, id string, err error)
ReSend(ctx context.Context, id string, recipients ...string) (string, string, error)
// Deprecated: use func NewMessage instead of method.
NewMessage(from, subject, text string, to ...string) *Message
// Deprecated: use func NewMIMEMessage instead of method.
NewMIMEMessage(body io.ReadCloser, to ...string) *Message

ListBounces(opts *ListOptions) *BouncesIterator
Expand Down
Loading

0 comments on commit cf1ace9

Please sign in to comment.