send

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2016 License: Apache-2.0 Imports: 7 Imported by: 1

README

基于golang的smtp包实现的邮件发送

GoDoc

支持异步发送邮件、一对一发送、群发

获取

$ go get -v github.com/go-mailer/send

使用范例(异步发送)

package main

import (
	"bytes"
	"fmt"
	"net/mail"
	"sync"

	"github.com/go-mailer/send"
)

func main() {
	from := mail.Address{Name: "Lyric", Address: "nianshou.tian@zjelite.com"}
	sender, err := send.NewSmtpSender("smtp.exmail.qq.com:25", from, "xxx")
	if err != nil {
		panic(err)
	}
	var wg sync.WaitGroup
	wg.Add(1)
	msg := &send.Message{
		Subject: "异步发送邮件测试",
		Content: bytes.NewBufferString("<h1>你好,异步测试邮件内容</h1>"),
		To:      "<tiannianshou@126.com>",
	}
	err = sender.AsyncSend(msg, false, func(err error) {
		defer wg.Done()
		if err != nil {
			fmt.Println("发送邮件出现错误:", err)
		}
	})
	if err != nil {
		panic(err)
	}
	wg.Wait()
	fmt.Println("邮件发送完成")
}

License

Copyright 2016.All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	Subject   string            // 标题
	Content   io.Reader         // 支持html的消息主体
	To        string            // 邮箱地址,格式:"Alice <alice@example.com>, Bob <bob@example.com>, Eve <eve@example.com>"
	Extension map[string]string // 发送邮件消息体扩展项
}

Message 邮件发送数据

type Sender

type Sender interface {
	// Send 发送邮件
	// msg 邮件发送数据
	// isMass 是否是群发,默认为一对一发送
	Send(msg *Message, isMass bool) error

	// AsyncSend 异步发送邮件
	// msg 邮件发送数据
	// isMass 是否是群发,默认为一对一发送
	// handle 发送完成之后的处理函数,如果发送失败,则返回error
	AsyncSend(msg *Message, isMass bool, handle func(err error)) error
}

Sender 提供邮件发送接口

func NewSmtpSender

func NewSmtpSender(addr string, from mail.Address, authPwd string) (Sender, error)

NewSmtpSender 创建基于smtp的邮件发送实例(使用PlainAuth) addr 服务器地址 from 发送者 authPwd 验证密码 如果创建实例发生异常,则返回错误

type SmtpClient

type SmtpClient struct {
	// contains filtered or unexported fields
}

SmtpClient 使用smtp发送邮件

func (*SmtpClient) AsyncSend

func (this *SmtpClient) AsyncSend(msg *Message, isMass bool, handle func(err error)) error

AsyncSend 异步发送邮件

func (*SmtpClient) Send

func (this *SmtpClient) Send(msg *Message, isMass bool) error

Send 发送邮件

Jump to

Keyboard shortcuts

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