|
1 | | -package main |
| 1 | +package examples |
2 | 2 |
|
3 | 3 | import ( |
4 | 4 | "context" |
@@ -33,7 +33,8 @@ func AddDomain(domain, apiKey string) (mailgun.DomainResponse, error) { |
33 | 33 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
34 | 34 | defer cancel() |
35 | 35 |
|
36 | | - return mg.CreateDomain(ctx, "example.com", "super_secret", &mailgun.CreateDomainOptions{ |
| 36 | + return mg.CreateDomain(ctx, "example.com", &mailgun.CreateDomainOptions{ |
| 37 | + Password: "super_secret", |
37 | 38 | SpamAction: mailgun.SpamActionTag, |
38 | 39 | Wildcard: false, |
39 | 40 | }) |
@@ -142,7 +143,8 @@ func CreateDomain(domain, apiKey string) (mailgun.DomainResponse, error) { |
142 | 143 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
143 | 144 | defer cancel() |
144 | 145 |
|
145 | | - return mg.CreateDomain(ctx, "example.com", "super_secret", &mailgun.CreateDomainOptions{ |
| 146 | + return mg.CreateDomain(ctx, "example.com", &mailgun.CreateDomainOptions{ |
| 147 | + Password: "super_secret", |
146 | 148 | SpamAction: mailgun.SpamActionTag, |
147 | 149 | Wildcard: false, |
148 | 150 | }) |
@@ -894,3 +896,190 @@ func VerifyWebhookSignature(domain, apiKey, timestamp, token, signature string) |
894 | 896 | Signature: signature, |
895 | 897 | }) |
896 | 898 | } |
| 899 | + |
| 900 | +func SendMessageWithTemplate(domain, apiKey string) error { |
| 901 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 902 | + var err error |
| 903 | + |
| 904 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 905 | + defer cancel() |
| 906 | + |
| 907 | + // Create a new template |
| 908 | + err = mg.CreateTemplate(ctx, &mailgun.Template{ |
| 909 | + Name: "my-template", |
| 910 | + Version: mailgun.TemplateVersion{ |
| 911 | + Template: `'<div class="entry"> <h1>{{.title}}</h1> <div class="body"> {{.body}} </div> </div>'`, |
| 912 | + Engine: mailgun.TemplateEngineGo, |
| 913 | + Tag: "v1", |
| 914 | + }, |
| 915 | + }) |
| 916 | + if err != nil { |
| 917 | + return err |
| 918 | + } |
| 919 | + |
| 920 | + // Give time for template to show up in the system. |
| 921 | + time.Sleep(time.Second * 1) |
| 922 | + |
| 923 | + // Create a new message with template |
| 924 | + m := mg.NewMessage("Excited User <excited@example.com>", "Template example", "") |
| 925 | + m.SetTemplate("my-template") |
| 926 | + |
| 927 | + // Add recipients |
| 928 | + m.AddRecipient("bob@example.com") |
| 929 | + m.AddRecipient("alice@example.com") |
| 930 | + |
| 931 | + // Add the variables to be used by the template |
| 932 | + m.AddVariable("title", "Hello Templates") |
| 933 | + m.AddVariable("body", "Body of the message") |
| 934 | + |
| 935 | + _, id, err := mg.Send(ctx, m) |
| 936 | + fmt.Printf("Queued: %s", id) |
| 937 | + return err |
| 938 | +} |
| 939 | + |
| 940 | +func CreateTemplate(domain, apiKey string) error { |
| 941 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 942 | + |
| 943 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 944 | + defer cancel() |
| 945 | + |
| 946 | + return mg.CreateTemplate(ctx, &mailgun.Template{ |
| 947 | + Name: "my-template", |
| 948 | + Version: mailgun.TemplateVersion{ |
| 949 | + Template: `'<div class="entry"> <h1>{{.title}}</h1> <div class="body"> {{.body}} </div> </div>'`, |
| 950 | + Engine: mailgun.TemplateEngineGo, |
| 951 | + Tag: "v1", |
| 952 | + }, |
| 953 | + }) |
| 954 | +} |
| 955 | + |
| 956 | +func DeleteTemplate(domain, apiKey string) error { |
| 957 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 958 | + |
| 959 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 960 | + defer cancel() |
| 961 | + |
| 962 | + return mg.DeleteTemplate(ctx, "my-template") |
| 963 | +} |
| 964 | + |
| 965 | +func UpdateTemplate(domain, apiKey string) error { |
| 966 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 967 | + |
| 968 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 969 | + defer cancel() |
| 970 | + |
| 971 | + return mg.UpdateTemplate(ctx, &mailgun.Template{ |
| 972 | + Name: "my-template", |
| 973 | + Description: "Add a description to the template", |
| 974 | + }) |
| 975 | +} |
| 976 | + |
| 977 | +func GetTemplate(domain, apiKey string) (mailgun.Template, error) { |
| 978 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 979 | + |
| 980 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 981 | + defer cancel() |
| 982 | + |
| 983 | + return mg.GetTemplate(ctx, "my-template") |
| 984 | +} |
| 985 | + |
| 986 | +func ListActiveTemplates(domain, apiKey string) ([]mailgun.Template, error) { |
| 987 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 988 | + it := mg.ListTemplates(&mailgun.ListTemplateOptions{Active: true}) |
| 989 | + |
| 990 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 991 | + defer cancel() |
| 992 | + |
| 993 | + var page, result []mailgun.Template |
| 994 | + for it.Next(ctx, &page) { |
| 995 | + result = append(result, page...) |
| 996 | + } |
| 997 | + |
| 998 | + if it.Err() != nil { |
| 999 | + return nil, it.Err() |
| 1000 | + } |
| 1001 | + return result, nil |
| 1002 | +} |
| 1003 | + |
| 1004 | +func ListTemplates(domain, apiKey string) ([]mailgun.Template, error) { |
| 1005 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 1006 | + it := mg.ListTemplates(nil) |
| 1007 | + |
| 1008 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 1009 | + defer cancel() |
| 1010 | + |
| 1011 | + var page, result []mailgun.Template |
| 1012 | + for it.Next(ctx, &page) { |
| 1013 | + result = append(result, page...) |
| 1014 | + } |
| 1015 | + |
| 1016 | + if it.Err() != nil { |
| 1017 | + return nil, it.Err() |
| 1018 | + } |
| 1019 | + return result, nil |
| 1020 | +} |
| 1021 | + |
| 1022 | +func AddTemplateVersion(domain, apiKey string) error { |
| 1023 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 1024 | + |
| 1025 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 1026 | + defer cancel() |
| 1027 | + |
| 1028 | + return mg.AddTemplateVersion(ctx, "my-template", &mailgun.TemplateVersion{ |
| 1029 | + Template: `'<div class="entry"> <h1>{{.title}}</h1> <div class="body"> {{.body}} </div> </div>'`, |
| 1030 | + Engine: mailgun.TemplateEngineGo, |
| 1031 | + Tag: "v2", |
| 1032 | + Active: true, |
| 1033 | + }) |
| 1034 | +} |
| 1035 | + |
| 1036 | +func DeleteTemplateVersion(domain, apiKey string) error { |
| 1037 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 1038 | + |
| 1039 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 1040 | + defer cancel() |
| 1041 | + |
| 1042 | + // Delete the template version tagged as 'v2' |
| 1043 | + return mg.DeleteTemplateVersion(ctx, "my-template", "v2") |
| 1044 | +} |
| 1045 | + |
| 1046 | +func GetTemplateVersion(domain, apiKey string) (mailgun.TemplateVersion, error) { |
| 1047 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 1048 | + |
| 1049 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 1050 | + defer cancel() |
| 1051 | + |
| 1052 | + // Get the template version tagged as 'v2' |
| 1053 | + return mg.GetTemplateVersion(ctx, "my-template", "v2") |
| 1054 | +} |
| 1055 | + |
| 1056 | +func UpdateTemplateVersion(domain, apiKey string) error { |
| 1057 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 1058 | + |
| 1059 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 1060 | + defer cancel() |
| 1061 | + |
| 1062 | + return mg.UpdateTemplateVersion(ctx, "my-template", &mailgun.TemplateVersion{ |
| 1063 | + Comment: "Add a comment to the template and make it 'active'", |
| 1064 | + Tag: "v2", |
| 1065 | + Active: true, |
| 1066 | + }) |
| 1067 | +} |
| 1068 | + |
| 1069 | +func ListTemplateVersions(domain, apiKey string) ([]mailgun.TemplateVersion, error) { |
| 1070 | + mg := mailgun.NewMailgun(domain, apiKey) |
| 1071 | + it := mg.ListTemplateVersions("my-template", nil) |
| 1072 | + |
| 1073 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 1074 | + defer cancel() |
| 1075 | + |
| 1076 | + var page, result []mailgun.TemplateVersion |
| 1077 | + for it.Next(ctx, &page) { |
| 1078 | + result = append(result, page...) |
| 1079 | + } |
| 1080 | + |
| 1081 | + if it.Err() != nil { |
| 1082 | + return nil, it.Err() |
| 1083 | + } |
| 1084 | + return result, nil |
| 1085 | +} |
0 commit comments