messageapi

package module
v0.0.0-...-0cd0786 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2017 License: Apache-2.0 Imports: 9 Imported by: 0

README

messageapi

The API to send the message by the email or the sms.

How to write the plugin?

For the api interface, see the doc.

For the invariable arguments each time to call the send interface, you should receive it by the interface mentod of Load; or use context.Context, such as context.WithValue.

For Email
  1. Implement the interface Email, that's, the two methods:
Load(map[string]string) error
SendEmail(context.Context, []string, string, string, map[string]io.Reader) error
  1. Register the plugin with a name by the function RegisterEmail:
RegisterEmail(pluginName, EmailPlugin)

By default, the api implements and registers the plain provider, which needs to Load the configuration options: host, port, username, password, from.

For SMS
  1. Implement the interface SMS, that's, the two methods:
Load(map[string]string) error
SendSMS(cxt context.Context, phone, content string) error
  1. Register the plugin with a name by the function RegisterSMS:
RegisterSMS(pluginName, SMSPlugin)

How to use?

  1. Get the provider with the name by GetSMS, or GetEmail.
  2. Load the configuration with the provider.
  3. Send the email or sms message.
Send the email
package main

import (
	"context"
	"fmt"
	"io"
	"os"

	"github.com/xgfone/messageapi"
)

func main() {
	// Get the email provider.
	email := messageapi.GetEmail("plain")
	if email == nil {
		fmt.Println("no email provider")
		return
	}

	// Load the configuration only once.
	config := map[string]string{
		"host":     "mail.example.com",
		"port":     "25",
		"username": "username",
		"password": "password",
		"from":     "username@example.com",
	}
	if err := email.Load(config); err != nil {
		fmt.Println(err)
		return
	}

	// Send the email without the attachment.
	to := "username@example.com"
	subject := "test"
	content := "test email send"
	if err := email.SendEmail(context.TODO(), []string{to}, subject, content, nil); err != nil {
		fmt.Println(err)
	}

	f, err := os.Open("test.txt")
	if err != nil {
		fmt.Println(err)
		return
	}

	// Send the email with the attachment for the first way.
	attachment1 := map[string]io.Reader{
		"test1.txt": f,
	}
	if err := email.SendEmail(context.TODO(), []string{to}, subject, content, attachment1); err != nil {
		fmt.Println(err)
	}

	// Send the email with the attachment for the second way.
	// The key is the path of the file, and the provider will open and read it.
	// The name of the attachment is same as filepath.Base(key).
	attachment2 := map[string]io.Reader{
		"test.txt": nil,
	}
	if err := email.SendEmail(context.TODO(), []string{to}, subject, content, attachment2); err != nil {
		fmt.Println(err)
	}
}
Send the sms

It is the similar to email.

App based on HTTP

You can use github.com/xgfone/messageapi/app to implement an app to send the Email or SMS based on HTTP. The example is as follow.

package main

import (
	"flag"

	"github.com/golang/glog"
	"github.com/xgfone/messageapi/app"
)

func main() {
	flag.Parse()
	c := app.NewDefaultConfig("")
	c.AllowGet = true // Allow to use the GET method to send the message
	c.Emails = map[string]map[string]string{
		"plain": map[string]string{
			"host":     "mail.example.com",
			"port":     "25",
			"username": "username",
			"password": "password",
			"from":     "username@example.com",
		},
	}
	glog.Error(app.Start(c, ":8080", "", ""))
}

Documentation

Overview

Package messageapi is the interface to send the message by the email or the sms.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAllEmails

func GetAllEmails() map[string]Email

GetAllEmails returns all the email providers.

func GetAllSMSs

func GetAllSMSs() map[string]SMS

GetAllSMSs returns all the sms providers.

func RegisterEmail

func RegisterEmail(name string, email Email)

RegisterEmail registers a Email provider implementation.

Notice: The plugin is a single instance in the global.

func RegisterSMS

func RegisterSMS(name string, sms SMS)

RegisterSMS registers a SMS provider implementation.

Notice: The plugin is a single instance in the global.

Types

type Config

type Config interface {
	Load(map[string]string) error
}

Config is the interface to load the configuration information.

The provider receives the configuration and should cache it. When the configuration changes, this function will be called. So it may be called more than once, and must be thread-safe.

The configuration passed by the interface Load should be invariable each time to call the send interface, such as SendSMS or SendEmail. Or please use the context in SendSMS, or SendEmail.

Notice: When failed to load the configuration, you should not use the corresponding plugin, or disable it for the moment until it's ok.

type Email

type Email interface {
	Config
	SendEmail(cxt context.Context, to []string, subject, content string,
		attachments map[string]io.Reader) error
}

Email is the interface which the email provider implements.

func GetEmail

func GetEmail(name string) Email

GetEmail returns a named Email provider.

Return nil if there is no the email provider named name.

type SMS

type SMS interface {
	Config
	SendSMS(cxt context.Context, phone, content string) error
}

SMS is the interface which the SMS provider implements.

func GetSMS

func GetSMS(name string) SMS

GetSMS returns a named SMS provider.

Return nil if there is no the sms provider named name.

Directories

Path Synopsis
Package app implements an interface to send the message based on HTTP.
Package app implements an interface to send the message based on HTTP.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL