本文整理汇总了Golang中net/mail.ParseAddressList函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseAddressList函数的具体用法?Golang ParseAddressList怎么用?Golang ParseAddressList使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseAddressList函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: saveHeaderFields
func (g *Gmail) saveHeaderFields(headerValue string) {
emails, err := mail.ParseAddressList(headerValue)
if err != nil {
log.Println("Unable to parse:", headerValue)
} else {
for _, v := range emails {
name := v.Name
email := v.Address
if !g.isKnownEmail(email) {
err := errors.New("")
err = g.db.SetContact(name, email)
if err != nil {
log.Println("Unable to save email:", email)
}
} else {
log.Println("Known email. Ignoring:", email)
}
}
}
}
示例2: AddressList
// AddressList returns a mail.Address slice with RFC 2047 encoded encoded names.
func (m *MIMEBody) AddressList(key string) ([]*mail.Address, error) {
isAddrHeader := false
for _, hkey := range AddressHeaders {
if strings.ToLower(hkey) == strings.ToLower(key) {
isAddrHeader = true
break
}
}
if !isAddrHeader {
return nil, fmt.Errorf("%s is not address header", key)
}
str := DecodeToUTF8Base64Header(m.header.Get(key))
if str == "" {
return nil, mail.ErrHeaderNotPresent
}
// These statements are handy for debugging ParseAddressList errors
// fmt.Println("in: ", m.header.Get(key))
// fmt.Println("out: ", str)
ret, err := mail.ParseAddressList(str)
if err != nil {
return nil, err
}
return ret, nil
}
示例3: getMultipleAddress
func getMultipleAddress(raw string) []*mail.Address {
addr, err := mail.ParseAddressList(raw)
if err != nil {
return nil
}
return addr
}
示例4: TestAddBccRecipients
func TestAddBccRecipients(t *testing.T) {
m := NewMail()
emails, _ := mail.ParseAddressList("Joe <[email protected]>, Doe <[email protected]>")
m.AddBccRecipients(emails)
if len(m.Bcc) != 2 {
t.Errorf("AddBccRecipients should append to SGMail.Bcc")
}
}
示例5: parseRecipients
func parseRecipients(input *string) []*mail.Address {
recipients, err := mail.ParseAddressList(*input)
if err != nil {
return []*mail.Address{}
}
log.Println("Recipients are", recipients)
return recipients
}
示例6: buildCCs
func buildCCs(m *Message) []*mail.Address {
cc, err := mail.ParseAddressList(m.CCs)
if err != nil {
cc = []*mail.Address{}
}
to, err := mail.ParseAddressList(m.To)
if err != nil {
to = []*mail.Address{}
}
all := append(to, cc...)
ret := make([]*mail.Address, 0)
for _, e := range all {
if !isMe(e) {
ret = append(ret, e)
}
}
return ret
}
示例7: String
func (m *Mailer) String() (string, error) {
message, header := "", make(map[string]string)
headers := map[string][]string{
"From": []string{m.Mail.From},
"To": m.Mail.To,
"Cc": m.Mail.Cc,
"Bcc": m.Mail.Bcc,
}
for key, addresses := range headers {
formated_addresses := []string{}
for _, address := range addresses {
parsed_addresses, err := mail.ParseAddressList(address)
if err == nil {
for _, parsed_address := range parsed_addresses {
if len(parsed_address.Name) > 0 {
formated_addresses = append(formated_addresses, fmt.Sprintf("%v <%v>", parsed_address.Name, parsed_address.Address))
} else {
formated_addresses = append(formated_addresses, fmt.Sprintf("%v", parsed_address.Address))
}
}
}
}
header[key] = strings.Join(formated_addresses, ", ")
}
header["Subject"] = m.Mail.Subject
header["MIME-Version"] = "1.0"
header["Content-Type"] = m.contentType()
header["Content-Transfer-Encoding"] = m.contentTransferEncoding()
for k, v := range header {
if len(v) > 0 {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
}
if len(m.Mail.Bodys) == 0 {
m.Body("")
}
for _, body := range m.Mail.Bodys {
message += m.crlfBoundary()
message += body.Encode() + "\r\n"
}
for _, attachment := range m.Mail.Attachments {
message += m.crlfBoundary()
message += attachment.Encode() + "\r\n"
}
message += m.endBoundary()
return message, nil
}
示例8: TestAddRecipients
func TestAddRecipients(t *testing.T) {
m := NewMail()
emails, _ := mail.ParseAddressList("Joe <[email protected]>, Doe <[email protected]>")
m.AddRecipients(emails)
switch {
case len(m.To) != 2:
t.Errorf("AddRecipients should append to SGMail.To")
case len(m.ToName) != 2:
t.Errorf("AddRecipients should append to SGMail.ToName if a valid email is supplied")
}
}
示例9: parseMultiExpressiveEmails
// Given a string like "Cathal Garvey <[email protected]>, Stephen Barr <[email protected]>"
// return []string{"Cathal Garvey <[email protected]>", "Stephen Barr <[email protected]>"}
func parseMultiExpressiveEmails(entry string) ([]string, error) {
out := make([]string, 0)
parsed, err := mail.ParseAddressList(entry)
if err != nil {
return nil, err
}
for _, m := range parsed {
out = append(out, m.String())
}
return out, nil
}
示例10: ParseAddressList
// ParseAddressList splits a comma separated list of addresses
// into multiple email addresses. The returned addresses can be
// used to call Send().
func ParseAddressList(s string) ([]string, error) {
addrs, err := mail.ParseAddressList(s)
if err != nil {
return nil, err
}
return generic.Map(addrs, func(addr *mail.Address) string {
if addr.Name != "" {
return fmt.Sprintf("%s <%s>", addr.Name, addr.Address)
}
return addr.Address
}).([]string), nil
}
示例11: ParseAddressList
func ParseAddressList() {
const list = `[email protected], Alice <[email protected]>, "Bob Uncle" <[email protected]>, "Yang-Tan, Eve" <[email protected]>`
emails, err := mail.ParseAddressList(list)
if err != nil {
log.Fatal(err)
}
for _, v := range emails {
fmt.Println(v.Name, "=>", v.Address)
}
}
示例12: NewMessageDebug
// Create a new email message, returning any reasons for failure.
func NewMessageDebug(from, to, subject, body string) (*Message, error) {
fromAddress, err := mail.ParseAddress(from)
if err != nil {
return nil, errors.New("Invalid from address; " + err.Error())
}
toAddresses, err := mail.ParseAddressList(to)
if err != nil {
return nil, errors.New("Invalid to address(es); " + err.Error())
}
return &Message{fromAddress, &AddressList{toAddresses}, subject, body}, nil
}
示例13: validateEmail
func validateEmail(ctx context.Context, address string, resolver bdns.DNSResolver) (prob *probs.ProblemDetails) {
emails, err := mail.ParseAddressList(address)
if err != nil {
return &probs.ProblemDetails{
Type: probs.InvalidEmailProblem,
Detail: unparseableEmailDetail,
}
}
if len(emails) > 1 {
return &probs.ProblemDetails{
Type: probs.InvalidEmailProblem,
Detail: multipleAddressDetail,
}
}
splitEmail := strings.SplitN(emails[0].Address, "@", -1)
domain := strings.ToLower(splitEmail[len(splitEmail)-1])
var resultMX []string
var resultA []net.IP
var errMX, errA error
var wg sync.WaitGroup
wg.Add(2)
go func() {
resultMX, errMX = resolver.LookupMX(ctx, domain)
wg.Done()
}()
go func() {
resultA, errA = resolver.LookupHost(ctx, domain)
wg.Done()
}()
wg.Wait()
if errMX != nil {
prob := bdns.ProblemDetailsFromDNSError(errMX)
prob.Type = probs.InvalidEmailProblem
return prob
} else if len(resultMX) > 0 {
return nil
}
if errA != nil {
prob := bdns.ProblemDetailsFromDNSError(errA)
prob.Type = probs.InvalidEmailProblem
return prob
} else if len(resultA) > 0 {
return nil
}
return &probs.ProblemDetails{
Type: probs.InvalidEmailProblem,
Detail: emptyDNSResponseDetail,
}
}
示例14: Write
func (w EMailWriter) Write(rs ...models.ScanResult) (err error) {
conf := config.Conf
to := strings.Join(conf.EMail.To[:], ", ")
cc := strings.Join(conf.EMail.Cc[:], ", ")
mailAddresses := append(conf.EMail.To, conf.EMail.Cc...)
if _, err := mail.ParseAddressList(strings.Join(mailAddresses[:], ", ")); err != nil {
return fmt.Errorf("Failed to parse email addresses: %s", err)
}
for _, r := range rs {
subject := fmt.Sprintf("%s%s %s",
conf.EMail.SubjectPrefix,
r.ServerInfo(),
r.CveSummary(),
)
headers := make(map[string]string)
headers["From"] = conf.EMail.From
headers["To"] = to
headers["Cc"] = cc
headers["Subject"] = subject
headers["Date"] = time.Now().Format(time.RFC1123Z)
headers["Content-Type"] = "text/plain; charset=utf-8"
var message string
for k, v := range headers {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + toFullPlainText(r)
smtpServer := net.JoinHostPort(conf.EMail.SMTPAddr, conf.EMail.SMTPPort)
err = smtp.SendMail(
smtpServer,
smtp.PlainAuth(
"",
conf.EMail.User,
conf.EMail.Password,
conf.EMail.SMTPAddr,
),
conf.EMail.From,
conf.EMail.To,
[]byte(message),
)
if err != nil {
return fmt.Errorf("Failed to send emails: %s", err)
}
}
return nil
}
示例15: checkContactEmail
// checkContactEmail checks the specified Contact Email and sets an appropriate erorr message
// if there's something wrong with it.
// Returns true if email is acceptable (valid or empty).
func checkContactEmail(p *page.Params, email string) (ok bool) {
if email == "" {
return true
}
if len(email) > 500 {
p.ErrorMsg = template.HTML(`<span class="code">Contact email</span> is too long! (cannot be longer than 500 characters)`)
return false
}
if _, err := mail.ParseAddressList(email); err != nil {
p.ErrorMsg = template.HTML(`Invalid <span class="code">Contact email</span>!`)
return false
}
return true
}