mirror of
https://github.com/GRFreire/nthmail.git
synced 2026-01-08 12:29:38 +00:00
add support for cc and bcc
now it can handle when mails are carbon-copied fixes #1 fixes #2
This commit is contained in:
parent
f3d8e7052f
commit
a8860ea560
@ -40,13 +40,13 @@ type Session struct {
|
||||
domain string
|
||||
}
|
||||
|
||||
func get_addr_domain(addr string) string {
|
||||
index := strings.Index(addr, "@")
|
||||
if index < 0 {
|
||||
return ""
|
||||
func append_addrs_with_domain(addrs []string, domain string, with_domain *[]string) {
|
||||
for _, a := range addrs {
|
||||
index := strings.Index(a, "@")
|
||||
if index > 0 && a[index+1:] == domain {
|
||||
*with_domain = append(*with_domain, a)
|
||||
}
|
||||
}
|
||||
|
||||
return addr[index+1:]
|
||||
}
|
||||
|
||||
func (session *Session) AuthPlain(username, password string) error {
|
||||
@ -61,9 +61,6 @@ func (session *Session) Mail(from string, opts *smtp.MailOptions) error {
|
||||
}
|
||||
|
||||
func (session *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
|
||||
if get_addr_domain(to) != session.domain {
|
||||
return errors.New("To addr domain is not available in this server")
|
||||
}
|
||||
session.rcpt = to
|
||||
|
||||
return nil
|
||||
@ -82,20 +79,28 @@ func (session *Session) Data(reader io.Reader) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if get_addr_domain(mail_obj.To) != session.domain {
|
||||
return errors.New("To addr domain is not available in this server")
|
||||
var addrs []string
|
||||
append_addrs_with_domain(mail_obj.To, session.domain, &addrs)
|
||||
append_addrs_with_domain(mail_obj.Cc, session.domain, &addrs)
|
||||
append_addrs_with_domain(mail_obj.Bcc, session.domain, &addrs)
|
||||
|
||||
if len(addrs) <= 0 {
|
||||
return errors.New("Not a single addr from to, cc and cc has the domain available in this server")
|
||||
}
|
||||
|
||||
stmt, err := session.tx.Prepare("INSERT INTO mails (arrived_at, rcpt_addr, from_addr, subject, data) VALUES (?, ?, ?, ?, ?)")
|
||||
if err != nil {
|
||||
println(err)
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
for _, addr := range addrs {
|
||||
stmt, err := session.tx.Prepare("INSERT INTO mails (arrived_at, rcpt_addr, from_addr, subject, data) VALUES (?, ?, ?, ?, ?)")
|
||||
if err != nil {
|
||||
println(err)
|
||||
return err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(session.arrived_at, addr, mail_obj.From, mail_obj.Subject, bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(session.arrived_at, session.rcpt, mail_obj.From, mail_obj.Subject, bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = session.tx.Commit()
|
||||
|
||||
@ -38,8 +38,9 @@ type Mail_obj struct {
|
||||
Id int
|
||||
From string
|
||||
Date time.Time
|
||||
To string
|
||||
Bcc string
|
||||
To []string
|
||||
Cc []string
|
||||
Bcc []string
|
||||
Subject string
|
||||
|
||||
Body []Mail_body
|
||||
@ -79,10 +80,26 @@ func Parse_mail(m_data []byte, header_only bool) (Mail_obj, error) {
|
||||
// HEADERS
|
||||
dec := new(mime.WordDecoder)
|
||||
m.From, _ = dec.DecodeHeader(mail_msg.Header.Get("From"))
|
||||
m.To, _ = dec.DecodeHeader(mail_msg.Header.Get("To"))
|
||||
m.Bcc, _ = dec.DecodeHeader(mail_msg.Header.Get("Bcc"))
|
||||
m.Subject, _ = dec.DecodeHeader(mail_msg.Header.Get("Subject"))
|
||||
|
||||
to_addrs, _ := mail_msg.Header.AddressList("To")
|
||||
m.To = make([]string, len(to_addrs))
|
||||
for i, a := range to_addrs {
|
||||
m.To[i] = a.Address
|
||||
}
|
||||
|
||||
cc_addrs, _ := mail_msg.Header.AddressList("Cc")
|
||||
m.Cc = make([]string, len(to_addrs))
|
||||
for i, a := range cc_addrs {
|
||||
m.Cc[i] = a.Address
|
||||
}
|
||||
|
||||
bcc_addrs, _ := mail_msg.Header.AddressList("Bcc")
|
||||
m.Bcc = make([]string, len(to_addrs))
|
||||
for i, a := range bcc_addrs {
|
||||
m.Bcc[i] = a.Address
|
||||
}
|
||||
|
||||
if header_only {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@ -159,7 +159,7 @@ func (sr ServerResouces) handleInbox(res http.ResponseWriter, req *http.Request)
|
||||
var mail_obj mail_utils.Mail_obj
|
||||
mail_obj.Id = m.Id
|
||||
mail_obj.Date = time.Unix(m.Arrived_at, 0)
|
||||
mail_obj.To = m.Rcpt_addr
|
||||
mail_obj.To = []string{m.Rcpt_addr}
|
||||
mail_obj.From = m.From_addr
|
||||
mail_obj.Subject = m.Subject
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user