天天看點

Go之發送釘釘和郵箱

smtp發送郵件

群發兩個郵箱,一個163,一個QQ
package main

import (
	"fmt"
	"net/smtp"
	"strings"
)

const (
	HOST        = "smtp.163.com"
	SERVER_ADDR = "smtp.163.com:25"
	USER        = "[email protected]"   //發送郵件的郵箱
	PASSWORD    = "xxxxx"         //發送郵件郵箱的密碼
)

type Email struct {
	to      string "to"
	subject string "subject"
	msg     string "msg"
}

func NewEmail(to, subject, msg string) *Email {
	return &Email{to: to, subject: subject, msg: msg}
}

func SendEmail(email *Email) error {
	auth := smtp.PlainAuth("", USER, PASSWORD, HOST)
	sendTo := strings.Split(email.to, ";")
	done := make(chan error, 1024)

	go func() {
		defer close(done)
		for _, v := range sendTo {

			str := strings.Replace("From: "+USER+"~To: "+v+"~Subject: "+email.subject+"~~", "~", "\r\n", -1) + email.msg

			err := smtp.SendMail(
				SERVER_ADDR,
				auth,
				USER,
				[]string{v},
				[]byte(str),
			)
			done <- err
		}
	}()
	for i := 0; i < len(sendTo); i++ {
		<-done
	}
	return nil
}

func main() {
	mycontent := "this is go test email"
	email := NewEmail("[email protected];[email protected];",
		"test golang email", mycontent)
	err := SendEmail(email)
	fmt.Println(err)
}
           
驗證郵箱
Go之發送釘釘和郵箱
Go之發送釘釘和郵箱

發送釘釘簡介

釘釘報警設定

建立群機器人
Go之發送釘釘和郵箱
Go之發送釘釘和郵箱
接口位址
Go之發送釘釘和郵箱

發送短消息

發送普通消息
package main

import (
	"fmt"
	"net/http"
	"strings"
)

func main() {
	SendDingMsg("zhou","1234")
}

func SendDingMsg(name ,msg string) {
	//請求位址模闆
	webHook := `https://xxxx`
	content := `{"msgtype": "text",
		"text": {"content": "` + name+ ":" + msg + `"}
	}`
	//建立一個請求
	req, err := http.NewRequest("POST", webHook, strings.NewReader(content))
	if err != nil {
		// handle error
	}

	client := &http.Client{}
	//設定請求頭
	req.Header.Set("Content-Type", "application/json; charset=utf-8")
	//發送請求
	resp, err := client.Do(req)
	//關閉請求
	defer resp.Body.Close()

	if err != nil {
		// handle error
		fmt.Println(err)
	}
}

           
Go之發送釘釘和郵箱