当前位置: 首页>>代码示例>>Golang>>正文


Golang mail.ParseAddress函数代码示例

本文整理汇总了Golang中net/mail.ParseAddress函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseAddress函数的具体用法?Golang ParseAddress怎么用?Golang ParseAddress使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了ParseAddress函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Send

// Send an email using the given host and SMTP auth (optional), returns any error thrown by smtp.SendMail
// This function merges the To, Cc, and Bcc fields and calls the smtp.SendMail function using the Email.Bytes() output as the message
func (e *Email) Send(addr string, a smtp.Auth) error {
	// Merge the To, Cc, and Bcc fields
	to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc))
	to = append(append(append(to, e.To...), e.Cc...), e.Bcc...)
	for i := 0; i < len(to); i++ {
		addr, err := mail.ParseAddress(to[i])
		if err != nil {
			return err
		}
		to[i] = addr.Address
	}
	// Check to make sure there is at least one recipient and one "From" address
	if e.From == "" || len(to) == 0 {
		return errors.New("Must specify at least one From address and one To address")
	}
	from, err := mail.ParseAddress(e.From)
	if err != nil {
		return err
	}
	raw, err := e.Bytes()
	if err != nil {
		return err
	}
	return smtp.SendMail(addr, a, from.Address, to, raw)
}
开发者ID:flyeven,项目名称:email,代码行数:27,代码来源:email.go

示例2: sendMail

func sendMail(from, to, subject, htmlContent string, mailer *mail.Mailer,
	debug bool, success chan *email.Email, fail chan error) {

	parsedSender, err := stdMail.ParseAddress(from)
	if err != nil {
		fail <- err
		return
	}

	parsedTo, err := stdMail.ParseAddress(to)
	if err != nil {
		fail <- err
		return
	}

	message, err := mail.NewMessage(
		parsedSender,
		parsedTo,
		subject,
		htmlContent,
	)
	if err != nil {
		fail <- err
		return
	}

	if err := mailer.Send(message); err != nil {
		fail <- err
		return
	}

	success <- message
}
开发者ID:kevgathuku,项目名称:mailer,代码行数:33,代码来源:home.go

示例3: testValidate

func testValidate(t *testing.T, s *Service) {
	// Valid
	userEmail, pwd, err := getUserCreds(t)
	if e(t, err) {
		return
	}
	eml, err := mail.ParseAddress(userEmail)
	if e(t, err) {
		return
	}
	valid, err := s.Validate(*eml, pwd)
	if e(t, err) {
		return
	}
	if !valid {
		t.Errorf("expected Validate to return true but returned %t", valid)
	}
	// Invalid
	eml, err = mail.ParseAddress("[email protected]")
	if e(t, err) {
		return
	}
	valid, err = s.Validate(*eml, "invalidpassword")
	if e(t, err) {
		return
	}
	if valid {
		t.Errorf("expected Validate to return false but returned %t", valid)
	}
}
开发者ID:homemade,项目名称:justin,代码行数:30,代码来源:justin_test.go

示例4: dispatchMessage

func (s *EmailServer) dispatchMessage(m *Message) error {
	var e email.Email
	//err := ds.LoadStruct(&e, []byte(m.Data))
	err := ds.Load(&e, []byte(m.Data))
	if err != nil {
		return err
	}

	// Merge the To, Cc, and Bcc fields
	to := make([]string, 0, len(e.To)+len(e.Cc)+len(e.Bcc))
	to = append(append(append(to, e.To...), e.Cc...), e.Bcc...)
	for i := 0; i < len(to); i++ {
		addr, err := mail.ParseAddress(to[i])
		if err != nil {
			return err
		}
		to[i] = addr.Address
	}

	// Check to make sure there is at least one recipient and one "From" address
	if e.From == "" || len(to) == 0 {
		return errors.New("Must specify at least one From address and one To address")
	}
	from, err := mail.ParseAddress(e.From)
	if err != nil {
		return err
	}
	raw, err := e.Bytes()
	if err != nil {
		return err
	}

	return smtp.SendMail(s.address, s.auth, from.Address, to, raw)
}
开发者ID:rightjoin,项目名称:mailman,代码行数:34,代码来源:email_server.go

示例5: testAccountAvailabilityCheck

func testAccountAvailabilityCheck(t *testing.T, s *Service) {
	// Available
	eml, err := mail.ParseAddress("[email protected]g.com")
	if e(t, err) {
		return
	}
	avail, err := s.AccountAvailabilityCheck(*eml)
	if e(t, err) {
		return
	}
	if !avail {
		t.Errorf("expected AccountAvailabilityCheck to return true but returned %t", avail)
	}
	// UnAvailable
	userEmail, _, err := getUserCreds(t)
	if e(t, err) {
		return
	}
	eml, err = mail.ParseAddress(userEmail)
	if e(t, err) {
		return
	}
	avail, err = s.AccountAvailabilityCheck(*eml)
	if e(t, err) {
		return
	}
	if avail {
		t.Errorf("expected AccountAvailabilityCheck to return false but returned %t", avail)
		return
	}
}
开发者ID:homemade,项目名称:justin,代码行数:31,代码来源:justin_test.go

示例6: NewMailAuthHook

// NewMailAuthHook creates a hook to be added to an instance of logger.
func NewMailAuthHook(appname string, host string, port int, from string, to string, username string, password string) (*MailAuthHook, error) {
	// Check if server listens on that port.
	conn, err := net.DialTimeout("tcp", host+":"+strconv.Itoa(port), 3*time.Second)
	if err != nil {
		return nil, err
	} else {
		defer conn.Close()
	}

	// Validate sender and recipient
	sender, err := mail.ParseAddress(from)
	if err != nil {
		return nil, err
	}
	receiver, err := mail.ParseAddress(to)
	if err != nil {
		return nil, err
	}

	return &MailAuthHook{
		AppName:  appname,
		Host:     host,
		Port:     port,
		From:     sender,
		To:       receiver,
		Username: username,
		Password: password}, nil
}
开发者ID:rikke,项目名称:logrus_mail,代码行数:29,代码来源:mail.go

示例7: sendMail

func sendMail(recipient Recipient, emailField string, mailer *mail.Mailer,
	success chan Recipient, fail chan error) {

	parsedSender, err := stdMail.ParseAddress(sender)
	if err != nil {
		fail <- err
		return
	}

	parsedTo, err := stdMail.ParseAddress(recipient[emailField])
	if err != nil {
		fail <- err
		return
	}

	message, err := mail.NewMessage(
		parsedSender,
		parsedTo,
		subject,
		textTemplatePath,
		htmlTemplatePath,
		recipient,
	)
	if err != nil {
		fail <- err
		return
	}

	if err := mailer.Send(message); err != nil {
		fail <- err
		return
	}

	success <- recipient
}
开发者ID:BlastarIndia,项目名称:postman,代码行数:35,代码来源:mail.go

示例8: NewMailHook

// NewMailHook creates a hook to be added to an instance of logger.
func NewMailHook(appname string, host string, port int, from string, to string) (*MailHook, error) {
	// Connect to the remote SMTP server.
	c, err := smtp.Dial(host + ":" + strconv.Itoa(port))
	if err != nil {
		return nil, err
	}

	// Validate sender and recipient
	sender, err := mail.ParseAddress(from)
	if err != nil {
		return nil, err
	}
	recipient, err := mail.ParseAddress(to)
	if err != nil {
		return nil, err
	}

	// Set the sender and recipient.
	if err := c.Mail(sender.Address); err != nil {
		return nil, err
	}
	if err := c.Rcpt(recipient.Address); err != nil {
		return nil, err
	}

	return &MailHook{
		AppName: appname,
		c:       c,
	}, nil

}
开发者ID:rikke,项目名称:logrus_mail,代码行数:32,代码来源:mail.go

示例9: sendEmail

func sendEmail(headers map[string]string, body string, address string, gConfig map[string]string) {

	host := gConfig["REMOTE_SMTP_HOST"]
	port := gConfig["REMOTE_SMTP_PORT"]
	user := gConfig["REMOTE_SMTP_USER"]
	password := gConfig["REMOTE_SMTP_PASS"]

	m := email.NewMessage(headers["Subject"], body)

	fromAddress := "Unknown"
	fromHeader, ok := headers["From"]
	if ok {
		from, _ := mail.ParseAddress(fromHeader)
		fromAddress = from.Address
	}

	to, _ := mail.ParseAddress(address)
	toAddress := to.Address

	m.From = fromAddress
	m.To = []string{toAddress}

	if gConfig["PGP_ATTACH_BODY"] == "Y" {
		m.AttachData("message.asc", []byte(body))
	}

	err := email.Send(host+":"+port, smtp.PlainAuth("", user, password, host), m)
	logging.CheckFatal(err)
}
开发者ID:elmundio87,项目名称:pgp-email-relay,代码行数:29,代码来源:pgp_encrypt.go

示例10: Validate

func (p *Post) Validate() error {
	if p.Replyto != "" {
		t := ""
		if !strings.Contains(p.Replyto, "<") || !strings.Contains(p.Replyto, ">") {
			t = p.Replyto + " <" + p.Replyto + ">"
		}
		a, err := mail.ParseAddress(t)
		if err != nil {
			return err
		}
		p.Replyto = a.Address
	}
	if len(p.CC) > 0 {
		for k, v := range p.CC {
			t := ""
			if !strings.Contains(v, "<") || !strings.Contains(v, ">") {
				t = v + " <" + v + ">"
			}
			a, err := mail.ParseAddress(t)
			if err != nil {
				return err
			}
			p.CC[k] = a.Address
		}
	}

	return nil
}
开发者ID:codemodus,项目名称:formlark,代码行数:28,代码来源:datatypes.go

示例11: NewSESHook

// NewSESHook creates a hook to be added to an instance of logger.
func NewSESHook(appName string, level log.Level) (*SESHook, error) {
	// Validate sender and recipient
	sender, err := mail.ParseAddress(getFrom())
	if err != nil {
		return nil, err
	}
	receiver, err := mail.ParseAddress(getTo())
	if err != nil {
		return nil, err
	}

	// Get SES specific username and password
	username := getUsername()
	if len(username) == 0 {
		return nil, errors.New("SES_ACCESS_KEY_ID not set.")
	}
	password := getPassword()
	if len(password) == 0 {
		return nil, errors.New("SES_SECRET_ACCESS_KEY not set.")
	}

	return &SESHook{
		AppName: appName,
		From:    sender,
		To:      receiver,
		Auth:    smtp.PlainAuth("", username, password, SESServer),
		Level:   level,
	}, nil
}
开发者ID:yxd-hde,项目名称:logrus_ses,代码行数:30,代码来源:ses.go

示例12: Deliver

func (s *SMTPDeliverer) Deliver(ctx scope.Context, ref *EmailRef) error {
	// Connect and authenticate to SMTP server.
	c, err := smtp.Dial(s.addr)
	if err != nil {
		return fmt.Errorf("%s: dial error: %s", s, err)
	}
	defer c.Quit()

	if err := c.Hello(s.localName); err != nil {
		return fmt.Errorf("%s: ehlo error: %s", s, err)
	}

	if s.tlsConfig != nil {
		if err := c.StartTLS(s.tlsConfig); err != nil {
			return fmt.Errorf("%s: starttls error: %s", s, err)
		}
	}

	if s.auth != nil {
		if err := c.Auth(s.auth); err != nil {
			return fmt.Errorf("%s: auth error: %s", s, err)
		}
	}

	// Send email.
	if ref.SendFrom == "" {
		ref.SendFrom = "[email protected]" + s.localName
	}
	sendFrom, err := mail.ParseAddress(ref.SendFrom)
	if err != nil {
		return fmt.Errorf("%s: from address error: %s", s, err)
	}
	if err := c.Mail(sendFrom.Address); err != nil {
		return fmt.Errorf("%s: mail error: %s", s, err)
	}

	sendTo, err := mail.ParseAddress(ref.SendTo)
	if err != nil {
		return fmt.Errorf("%s: to address error: %s", s, err)
	}
	if err := c.Rcpt(sendTo.Address); err != nil {
		return fmt.Errorf("%s: rcpt error: %s", s, err)
	}

	wc, err := c.Data()
	if err != nil {
		return fmt.Errorf("%s: data error: %s", s, err)
	}

	if _, err := wc.Write(ref.Message); err != nil {
		return fmt.Errorf("%s: write error: %s", s, err)
	}
	if err := wc.Close(); err != nil {
		return fmt.Errorf("%s: close error: %s", s, err)
	}

	ref.Delivered = time.Now()
	return nil
}
开发者ID:logan,项目名称:heim,代码行数:59,代码来源:smtp.go

示例13: Dequeue

// Dequeue returns jobs from the queue. It maps hosts to the packages
// that need to be delivered to them. If anything goes wrong, Dequeue
// returns an error. Dequeue will never return more hosts than the
// current MaxHostsPerDequeue value.
func (mb mailBox) Dequeue() (map[string]Package, error) {
	rows, err := mb.dequeueStmt.Query(MaxHostsPerDequeue)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	jobs := make(map[string]Package)
	cache := make(map[uint64]*Message)
	for rows.Next() {
		var row struct {
			User, Host  string
			Date        pq.NullTime
			Attempts    int
			MID         uint64
			MRaw, MFrom string
		}
		err := rows.Scan(&row.Host, &row.MID, &row.User, &row.Date,
			&row.Attempts, &row.MRaw, &row.MFrom)
		if err != nil {
			return jobs, err
		}
		if jobs[row.Host] == nil {
			jobs[row.Host] = make(Package)
		}
		msg, ok := cache[row.MID]
		if !ok {
			msg = &Message{
				ID:  row.MID,
				Raw: row.MRaw,
			}
			addr, err := mail.ParseAddress(row.MFrom)
			if err != nil {
				return nil, err
			}
			msg.SetFrom(addr)
			cache[row.MID] = msg
		}
		if jobs[row.Host][msg] == nil {
			jobs[row.Host][msg] = make([]*mail.Address, 0, 1)
		}
		dest, err := mail.ParseAddress(row.User + "@" + row.Host)
		if err != nil {
			return nil, err
		}
		jobs[row.Host][msg] = append(jobs[row.Host][msg], dest)
	}
	return jobs, nil
}
开发者ID:gbbr,项目名称:gomez,代码行数:52,代码来源:dequeuer.go

示例14: insertTargetIntoGroup

func insertTargetIntoGroup(t Target, gid int64) error {
	if _, err = mail.ParseAddress(t.Email); err != nil {
		Logger.Printf("Invalid email %s\n", t.Email)
		return err
	}
	trans := db.Begin()
	trans.Where(t).FirstOrCreate(&t)
	if err != nil {
		Logger.Printf("Error adding target: %s\n", t.Email)
		return err
	}
	err = trans.Where("group_id=? and target_id=?", gid, t.Id).Find(&GroupTarget{}).Error
	if err == gorm.RecordNotFound {
		err = trans.Save(&GroupTarget{GroupId: gid, TargetId: t.Id}).Error
		if err != nil {
			Logger.Println(err)
			return err
		}
	}
	if err != nil {
		Logger.Printf("Error adding many-many mapping for %s\n", t.Email)
		return err
	}
	err = trans.Commit().Error
	if err != nil {
		Logger.Printf("Error committing db changes\n")
		return err
	}
	return nil
}
开发者ID:tohitsugu,项目名称:gophish,代码行数:30,代码来源:group.go

示例15: LoadConfig

// LoadConfig will try to search around for the corresponding config file.
// It will search /tmp/fileName then attempt ./config/fileName,
// then ../config/fileName and last it will look at fileName
func LoadConfig(fileName string) {

	fileName = findConfigFile(fileName)
	l4g.Info("Loading config file at " + fileName)

	file, err := os.Open(fileName)
	if err != nil {
		panic("Error opening config file=" + fileName + ", err=" + err.Error())
	}

	decoder := json.NewDecoder(file)
	config := Config{}
	err = decoder.Decode(&config)
	if err != nil {
		panic("Error decoding configuration " + err.Error())
	}

	// Check for a valid email for feedback, if not then do [email protected]
	if _, err := mail.ParseAddress(config.EmailSettings.FeedbackEmail); err != nil {
		config.EmailSettings.FeedbackEmail = "[email protected]"
		l4g.Error("Misconfigured feedback email setting: %s", config.EmailSettings.FeedbackEmail)
	}

	configureLog(config.LogSettings)

	Cfg = &config
	SanitizeOptions = getSanitizeOptions()

	// Validates our mail settings
	if err := CheckMailSettings(); err != nil {
		l4g.Error("Email settings are not valid err=%v", err)
	}
}
开发者ID:crspeller,项目名称:platform,代码行数:36,代码来源:config.go


注:本文中的net/mail.ParseAddress函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。