本文整理汇总了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)
}
示例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
}
示例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)
}
}
示例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)
}
示例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
}
}
示例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
}
示例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
}
示例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
}
示例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)
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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)
}
}